如何在case语句中编写select语句

时间:2013-10-01 07:33:04

标签: sql-server-2008 case temp-tables

让我知道如何在case语句中编写select查询。

select
ROW_NUMBER() OVER(Order by vendor.VendorName ) AS ID,
PH.PurchasingHeaderID as BILLNo,
PH.TotalPriceCompanyCurrency as Balance,
acc.AccountName as [Account_Name]  
**Into #tempOpenVedorlist** 
from PurchasingHeader PH
LEFT OUTER JOIN TransactionType Trans ON PH.TransactionTypeID =Trans.TransactionTypeID
LEFT OUTER JOIN Vendor vendor on PH.VendorID=vendor.VendorID
LEFT OUTER JOIN PaymentTerm PT on PT.PaymentTermID = vendor.PaymentTermID
LEFT OUTER JOIN PurchasingDetail PD on PD.PurchasingHeaderID = PH.PurchasingHeaderI
LEFT OUTER JOIN Account Acc on Acc.AccountID= PD.FinancialAccountID 
where PH.TransactionTypeID=7
Group by vendor.VendorName,
PH.PurchasingHeaderID,PH.TotalPriceCompanyCurrency,acc.AccountName

我得出这个结果:

有了这个结果:这里我有No:VB1003两次,但帐号名称不同。

ID      BILLNo          Account_Name            Balance   
-------------------------------------------------------------
 101      VB1000        Cash-Petty Cash         4000.00  
 102     VB1001        Accounts Receivable      5000.00 
 103    VB1003       Cash-PettyCash             6000.00 
 104     VB1003       Cash                      6000.00  
 105    VB1004        UndepositedFunds          7000.00  

我必须在这里展示;

我需要这个结果:

ID      BILLNo        Account_Name          Balance    
------------------------------------------------------
 101      VB1000      Cash-PettyCash        4000.00  
 102      VB1001      AccountsReceivable     5000.00  
 103     VB1003      ---Multiple----        6000.00  
 104     VB1004      UndepositedFunds      7000.00   

对于aboue结果我做了什么:我已经获取了临时表中的所有数据。

能够显示多个字符串,其中包含多个编号

但不幸的是,我无法显示只有一个BILLNo的帐户名称。

select ROW_NUMBER() OVER(Order by BILLNo ) AS ID,
[BILLNo],
Balance,
 CASE
 WHEN count(BILLNo)>1 THEN 'Multipul'
 WHEN count(BILLNo)<2 THEN **(Select Account_Name from #tempOpenVedorlist )** 
 End As [Financial_Account] 
from #tempOpenVedorlist
Group By BILLNo,Balance

让我知道如何从临时表中获取与该BILLNo相关的帐户名称。

1 个答案:

答案 0 :(得分:2)

AccountName中删除GROUP BY并在其上加上汇总。

SELECT  ROW_NUMBER() OVER(Order by vendor.VendorName ) AS ID,
        PH.PurchasingHeaderID as BILLNo,
        PH.TotalPriceCompanyCurrency as Balance,
        CASE WHEN MIN(acc.AccountName) IS NULL
             THEN '----'
             WHEN MIN(acc.AccountName) = MAX(acc.AccountName)
             THEN MIN(acc.AccountName) 
             ELSE '--MULTIPLE--' 
        END  as [Account_Name]  
INTO    #tempOpenVedorlist
FROM    PurchasingHeader PH
        LEFT OUTER JOIN TransactionType Trans 
            ON PH.TransactionTypeID =Trans.TransactionTypeID
        LEFT OUTER JOIN Vendor vendor 
            ON PH.VendorID=vendor.VendorID
        LEFT OUTER JOIN PaymentTerm PT 
            ON PT.PaymentTermID = vendor.PaymentTermID
        LEFT OUTER JOIN PurchasingDetail PD 
            ON PD.PurchasingHeaderID = PH.PurchasingHeaderI
        LEFT OUTER JOIN Account Acc 
            ON Acc.AccountID= PD.FinancialAccountID 
WHERE   PH.TransactionTypeID=7
GROUP BY  vendor.VendorName,
        PH.PurchasingHeaderID,
        PH.TotalPriceCompanyCurrency