简体   繁体   中英

how to write the select statement in case statement

Let me know how to write select query in case statement.

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

I GOT THIS RESULT :

with this result: Here i have No : VB1003 two times but Account Name is different.

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  

Here i have to show ;

I need this result :

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   

For aboue result what i have did : i have taken all the data in temp table.

Am able show Multiple string for which has more than one No.

But unfortunatly am not able show Account Name for which has only one 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

Let me know how can i get account name from temp table which is related to that BILLNo.

Remove the AccountName from the GROUP BY and put an aggregate on that.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM