2017-06-05 5 views
1

私は "実行中の合計"コンセプトに基づいてSQLコードを実行しています。私のコードは正確な稼働合計を計算していますが、実行中の合計を日付ではなく毎週の完了後に表示する必要があります。
別週を表示するにはどうすればよいですか?

+---------------+------------+------------+-----------+--------+-------+ 
| 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 | 
+---------------+------------+------------+-----------+--------+-------+ 

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] 

期待される結果:

+--------+---------------+ 
| Week | Running total | 
+--------+---------------+ 
| Week1 | 89.44   | 
| Week2 | 184.57  | 
| Week3 | 281.44  | 
| Week4 | 369.69  | 
| Week5 | 404.82  | 
+--------+---------------+     
+1

テーブル構造、テストデータ、および希望する出力を追加してください – iamdave

答えて

0

あなたはこれが一列に売上高を合計します

Select DISTINCT SD1.[Week], 
    SD1.[Allocation], 
    SD1.[Subsidiaries], 
    SD1.[Month], 
    SD1.[Date], 
    SUM(Daily Sale) as 'Running Total' 
from [dbo].[Solar data] SD1 
where SD1.[Subsidiaries]='WCL' 
group by SD1.[Allocation], 
    SD1.[Month], 
    SD1.[Subsidiaries], 
    SD1.[Daily Sale], 
    SD1.[Week],SD1.[Date] 
order by SD1.[Week] 

試すことができます

関連する問題