是否可以透视此查询?

时间:2018-12-17 20:50:47

标签: sql sql-server vb.net

我的查询

SELECT DISTINCT dbo.tblacc_cv.cv_docno, 
                dbo.tblacc_cv.cv_docdate, 
                dbo.tblacc_cv.cv_suppliername, 
                dbo.tblacc_cv.cv_extdocno, 
                dbo.tblacc_chartaccount.coa_cvtype AS cvtype, 
                dbo.tblacc_glentry.gl_accountid, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid = '201001' THEN Sum( 
                    dbo.tblacc_glentry.gl_credit) 
                    ELSE NULL 
                  END )                            AS AP, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid = '208000' THEN Sum( 
                    dbo.tblacc_glentry.gl_amount) 
                    ELSE NULL 
                  END )                            AS EWT, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid = '616000' 
                         AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum( 
                    dbo.tblacc_glentry.gl_debit) 
                    ELSE NULL 
                  END )                            AS FABpur, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid = '603000' 
                         AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum( 
                    dbo.tblacc_glentry.gl_debit) 
                    ELSE NULL 
                  END )                            AS FABcre, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid = '602000' 
                         AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum( 
                    dbo.tblacc_glentry.gl_debit) 
                    ELSE NULL 
                  END )                            AS RawMat, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid = '601003' 
                         AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum( 
                    dbo.tblacc_glentry.gl_debit) 
                    ELSE NULL 
                  END )                            AS PurCre, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid NOT IN ( 
                         '201001', '208000', '616000', '603000', 
                         '602000', '601003' ) THEN 
                    dbo.tblacc_glentry.gl_accountname 
                    ELSE NULL 
                  END )                            AS GL_AccountName, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid NOT IN ( 
                         '616000', '603000', '602000', '601003' 
                              ) 
                         AND dbo.tblacc_chartaccount.coa_cvtype = 'M' THEN Sum( 
                    dbo.tblacc_glentry.gl_debit) 
                    ELSE NULL 
                  END )                            AS MOdr, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid NOT IN ( 
                         '616000', '603000', '602000', '601003' 
                              ) 
                         AND dbo.tblacc_chartaccount.coa_cvtype = 'F' THEN Sum( 
                    dbo.tblacc_glentry.gl_debit) 
                    ELSE NULL 
                  END )                            AS FDR, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid NOT IN ( 
                         '201001', '208000', '616000', '603000', 
                         '602000', '601003' ) 
                         AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum( 
                    dbo.tblacc_glentry.gl_debit) 
                    ELSE NULL 
                  END )                            AS SunDR, 
                ( CASE 
                    WHEN dbo.tblacc_glentry.gl_accountid NOT IN ( 
                         '201001', '208000', '616000', '603000', 
                         '602000', '601003' ) 
                         AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum( 
                    dbo.tblacc_glentry.gl_credit) 
                    ELSE NULL 
                  END )                            AS SunCR 
FROM   dbo.tblacc_glentry 
       INNER JOIN dbo.tblacc_apv 
               ON dbo.tblacc_glentry.gl_docno = dbo.tblacc_apv.apv_docno 
       INNER JOIN dbo.tblacc_cv 
                  INNER JOIN dbo.tblacc_cvdetail 
                          ON dbo.tblacc_cv.cv_docno = 
                             dbo.tblacc_cvdetail.cvd_cv_docno 
               ON dbo.tblacc_apv.apv_docno = dbo.tblacc_cvdetail.cvd_refdocno 
       INNER JOIN dbo.tblacc_chartaccount 
               ON dbo.tblacc_glentry.gl_accountid = 
                  dbo.tblacc_chartaccount.coa_account_id 
WHERE  ( dbo.tblacc_cv.cv_docdate BETWEEN 
         '01/01/2018 00:00:00' AND '1/30/2018 23:59:59' ) 
GROUP  BY dbo.tblacc_cv.cv_docno, 
          dbo.tblacc_cv.cv_docdate, 
          dbo.tblacc_cv.cv_suppliername, 
          dbo.tblacc_glentry.gl_accountname, 
          dbo.tblacc_cv.cv_extdocno, 
          dbo.tblacc_chartaccount.coa_cvtype, 
          dbo.tblacc_glentry.gl_accountid, 
          dbo.tblacc_glentry.gl_credit, 
          dbo.tblacc_glentry.gl_amount, 
          dbo.tblacc_glentry.gl_debit 
ORDER  BY dbo.tblacc_cv.cv_docdate ASC 

样本输出

Sample Output

希望的食物

Desired Output

1 个答案:

答案 0 :(得分:0)

您想要的输出不是PIVOT的示例。

对于您的逻辑没有详细的说明,我可以告诉您,您可以通过对前四列进行GROUPING,然后将MAX()应用于所有其他列来获得所需的输出。 (除非cvtype是MIN)。