在多个表上加入和取消透视
Join and unpivot on multiple tables
我有两个 table:
更正table:
C_CORRECTIONS
CorrAmount |HalfYear1 |HalfYear2 |Type |REFNUM |
-----------------------------------------------------
1000 |50 |50 |HYINT |R123 |
已收集Table:
P_COLLECTED
Amount |Type |Account |REFNUM |
-----------------------------------------
1500 |TOTAL |ACCT1 |R123 |
50 |HYINT |ACCT2 |R123 |
50 |HYINT |ACCT3 |R123 |
我需要的是:
CorrAmount |Type |Account |REFNUM |
-----------------------------------------
1000 |TOTAL |ACCT1 |R123 |
50 |HYINT |ACCT2 |R123 |
50 |HYINT |ACCT3 |R123 |
我试过以下:
SELECT DISTINCT C.AMT, P.ACCOUNT,C.REFNUM FROM (
SELECT U.AMT, U.Type, REFNUM
FROM C_CORRECTIONS
UNPIVOT
(
AMT
FOR INT_AMT IN (CorrAmount,HalfYear1,HalfYear2)
) U
) C
LEFT JOIN P_COLLECTED P
ON C.REFNUM = P.REFNUM AND C.AMT = P.Amount;
结果:
AMT |ACCOUNT |REFNUM |
--------|-----------|-------|
50 |ACCT2 |R123 |
50 |ACCT3 |R123 |
1000 | |R123 |
编辑:好的。所以我使用了 left join 而不是 join,并输入了 distinct 并且我得到了主要金额,但我似乎还不能得到它的帐户。
您好,我已经在您的脚本中进行了修改,请验证
Select Amount,
Type,
'Acct' + CAST(ROW_NUMBER()OVER(PARTITION BY Refnum ORDER BY (Select NULL))AS VARCHAR(20))Account,REFNUM
from
(Select Amount, Type, REFNUM from @Table2 where type = 'Total'
UNION ALL
SELECT C.AMT, C.Type,C.REFNUM FROM (
SELECT U.AMT, U.Type, REFNUM
FROM @Table1
UNPIVOT
(
AMT
FOR INT_AMT IN (CorrAmount,HalfYear1,HalfYear2)
) U
) C,@Table2 P
WHERE C.REFNUM = P.REFNUM OR C.AMT = P.Amount
GROUP BY C.AMT, C.Type,C.REFNUM)T
您取消旋转 C_CORRECTIONS,然后加入 P_COLLECTED。
但也许您可以从 P_COLLECTED 开始并加入 C_CORRECTIONS。
由于 P_COLLECTED 已经有多行。
测试示例:
declare @P_COLLECTED table (Amount int, Type varchar(8), Account varchar(8), REFNUM varchar(8));
declare @C_CORRECTIONS table (CorrAmount int, HalfYear1 int, HalfYear2 int, Type varchar(8), REFNUM varchar(8));
insert into @P_COLLECTED (Amount, Type, Account, REFNUM) values
(1500,'TOTAL','ACCT1','R123'),
(50 ,'HYINT','ACCT2','R123'),
(50 ,'HYINT','ACCT3','R123');
insert into @C_CORRECTIONS (CorrAmount, HalfYear1, HalfYear2, Type, REFNUM) values
(1000,24,26,'HYINT','R123');
select
(case when t.Type = 'TOTAL' then c.CorrAmount else t.Amount end) as Amount,
t.Type, t.Account, t.REFNUM
-- , c.*
from @P_COLLECTED t
left join @C_CORRECTIONS c
-- on (t.REFNUM = c.REFNUM and t.Type in ('TOTAL',c.Type));
on (t.REFNUM = c.REFNUM and t.Type = 'TOTAL');
我有两个 table:
更正table:
C_CORRECTIONS
CorrAmount |HalfYear1 |HalfYear2 |Type |REFNUM |
-----------------------------------------------------
1000 |50 |50 |HYINT |R123 |
已收集Table:
P_COLLECTED
Amount |Type |Account |REFNUM |
-----------------------------------------
1500 |TOTAL |ACCT1 |R123 |
50 |HYINT |ACCT2 |R123 |
50 |HYINT |ACCT3 |R123 |
我需要的是:
CorrAmount |Type |Account |REFNUM |
-----------------------------------------
1000 |TOTAL |ACCT1 |R123 |
50 |HYINT |ACCT2 |R123 |
50 |HYINT |ACCT3 |R123 |
我试过以下:
SELECT DISTINCT C.AMT, P.ACCOUNT,C.REFNUM FROM (
SELECT U.AMT, U.Type, REFNUM
FROM C_CORRECTIONS
UNPIVOT
(
AMT
FOR INT_AMT IN (CorrAmount,HalfYear1,HalfYear2)
) U
) C
LEFT JOIN P_COLLECTED P
ON C.REFNUM = P.REFNUM AND C.AMT = P.Amount;
结果:
AMT |ACCOUNT |REFNUM |
--------|-----------|-------|
50 |ACCT2 |R123 |
50 |ACCT3 |R123 |
1000 | |R123 |
编辑:好的。所以我使用了 left join 而不是 join,并输入了 distinct 并且我得到了主要金额,但我似乎还不能得到它的帐户。
您好,我已经在您的脚本中进行了修改,请验证
Select Amount,
Type,
'Acct' + CAST(ROW_NUMBER()OVER(PARTITION BY Refnum ORDER BY (Select NULL))AS VARCHAR(20))Account,REFNUM
from
(Select Amount, Type, REFNUM from @Table2 where type = 'Total'
UNION ALL
SELECT C.AMT, C.Type,C.REFNUM FROM (
SELECT U.AMT, U.Type, REFNUM
FROM @Table1
UNPIVOT
(
AMT
FOR INT_AMT IN (CorrAmount,HalfYear1,HalfYear2)
) U
) C,@Table2 P
WHERE C.REFNUM = P.REFNUM OR C.AMT = P.Amount
GROUP BY C.AMT, C.Type,C.REFNUM)T
您取消旋转 C_CORRECTIONS,然后加入 P_COLLECTED。
但也许您可以从 P_COLLECTED 开始并加入 C_CORRECTIONS。
由于 P_COLLECTED 已经有多行。
测试示例:
declare @P_COLLECTED table (Amount int, Type varchar(8), Account varchar(8), REFNUM varchar(8));
declare @C_CORRECTIONS table (CorrAmount int, HalfYear1 int, HalfYear2 int, Type varchar(8), REFNUM varchar(8));
insert into @P_COLLECTED (Amount, Type, Account, REFNUM) values
(1500,'TOTAL','ACCT1','R123'),
(50 ,'HYINT','ACCT2','R123'),
(50 ,'HYINT','ACCT3','R123');
insert into @C_CORRECTIONS (CorrAmount, HalfYear1, HalfYear2, Type, REFNUM) values
(1000,24,26,'HYINT','R123');
select
(case when t.Type = 'TOTAL' then c.CorrAmount else t.Amount end) as Amount,
t.Type, t.Account, t.REFNUM
-- , c.*
from @P_COLLECTED t
left join @C_CORRECTIONS c
-- on (t.REFNUM = c.REFNUM and t.Type in ('TOTAL',c.Type));
on (t.REFNUM = c.REFNUM and t.Type = 'TOTAL');