2016-10-22 44 views
0

當我單獨執行這些查詢時,Running Total和Running Average出現正確,但聯合Running Total和Running Average沒有正確計算權限。聯合SQL中的兩個運行平衡

不能理解聯盟中發生了什麼。

SELECT BillDate,Location,Branch,Quantity,Amount,NoOfTransaction,BillYear,Item, 
    SUM(Amount) over(order by BillDate rows unbounded preceding) as RunningTotal, 
    AVG(Amount) over(order by BillDate rows unbounded preceding) as RunningAverage 
    FROM vwLocationWiseSales 
    WHERE YEAR(BillDate) = YEAR(GETDATE()) 

    UNION 

    SELECT BillDate,Location,Branch,Quantity,Amount,NoOfTransaction,BillYear,Item, 
    SUM(Amount) over(order by BillDate rows unbounded preceding) as RunningTotal, 
    AVG(Amount) over(order by BillDate rows unbounded preceding) as RunningAverage 
    FROM vwLocationWiseSales 
    WHERE YEAR(BillDate) = YEAR(DATEADD(YEAR,-1,GETDATE())) 

樣本數據

2015-01-01 00:00:00.000 CHICKEN           Bahadurabad - Floor        9.248         3782.71         4    2015  WHOLE CHICKEN                                                                      
2015-01-01 00:00:00.000 CHICKEN           Binori Town - Floor        5.560         2298.27         4    2015  WHOLE CHICKEN                                                                     
2015-01-01 00:00:00.000 CHICKEN           Boat Basin-Floor         1.634         678.11         2    2015  WHOLE CHICKEN                                                                     
2016-01-31 00:00:00.000 CHICKEN           Phase 2 Commercial-Floor       17.374         5994.03         17    2016  CHICKEN KARHAI CUT (18 PIECES)                                                               
2016-01-31 00:00:00.000 CHICKEN           SEHER - Floor          6.142         3249.12         7    2016  CHICKEN MINCE                                                                    
2016-01-31 00:00:00.000 CHICKEN           SEHER - Floor          0.006         2.02         1    2016  WHOLE CHICKEN 

所需的輸出

2015-01-01 00:00:00.000 CHICKEN           Bahadurabad - Floor        9.248         3782.71         4    2015  WHOLE CHICKEN                                                             3782.71         
2015-01-01 00:00:00.000 CHICKEN           Binori Town - Floor        5.560         2298.27         4    2015  WHOLE CHICKEN                                                             6080.98         
2015-01-01 00:00:00.000 CHICKEN           Boat Basin-Floor         1.634         678.11         2    2015  WHOLE CHICKEN                                                             6759.09         
2016-01-31 00:00:00.000 CHICKEN           Phase 2 Commercial-Floor       17.374         5994.03         17    2016  CHICKEN KARHAI CUT (18 PIECES)                                                        5994.03        
2016-01-31 00:00:00.000 CHICKEN           SEHER - Floor          6.142         3249.12         7    2016  CHICKEN MINCE                                                             9243.15        
2016-01-31 00:00:00.000 CHICKEN           SEHER - Floor          0.006         2.02         1    2016  WHOLE CHICKEN                                                             9245.17        

回答

0

嘗試使用一個查詢,而不是:

SELECT BillDate, Location, Branch, Quantity, Amount, NoOfTransaction, 
     BillYear,Item, 
     SUM(Amount) over(partition by YEAR(BillDate) 
         order by BillDate rows unbounded preceding) as RunningTotal, 
     AVG(Amount) over(partition by YEAR(BillDate) 
         order by BillDate rows unbounded preceding) as RunningAverage 
    FROM vwLocationWiseSales 
    WHERE YEAR(BillDate) IN (YEAR(DATEADD(YEAR,-1,GETDATE())), YEAR(GETDATE())) 
+0

附近有語法錯誤)',可以請你改正 –

+0

還是一樣的問題,運行平衡的第一年是計算正確,但第二年出現錯誤的運行平衡 –

+0

@ Mateen-ul-haq你可以發佈一些示例數據,以便我可以重現此問題嗎? –