Null Rows'由于通过分组连接表而存在

时间:2012-06-06 07:26:20

标签: sql sql-server-2005

我正在努力应对Group ByNull,如下所示。

我有两张表,如下面

Table : 1 accountmast       
companyID   accname             category    
102         PURCHASE ACCOUNT    Purchase Account    
102         LOCAL PURCHASE      Purchase Account    
102         SALES ACCOUNT       Sales Account   

Table: 2 ledger     
companyID   name                debit   credit
102         PURCHASE ACCOUNT    4742.3  
102         LOCAL PURCHASE      51106   
102         SALES ACCOUNT               8010
102         SALES ACCOUNT               4330000
102         PURCHASE ACCOUNT    5480000 

我问过如下:

select  
case    
when a.catagory ='Purchase Account' then    
l.name  
end as PurchaseAccount, 

case    
when a.catagory ='Purchase Account' then    
sum(coalesce(l.debit,0))-sum(coalesce(l.credit,0) ) 
end as PurAmt,  

case    
when a.catagory = 'Sales Account' then  
l.name      
end as SalesAccount,    
case    
when a.catagory = 'Sales Account' then  
sum(coalesce(l.credit,0))-sum(coalesce(l.debit,0) ) 
end as SalesAmt 

from ledger l join accountmast a    
on l.companyID=a.companyID  
and l.name = a.accname  
where l.companyID=102   
and a.catagory IN('Purchase Account','Sales Account')   
group by l.name,a.catagory  

结果是:

Purchase Account    PurAmt          Sales Account   SalesAmt
LOCAL PURCHASE      51106.00         NULL           NULL
PURCHASE ACCOUNT    5484742.30       NULL           NULL
NULL                NULL            SALES ACCOUNT  4338010.00

要求是:

Purchase Account    PurAmt           Sales Account   SalesAmt
LOCAL PURCHASE      51106.00         SALES ACCOUNT   4338010.00
PURCHASE ACCOUNT    5484742.30      

解决方案是什么?。

如果我使用Group By,那么它允许Null值关联表的列。

如果我使用MAXMIN,则会显示单个记录。我该怎么办?

如果有人有更好的解决方案,请建议。

2 个答案:

答案 0 :(得分:2)

WITH    q AS
        (
        SELECT  am.category, l.name,
                CASE am.category WHEN 'Purchase Account' THEN amt ELSE -amt END
        FROM    accountmast am
        CROSS APPLY
                (
                SELECT  name, SUM(COALESCE(credit, 0) - COALESCE(debit, 0)) AS amt
                FROM    ledger l
                WHERE   l.name = am.accname
                        AND l.companyId = am.companyId
                GROUP BY
                        name
                ) l
        WHERE   am.companyId = 102
        )
SELECT  l1.name, l1.amt, l2.name, l2.amt
FROM    (
        SELECT  *, ROW_NUMBER() OVER (ORDER BY name) AS rn
        FROM    q
        WHERE   category = 'Purchase account'
        ) l1
FULL JOIN
        (
        SELECT  *, ROW_NUMBER() OVER (ORDER BY name) AS rn
        FROM    q
        WHERE   category = 'Sales account'
        ) l2
ON      l2.rn = l1.rn

答案 1 :(得分:0)

注意:这不是答案这是Quassnoi考虑的参考。

这里我做了一些修改供你考虑。

WITH    q AS
(
SELECT  am.catagory, l.name,
CASE am.catagory WHEN 'Purchase Account' THEN Amount ELSE -Amount     END as Amount -- Here I provide the column name and alter the Column
FROM    accountmast am
    CROSS APPLY
    (
    SELECT  name, SUM(COALESCE(debit, 0) - COALESCE(credit, 0)) AS Amount --Reverse the sum function which is oppose of yours. And your's shows the value in minus.
    FROM    ledger l
    WHERE   l.name = am.accname
    AND l.companyId = am.companyId
    GROUP BY
             name
    ) l
    WHERE   am.companyId = 102
)
SELECT  l1.name as PurACC, l1.Amount as PurAmt, l2.name as SalACC, l2.Amount as SalAmt -- alter the columns for Binding Purpose.
FROM
    (
        SELECT  *, ROW_NUMBER() OVER (ORDER BY name) AS rn
        FROM    q
        WHERE   catagory = 'Purchase account'
    ) l1
FULL JOIN
    (
        SELECT  *, ROW_NUMBER() OVER (ORDER BY name) AS rn
        FROM    q
        WHERE   catagory = 'Sales account'
    ) l2
ON l2.rn = l1.rn

以上更改很重要,否则您的答案仍然不正确,并会检查错误。