0

我有一张桌子,里面有各种订单:

ID | Date       | etc...
1  | 2013-01-01 | etc
2  | 2013-02-01 | etc
3  | 2013-03-01 | etc
4  | 2013-04-01 | etc
5  | 2013-05-01 | etc
6  | 2013-06-01 | etc
7  | 2013-06-01 | etc
8  | 2013-03-01 | etc
9  | 2013-04-01 | etc
10 | 2013-05-01 | etc

我想要一个以结果结尾的查询:

overallTotal | totalThisMonth | totalLastMonth
10           | 2              | 1

但我想在一个查询中做到这一点!我正在尝试找到一种使用子查询来执行此操作的方法。到目前为止,我有:

SELECT * from (
  SELECT count(*) as overallTotal from ORDERS
)

如何将其与其他子查询结合起来,以便在一个查询中获得总数?

更新

最初的问题是针对 MySQL 的,但我现在需要 Firebird。

4

4 回答 4

2

使用条件和可以做到这一点(MySQL 语法):

select 
    count(*) as overallTotal, 
    sum(if(Month(Date)+12*Year(Date)=Month(GetDate())+12*Year(GetDate()), 1, 0)) 
       as totalThisMonth 
    sum(if(Month(Date)+12*Year(Date)=Month(GetDate())+12*Year(GetDate())-1, 1, 0)) 
       as totalThisMonth;
from mytable

使用month+12*year公式可以避免年份变化的问题。

更新

对于Firebird ,同样适用,您只需Month(x)EXTRACT (MONTH FROM x)Year(x)和替换。这看起来很难看,所以我不会把它放在这里,但你可以很容易地自己做。EXTRACT (YEAR FROM x)Getdate()CURRENT_TIME

于 2013-06-27T10:25:03.353 回答
0

我看到您已经在使用子查询,所以为什么不执行以下操作,

SELECT
(SELECT count(*) from ORDERS) as overallTotal,
(SELECT count(*) from ORDERS where Date between ... and ...) as totalThisMonth,
(SELECT count(*) from ORDERS where Date between ... and ...) as overallTotal
于 2013-06-27T10:30:30.773 回答
0

合理地使用 IF 语句结果的 SUM,IF 检查它是否是相关日期。

SELECT COUNT(*), 
    SUM(IF(YEAR(Date) = YEAR(CURDATE()) AND MONTH(Date) = MONTH(CURDATE()), 1, 0))
    SUM(IF((YEAR(Date) = YEAR(CURDATE()) AND MONTH(Date) = MONTH(CURDATE()) - 1) OR (YEAR(Date) = YEAR(CURDATE()) - 1 AND MONTH(Date) = 12 AND MONTH(CURDATE()) = 1), 1, 0))
from ORDERS

复杂性是由于在查找上个月时应对年份的变化而引起的

于 2013-06-27T10:23:24.790 回答
0
SELECT
  (SELECT COUNT(*) FROM C AS total) AS T,
  (SELECT COUNT(*) AS thisMonth FROM C WHERE MONTH(d) = 6) AS A,
  (SELECT COUNT(*) AS lastMonth FROM C WHERE MONTH(d) = 5) AS B;

请注意“月份”是“硬编码”的——但在应用程序级别提取“当前月份”应该不会太难。如果你真的需要,你可以在 SQL 级别使用类似的东西

SELECT
  (SELECT COUNT(*) FROM C AS total) AS T,
  (SELECT COUNT(*) AS thisMonth FROM C WHERE MONTH(d) = MONTH(NOW()) ) AS A,
  (SELECT COUNT(*) AS lastMonth FROM C WHERE MONTH(d) = (MONTH(NOW())+11)%12) ) AS B;
于 2013-06-27T10:28:44.040 回答