以下是Access中的資料部分:
icode soldQty rackQty dt
---------------------------------------
14000 10 50 03/17/22 20:35
15000 1 45 03/17/22 15:35
16000 3 55 03/17/22 08:22
14000 30 48 03/18/22 14:05
15000 18 62 03/17/22 13:35
16000 3 47 03/17/22 15:23
14000 1 49 03/19/22 16:35
17000 1 49 03/17/22 15:13
14000 24 26 03/17/22 10:35
15000 10 33 03/17/22 20:37
有 100 多個 icode,我僅以 3 個為例。我想根據某個時間段生成每周(或某些日期之間)報告。比如說,10:00 到 17:00 之間的每周報告 14000、15000 和 16000 三個專案。我期望的輸出是:
icode soldQty rackQty
14000 54 74
15000 19 107
16000 4 96
使用這個查詢,我可以得到總和。
select icode, sum(soldQty), sum(rackQty) from sales
group by icode having icode between 14000 and 16000
order by icode
我很困惑在哪里放置where clause以便我可以有時間(10:00 到 17:00)約束的條件?
uj5u.com熱心網友回復:
所有條件都應放在WHERE子句中。
該HAVING子句用于包含聚合值(如SUM()or )的條件,COUNT()并在聚合后處理:
SELECT icode,
SUM(soldQty) AS total_soldQty,
SUM(rackQty) AS total_rackQty
FROM sales
WHERE icode BETWEEN 14000 AND 16000
AND TimeValue(dt) BETWEEN #10:00:00# AND #17:00:00#
GROUP BY icode
ORDER BY icode;
uj5u.com熱心網友回復:
嘗試這個:
select icode, sum(soldQty), sum(rackQty)
from sales
where TimeValue(dt) between #10:00:00# and #17:00:00#
group by icode
having icode between 14000 and 16000
order by icode
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/446905.html
