4
我試圖獲得一天內銷售數量的總和。我需要結果在ORDER BY日期。下面的查詢給我我需要的確切結果,除了日期沒有格式化爲我所需要的。Group By Date和SUM int - 日期格式和訂單
SELECT CAST(Datetime AS DATE) AS 'date', SUM(quantity) as total_quantity
FROM Invoice_Itemized ii
INNER JOIN Invoice_Totals it ON it.Invoice_Number = ii.Invoice_Number
WHERE ii.ItemNum = '4011'
AND it.datetime > '05/15/2015'
GROUP BY CAST(Datetime AS DATE)
SELECT DATENAME(MM, datetime) + ' ' + CAST(DAY(datetime) AS VARCHAR(2)) AS [DD Month], SUM(quantity) as total_quantity
FROM Invoice_Itemized ii
INNER JOIN Invoice_Totals it ON it.Invoice_Number = ii.Invoice_Number
WHERE ii.ItemNum = '4011'
AND it.datetime > '05/15/2015'
GROUP BY DATENAME(MM, datetime) + ' ' + CAST(DAY(datetime) AS VARCHAR(2))
結果頂端查詢:
- 2015年5月15日91.43
- 2015年5月16日84.77
結果底部查詢:
- 6月1日128.34
- 6月10日85.06
頂部查詢爲我提供了我需要的信息,以我需要的順序。 底部查詢爲我提供了我需要的日期格式,但順序錯誤。
我測試你的第二個查詢。正如我所需要的那樣工作。謝謝! – user3556177