Select SQL mapping query

2019-08-25 18:05发布

I use SQL Server 2012 and I have these tables:

Table Tb1 (ID Int, CodeID Int, Value Int)

 ID     CodeID    Value
 ----------------------
 1      1         10
 2      1         14
 3      1         5
 4      1         25
 5      2         12
 6      2         17
 7      2         4
 8      2         10
 9      2         6

Table Tb2 (CodeID Int Value Int)

CodeID    Value
---------------
1         25
2         20

I want a query for get records from Tb1 that SUM(Tb1.Value) <= Tb2.Value

For example result for above records is :

Tb1.ID   Tb1.CodeID  Tb1.Value   UsedValue     Tb2.Value
--------------------------------------------------------
1         1          10          10            25
2         1          14          14            25
3         1          5           1             25
5         2          12          12            20
6         2          17          8             20

I use cursor for get above query. But my query, execute with low performance. I want a query without cursor.

EDIT 1 UsedValue is Tb1.Value until sum of Tb1.Value was lowest than Tb2.Value.

3条回答
时光不老,我们不散
2楼-- · 2019-08-25 18:40

Try this:

;WITH Subtotals
AS
(
  SELECT
    T1.Id, T1.CodeId, SUM(T2.Value) SubTotal  
  FROM Tb1 T1
    JOIN Tb1 T2
       ON T1.CodeId = T2.CodeId
         AND T1.Id >= T2.Id
  GROUP BY T1.Id, T1.CodeId
)
SELECT
    S.ID,
    S.CodeID,
    T1.Value,
    CASE WHEN T2.value >= S.Subtotal
         THEN T1.value
         ELSE T1.value - (S.Subtotal - T2.value)
    END UsedValue,
    T2.Value T2Value
FROM Subtotals S
   JOIN Tb2 T2
      ON S.CodeId = T2.CodeId
   JOIN Tb1 T1
      ON S.Id = T1.Id
WHERE T2.Value >= S.SubTotal - T1.Value

See result on SQL FIDDLE

查看更多
手持菜刀,她持情操
3楼-- · 2019-08-25 18:50
select 
T.Id
, T.CodeId
, T.Value
, T.Value -(Total - T2.Value) as UsedValue
, T2.Value as T2_Value
from 
(
   select a.id
   , a.codeid
   , a.Value
   , sum(b.value) as Total
   from Tb1 a join Tb1 b 
   on a.Id >= b.id and a.CodeId = b.CodeId
   group by a.id, a.CodeId, a.Value
) T
join Tb2 T2 
on T.CodeId = T2.CodeId
where Total < (T2.Value + T.Value)

there is only one assumption when i wrote this code . that rows are ordered by ID, if it is not true you have to add ordering column !

查看更多
啃猪蹄的小仙女
4楼-- · 2019-08-25 19:00
select id,codeid,value,case when vt>0 then value else value+vt end usedvalue, 
       value2 
from (select *, value2-runtot vt,
       max(case when value2-runtot<0 then value2-runtot else -runtot end) 
       over(PARTITION BY codeid)  usedvalue 
           from (SELECT t1.id, t1.codeid, t1.value, t2.value value2, 
                sum(t1.value) OVER(PARTITION BY t1.codeid ORDER BY t1.id) runtot
              FROM tb1 t1
              INNER JOIN tb2 t2 ON t1.codeid = t2.codeid
              ) X
             )Y
where vt>=usedvalue
查看更多
登录 后发表回答