Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

sql server - SQL - Case When on same row

I have a problem with a query that involves multiple case whens

     SELECT DISTINCT
            ,-1*(SUM(ISNULL(DB.[Commission_Bal],0)) + SUM(ISNULL(UP.AMOUNT,0))) AS [Commission_Bal]
            ,DATEDIFF(DAY,DB_CD.Create_Date,'20170123')  AS Days_In_Neg_Bal
            ,CASE WHEN DOC.DocumentTypeCode = 'MCS30' THEN DOC.DocumentDate END AS '30 Day Letter'
            ,CASE WHEN DOC.DocumentTypeCode = 'MCS60' THEN DOC.DocumentDate END AS '60 Day Letter'
            ,CASE WHEN DOC.DocumentTypeCode = 'MCS90' THEN DOC.DocumentDate END AS '90 Day Letter'
            ,CASE WHEN DOC.DocumentTypeCode = 'MCS20' THEN DOC.DocumentDate END AS '120 Day Letter'
            ,DB.[Cycle_Date]

It returns separate rows but i would like the result to have them on the same row. It returns the top image but i would like it to return results similar to the bottom image

query

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make it a conditional aggregation

SELECT  (-1*(SUM(ISNULL(DB.[Commission_Bal],0)) + SUM(ISNULL(UP.AMOUNT,0))))/count(*) AS [Commission_Bal]
       ,DATEDIFF(DAY,DB_CD.Create_Date,'20170123')  AS Days_In_Neg_Bal
       ,Max(CASE WHEN DOC.DocumentTypeCode = 'MCS30' THEN DOC.DocumentDate END) AS '30 Day Letter'
       ,Max(CASE WHEN DOC.DocumentTypeCode = 'MCS60' THEN DOC.DocumentDate END) AS '60 Day Letter'
       ,Max(CASE WHEN DOC.DocumentTypeCode = 'MCS90' THEN DOC.DocumentDate END) AS '90 Day Letter'
       ,Max(CASE WHEN DOC.DocumentTypeCode = 'MCS20' THEN DOC.DocumentDate END) AS '120 Day Letter'
       ,DB.[Cycle_Date]
 From  YourTable
 Group By -1*(SUM(ISNULL(DB.[Commission_Bal],0)) + SUM(ISNULL(UP.AMOUNT,0)))
         ,DATEDIFF(DAY,DB_CD.Create_Date,'20170123')
         ,DB.[Cycle_Date]

EDIT - Option 2:

Select [Commission_Bal]
      ,[Days_In_Neg_Bal]
      ,[30 Day Letter]  = max([30 Day Letter])
      ,[60 Day Letter]  = max([60 Day Letter])
      ,[60 Day Letter]  = max([90 Day Letter])
      ,[120 Day Letter] = max([120 Day Letter])
      ,[Cycle_Date]
 From (
         SELECT DISTINCT
                     -1*(SUM(ISNULL(DB.[Commission_Bal],0)) + SUM(ISNULL(UP.AMOUNT,0))) AS [Commission_Bal]
                    ,DATEDIFF(DAY,DB_CD.Create_Date,'20170123')  AS Days_In_Neg_Bal
                    ,CASE WHEN DOC.DocumentTypeCode = 'MCS30' THEN DOC.DocumentDate END AS '30 Day Letter'
                    ,CASE WHEN DOC.DocumentTypeCode = 'MCS60' THEN DOC.DocumentDate END AS '60 Day Letter'
                    ,CASE WHEN DOC.DocumentTypeCode = 'MCS90' THEN DOC.DocumentDate END AS '90 Day Letter'
                    ,CASE WHEN DOC.DocumentTypeCode = 'MCS20' THEN DOC.DocumentDate END AS '120 Day Letter'
                    ,DB.[Cycle_Date]
          From  YourTable
       ) A
 Group By [Commission_Bal]
         ,[Days_In_Neg_Bal]
         ,[Cycle_Date]

EDIT 3 - Full Final Query

