简体   繁体   English

选择总计作为同一个表中的三个字段作为一个查询?

[英]Selecting totals as three fields from same table as one query?

I have a table with various orders in it: 我有一张包含各种订单的表格:

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

I want a query that ends wit the result: 我想要一个以结果结束的查询:

overallTotal | totalThisMonth | totalLastMonth
10           | 2              | 1

But I want to do this in one query! 但我想在一个查询中执行此操作! I am trying to find a way to use subqueries to do this. 我试图找到一种方法来使用子查询来执行此操作。 SO far I have: 我到目前为止:

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

How can I combine this with other subqueries so I can get the totals in one query? 如何将其与其他子查询结合使用,以便在一个查询中获取总计?

UPDATE UPDATE

Original question was for MySQL, but I need it for Firebird now. 最初的问题是MySQL,但我现在需要Firebird。

With conditional sums you can do it (MySQL syntax): 使用条件求和,你可以做到(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

Use the month+12*year formula to avoid the problem with year change. 使用month+12*year公式可以避免年度变化的问题。

UPDATE UPDATE

With Firebird the same applies, you only have to replace Month(x) with EXTRACT (MONTH FROM x) , Year(x) with EXTRACT (YEAR FROM x) and Getdate() with CURRENT_TIME . 使用Firebird同样适用,您只需要使用EXTRACT (MONTH FROM x)替换Month(x) ,使用EXTRACT (YEAR FROM x) Getdate() Year(x) ,使用CURRENT_TIME Getdate() This will look ugly, so I won't put it here, but you could easily do it yourself. 这看起来很难看,所以我不会把它放在这里,但你可以轻松地自己做。

Posubly use SUM of the result of IF statements, with the IF checking that it is a relevant date. Posubly使用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

Complexity is caused by coping with a change of year when looking for the previous month 复杂性是由于在寻找上个月时应对年度变化而引起的

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;

Please notice "month" are "hard coded" -- but it shouldn't be too difficult to extract the "current month" at application level. 请注意“月份”是“硬编码” - 但在应用程序级别提取“当前月份”应该不会太困难。 If you really need to, you could do it at SQL level with something like that: 如果你真的需要,你可以在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;

I see you are already using sub queries, so why not do something like the following, 我看到你已经在使用子查询,所以为什么不做类似下面的事情,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM