使用t-sql中的select语句改进查询以连接表

时间:2014-01-30 04:14:38

标签: sql sql-server tsql

如果有人可以帮我解决问题,我会很感激:

我的第一个问题:

SELECT 
    1 as num, 'TITLE1' as tt, [DATE], CURRENCY, 
    SUM(AMOUNT) as assets, NULL as liabilities
FROM 
    [MYTABLE] T1  
WHERE 
    GROUP_ID IN (1700) AND [DATE] = '2014-01-20' 
GROUP BY 
    CURRENCY, [DATE]

这给了我:

num   tt        DATE        CURRENCY    assets     liabilities
1   TITLE1  2014-01-20      USD         1111        NULL

第二个查询是:

SELECT 
    1, 'TITLE1', [DATE], CURRENCY, NULL as assets,
    SUM(AMOUNT) as liabilities 
FROM 
    [MYTABLE] T2 
WHERE 
    GROUP_ID IN (2700, 2770)  
    AND [DATE] = '2014-01-20' 
GROUP BY 
    CURRENCY, [DATE]

这给了我:

num      tt         DATE       CURRENCY        assets      liabilities
1       TITLE1      2014-01-20   EUR            NULL        22222

我想加入这两个查询,结果将是:

num      tt         DATE        CURRENCY        assets      liabilities
1       TITLE1      2014-01-20   EUR            NULL        22222
1       TITLE1      2014-01-20   USD            1111        NULL

CURRENCY不同时就是这种情况。但是当CURRENCY相同时,结果应该是:

   num      tt         DATE        CURRENCY        assets      liabilities
    1       TITLE1      2014-01-20   EUR            3333        22222
    1       TITLE1      2014-01-20   USD            1111        44444

逻辑是,我计算AssetsliabilitiesCURRENCYDATE分组来自一个表但位于不同的GROUD_ID s。试图使用子查询,但只获得一个条目(如果assetsliabilities的货币不同):

SELECT 
    1, 'TITLE1', [DATE], CURRENCY, SUM(AMOUNT) as assets,
   (SELECT SUM(AMOUNT) as liabilities 
    FROM [MYTABLE] T2 
    WHERE (GROUP_ID IN (2700, 2770)) 
      AND [DATE] = '2014-01-20' AND T1.CURRENCY = T2.CURRENCY 
    GROUP BY CURRENCY, [DATE])  
FROM 
    [MYTABLE] T1  
WHERE 
    GROUP_ID IN (1700) AND [DATE] = '2014-01-20' 
GROUP BY 
    CURRENCY, [DATE]

我相信我可以通过左连接处理这个问题。我很感激任何帮助。

3 个答案:

答案 0 :(得分:1)

declare @MTable table ( Date date, Currency char(3), Group_ID int, amount int)
insert @Mtable 
values 
('20140120', 'USD',1700,1111),
('20140120', 'USD',2700,1111),
('20140120', 'EUR',2770,1111)

declare @Assets table ( ID int)
insert @Assets values (1700)
declare @Liabilities table ( ID int)
insert @Liabilities values (2700),(2770)

;

with RES as (
select [DATE], CURRENCY, 
       asset = sum(T1.Amount) over (PARTITION by [DATE], CURRENCY) , 
       liab = null
FROM @MTABLE T1 
join @Assets T2 on T2.ID = T1.Group_ID 
union 
select [DATE], CURRENCY, 
        null,  
        sum(T1.Amount) over (PARTITION by [DATE], CURRENCY)
FROM @MTABLE T1 join @Liabilities T2
on  T2.ID = T1.Group_ID 
)

select distinct
num = 1, tt = 'TITLE', RES.Date, RES.Currency,  
assets = sum(RES.asset) over (PARTITION by [DATE], CURRENCY), 
liabilities = sum(RES.liab) over (PARTITION by [DATE], CURRENCY)
from RES



/*   Result :
num         tt    Date       Currency assets      liabilities
----------- ----- ---------- -------- ----------- -----------
1           TITLE 2014-01-20 EUR      NULL        1111
1           TITLE 2014-01-20 USD      1111        1111


*/

答案 1 :(得分:0)

SELECT 1 as num, 'TITLE1' as tt, [DATE], CURRENCY, SUM(AMOUNT) as assets, NULL as liabilities
FROM [MYTABLE] T1  WHERE GROUP_ID IN (1700) AND [DATE] = '2014-01-20' 
GROUP BY CURRENCY, [DATE]

UNION

SELECT 1, 'TITLE1', [DATE], CURRENCY, NULL as assets,
SUM(AMOUNT) as liabilities FROM [MYTABLE] T2 WHERE GROUP_ID IN (2700, 2770)  
AND [DATE] = '2014-01-20' GROUP BY CURRENCY, [DATE]

答案 2 :(得分:0)

试试这个:

SELECT  1 as num, 'TITLE1' as tt, [DATE], CURRENCY, 
        SUM(CASE WHEN GROUP_ID IN (1700) THEN AMOUNT END) as assets, 
        SUM(CASE WHEN GROUP_ID IN (2700, 2770) THEN AMOUNT END) as liabilities
FROM    [MYTABLE] T1  
WHERE   GROUP_ID IN (1700, 2700, 2770) 
AND     [DATE] = '2014-01-20' 
GROUP BY CURRENCY, [DATE]