需要SQL Group By和AGGREGATE帮助

时间:2014-06-14 14:13:09

标签: mysql

我有两张桌子......

  • 订单(orderNumber,orderDate,shippedDate,requiredDate,status,customerNumber)和
  • 订单明细(ORDERNUMBER,PRODUCTCODE,quantityOrdered,priceEach,orderLineNumber)

我试图返回这样的查询...

+----------------------------------+--------------------+
| DATEDIFF(shippedDate, orderDate) | COUNT(shippedDate) |
+----------------------------------+--------------------+
|                                1 |                 50 |
|                                2 |                 49 |
|                                3 |                 52 |
|                                4 |                 52 |
|                                5 |                 59 |
|                                6 |                 45 |
|                                7 |                  2 |
|                                8 |                  2 |
|                               65 |                  1 |
+----------------------------------+--------------------+

但是我没有得到它。我知道我必须做一个分组并使用DateDiff()和Count()......

我试过这个但是不对 mysql:

select DATEDIFF(shippedDate,orderDate), COUNT(shippedDate) from Orders Group by shippedDate;

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您需要group by datediff()

select DATEDIFF(shippedDate, orderDate), COUNT(shippedDate)
from Orders
Group by DATEDIFF(shippedDate, orderDate);

答案 1 :(得分:1)

您需要在group by语句中添加datediff(shippedDate,orderDate)而不是shippingDate