2017-05-04 65 views
-1

我有一個需求,我需要在多個記錄中均勻分配一個值。下面是我的數據看起來像價值分攤的T-SQL查詢

Relationship Table 
------------------------------------------------------- 
CategoryID   SubCategoryID 
------------------------------------------------------- 
101    1 
101    2 
101    3 
102    4 
102    5  

Table 1 
------------------------------------------------------- 
BatchLine ID  CategoryID  Amount 
------------------------------------------------------- 
1     101    10 
2     102    100 
3     101    100 

Table 2 
------------------------------------------------------------------- 
LineID BatchLineID CategoryID SubCategoryID LineValue 
------------------------------------------------------------------- 
1   1    101   1    3.33 
2   1    101   2    3.33 
3   1    101   3    3.33 
4   2    102   4    66.66 
5   2    102   5    33.33 
6   3    101   1    33.33 
7   3    101   2    33.33 
8   3    101   3    33.33  

在「表2」中的「LineValue」是從「表1」上的「金額」字段進行的計算的結果,然後將最終值四捨五入到兩位小數點。在某些情況下,舍入過程會導致與「表格1」中的「金額」字段存在一些差異。

Difference 
------------------------------------------------------------------ 
BatchLineID CategoryID Amount SumofLineValue 
------------------------------------------------------------------ 
1    101   10  9.99 
2    102   100  99.99 
3    101   100  99.99 

爲了從「表1」匹配「金額」值,我們需要與類別ID組中添加「0.01」到每個「LineID」直到的「LineValue」等於它的總和。

還有一個條件,在'CategoryID'101的情況下'表2'中有兩組數據。我們將「0.01」添加到LineID 1,這將使總和與「表1」中的「金額」字段匹配。當我們需要分攤第二組時,需要以「線路ID」7而不是6開始。下面是我預期的數據。

Expected Output 
------------------------------------------------------------------- 
LineID BatchLineID CategoryID SubCategoryID LineValue 
------------------------------------------------------------------- 
1   1    101   1    3.33 + 0.01 
2   1    101   2    3.33 
3   1    101   3    3.33 
4   2    102   4    66.66 + 0.01 
5   2    102   5    33.33 
6   3    101   1    33.33 
7   3    101   2    33.33 + 0.01 
8   3    101   3    33.33 
+0

是否將LineValue定義爲十進制(x,2)? – McNets

+0

@McNets是,它被定義爲十進制(X,2) – Prakazz

+0

批量最多有3行嗎? – McNets

回答

0

只要沒有標誌哪些記錄必須更新,該解決方案就會更新每個組的第一條記錄。

with rn as 
(
    select t2.LineID, t1.BatchLineID, t1.CategoryID, t1.Amount, t2.SubCategoryID, 
      sum(t2.LineValue) over (partition by t2.BatchLineID, t2.CategoryID) as ActSum, 
      row_number() over (partition by t2.BatchLineID, t2.CategoryID order by SubCategoryID) as RNum 
    from  t1 
    left join t2 
    on  t1.BatchLineID = t2.BatchLineID 
    and  t1.CategoryID = t2.CategoryID 
) 
update  t2 
set  LineValue = LineValue + (Amount - ActSum) 
from  t2 tt2 
inner join rn 
on   tt2.LineID = rn.LineID 
where  RNum = 1 
and  Amount <> ActSum; 
GO 
select * from t2; 
GO 
 
LineID | BatchLineID | CategoryID | SubCategoryID | LineValue 
-----: | ----------: | ---------: | ------------: | :-------- 
    1 |   1 |  101 |    1 | 3.34  
    2 |   1 |  101 |    2 | 3.33  
    3 |   1 |  101 |    3 | 3.33  
    4 |   2 |  102 |    4 | 66.67  
    5 |   2 |  102 |    5 | 33.33  
    6 |   3 |  101 |    1 | 33.34  
    7 |   3 |  101 |    2 | 33.33  
    8 |   3 |  101 |    3 | 33.33  

dbfiddle here

+0

要求是不添加相同的' SubCategoryID'。對於'CategoryID'第二組的情況101 LineID 7應該添加'0.01'。這是真正的抓住這裏。 – Prakazz

+0

好吧,讓我想想解決方案 – McNets

0

適應的fiddl從McNets E,你可以使用dense_rank()以確定哪一行應該被更新:

with cte1 as (
    select t2.LineID, t1.BatchLineID, t1.CategoryID, t1.Amount, t2.SubCategoryID, 
      sum(t2.LineValue) over (partition by t2.BatchLineID, t2.CategoryID) as ActSum, 
      row_number() over (partition by t2.BatchLineID, t2.CategoryID order by SubCategoryID) as RNum, 
      dense_rank() over (partition by t1.CategoryID order by t1.BatchLineID) as drank 
    from  t1 
    left join t2 
    on  t1.BatchLineID = t2.BatchLineID 
    and  t1.CategoryID = t2.CategoryID 
) 
select a.LineID, a.BatchLineID, a.CategoryID, a.SubCategoryID, case when drank = rnum then a.LineValue + (b.Amount - b.ActSum) else a.LineValue end as LineValue 
from t2 a 
left join cte1 b 
    on a.LineID = b.LineID 

需要注意的是,你可以運行到隨後的羣體沒有足夠的行,因此沒有行被更新的問題。

+0

謝謝你的努力喬希。但不一定我們需要在第二個實例和第三個實例中添加第二行和第三行。如果我們停止在第一組中的第二個「SubCategoryID」分配,那麼它需要是動態的,然後我們需要在第二組中的第三個「SubCategoryID」開始分攤。此外,我們需要在記錄中均勻添加'0.01',而不是將差異添加到一條記錄。 – Prakazz

+0

也許你的問題需要這些細節呢? –