1
我正在執行基於「運行總數」概念的SQL代碼。我的代碼正在計算準確的運行總數,但我需要在每週完成後才顯示運行總數,而不是按日期。如何顯示不同的星期?
Select DISTINCT SD1.[Week],
SD1.[Allocation],
SD1.[Subsidiaries],
SD1.[Month],
SD1.[Date],
(SD1.[Daily Sale]+IsNull(SUM(SD2.[Daily Sale]),0)) as 'Running Total'
from [dbo].[Solar data] SD1
left join [dbo].[Solar data] SD2 on SD1.[Subsidiaries]=SD2.[Subsidiaries]
and SD2.[Date]<SD1.[Date] And SD1.[Month]=SD2.[Month]
where SD1.[Subsidiaries]='WCL'
group by SD1.[Allocation],
SD1.[Month],
SD1.[Subsidiaries],
SD1.[Daily Sale],
SD1.[Week],SD1.[Date]
order by SD1.[Week]
+---------------+------------+------------+-----------+--------+-------+
| Subsidiaries | Allocation | Daily Sale | Date | Month | Week |
+---------------+------------+------------+-----------+--------+-------+
| WCL | 1075 | 44.93 | 2-Jan-17 | Jan-17 | Week1 |
| WCL | 1075 | 44.51 | 3-Jan-17 | Jan-17 | Week1 |
| WCL | 1075 | 53.68 | 8-Jan-17 | Jan-17 | Week2 |
| WCL | 1075 | 41.45 | 9-Jan-17 | Jan-17 | Week2 |
| WCL | 1075 | 40.97 | 15-Jan-17 | Jan-17 | Week3 |
| WCL | 1075 | 55.9 | 16-Jan-17 | Jan-17 | Week3 |
| WCL | 1075 | 43.89 | 22-Jan-17 | Jan-17 | Week4 |
| WCL | 1075 | 44.36 | 23-Jan-17 | Jan-17 | Week4 |
| WCL | 1075 | 35.13 | 29-Jan-17 | Jan-17 | Week5 |
+---------------+------------+------------+-----------+--------+-------+
預期結果:
+--------+---------------+
| Week | Running total |
+--------+---------------+
| Week1 | 89.44 |
| Week2 | 184.57 |
| Week3 | 281.44 |
| Week4 | 369.69 |
| Week5 | 404.82 |
+--------+---------------+
請添加您的表格結構,一些測試數據和您想要的輸出 – iamdave