我正在嘗試創建一個顯示每小時銷售額的 SQL 報告。這是來自我的 POS 系統。
有一個內置表,基本上包含我需要的所有資料,如果它們在同一小時內,我只是在努力計算一列的總和。
表vRPTHourlySalesPerformance(精簡為僅顯示相關資訊):
| dt_when | create_hour_ordinal | c_ticketitem_net_price |
|---|---|---|
| 2022-11-07 11:20 | 11 | 16.00 |
| 2022-11-07 12:20 | 11 | 17.00 |
| 2022-11-07 13:20 | 12 | 18.00 |
我正在尋找一種輸出以下內容的方法。
| 小時 | 總銷售額 |
|---|---|
| 11 | 33.00 |
| 12 | 18.00 |
這是據我所知:
DECLARE @start_of_day datetime;
DECLARE @now datetime;
SET @now = getdate();
SET @start_of_day = dbo.dwf_beginofday_for_day(@now);
--Creates Sales by Hour Table
DECLARE @reportable_table AS TABLE (
Hour int,
[Total Sales] money
)
INSERT INTO @reportable_table
SELECT create_hour_ordinal AS 'Hour', c_ticketitem_net_price AS 'Total Sales'
FROM vRPTHourlySalesPerformance
WHERE dt_when >= @start_of_day AND dt_when < @now
uj5u.com熱心網友回復:
看來你是在一個簡單的聚合之后:
insert into @reportable_table([hour], [total Sales])
Select create_hour_ordinal, sum(c_ticketitem_net_price)
from vRPTHourlySalesPerformance
where dt_when >= @start_of_day AND dt_when < @now
group by create_hour_ordinal;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/530029.html
標籤:sqlsql服务器sql-server-2016销售点
上一篇:如何在字串中使用字符'#?