Select [ProcessingCompany]
      ,[External_ID]
      ,[BrokerName]
      ,[AGENT_ADDRESS]
      ,[Commission_Bal]
      ,[Days_In_Neg_Bal]
      ,[30 Day Letter]  = max([30 Day Letter])
      ,[60 Day Letter]  = max([60 Day Letter])
      ,[60 Day Letter]  = max([90 Day Letter])
      ,[120 Day Letter] = max([120 Day Letter])
      ,[Cycle_Date]
 From (

    SELECT DISTINCT
           db.ProcessingCompany
          ,DB.[External_ID]
          ,DB.BrokerName
          ,DB.AGENT_ADDRESS
          ,-1*(SUM(ISNULL(DB.[Commission_Bal],0)) + SUM(ISNULL(UP.AMOUNT,0))) AS [Commission_Bal]
          ,DATEDIFF(DAY,DB_CD.Create_Date,'20170123')  AS Days_In_Neg_Bal
          ,CASE WHEN DOC.DocumentTypeCode = 'MCS30' THEN DOC.DocumentDate END AS '30 Day Letter'
          ,CASE WHEN DOC.DocumentTypeCode = 'MCS60' THEN DOC.DocumentDate END AS '60 Day Letter'
          ,CASE WHEN DOC.DocumentTypeCode = 'MCS90' THEN DOC.DocumentDate END AS '90 Day Letter'
          ,CASE WHEN DOC.DocumentTypeCode = 'MCS20' THEN DOC.DocumentDate END AS '120 Day Letter'
          ,DB.[Cycle_Date]
        FROM     
            debit_balance_final AS DB
        LEFT JOIN up_debit AS UP
            ON UP.GroupID = DB.[Processing_Group_Code] 
            AND UP.PAY_ENTITY_ID = DB.[External_ID]
        LEFT JOIN docs AS DOC
            ON DOC.RepMAN = DB.[External_ID]

    INNER JOIN 
        (
            SELECT DB2.EXTERNAL_ID,

                DB2.Processing_Group_Code ,
                MIN(DB2.Create_Date) AS CREATE_DATE

            FROM MCS_Debit_Balance DB2 WITH (NOLOCK)  
            WHERE DB2.CREATE_DATE  > ( SELECT ISNULL(MAX(CREATE_DATE),'1/1/1900') 
                                       FROM MCS_DEBIT_BALANCE MDB 
                                       WHERE MDB.EXTERNAL_ID = DB2.External_ID 
                                       AND DB2.Processing_Group_Code = MDB.Processing_Group_Code 
                                       AND MDB.Commission_Bal = 0)

            GROUP BY DB2.External_ID,DB2.Processing_Group_Code

        )  DB_CD 
                ON DB.External_ID = DB_CD.External_ID 
                AND DB.Processing_Group_Code = DB_CD .Processing_Group_Code  

    --      WHERE DB.[External_ID] = '1002937'

            GROUP BY

            db.ProcessingCompany
            ,DB.[Business_Unit_Ap]
            ,DB.[External_ID]
            ,DB.[Processing_Group_Code]
            ,DB.BrokerName
            ,DB.AGENT_ADDRESS
            ,CASE WHEN DOC.DocumentTypeCode = 'MCS30' THEN DOC.DocumentDate END
            ,CASE WHEN DOC.DocumentTypeCode = 'MCS60' THEN DOC.DocumentDate END
            ,CASE WHEN DOC.DocumentTypeCode = 'MCS90' THEN DOC.DocumentDate END
            ,CASE WHEN DOC.DocumentTypeCode = 'MCS20' THEN DOC.DocumentDate END
            ,DB.[Balance_Year]
            ,DATEDIFF(DAY,DB_CD.Create_Date,'20170123')
    --      ,DOC.DocumentTypeID
            ,DB.[Cycle_Date] 

) A
 Group By [ProcessingCompany]
         ,[External_ID]
         ,[BrokerName]
         ,[AGENT_ADDRESS]
         ,[Commission_Bal]
         ,[Days_In_Neg_Bal]
         ,[Cycle_Date]

ORDER BY DB.[External_ID]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...