1

我正在努力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那么它会显示单个记录。我需要做什么?

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

4

2 回答 2

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
于 2012-06-06T07:42:50.597 回答
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

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

于 2012-06-06T13:21:42.713 回答