0

给定以下查询:

SELECT  C.AGENCY_NAME, B.PROJECT_NUMBER, B.PROJECT_NAME, D.CONTRACT_NO, 
            B.PROJECT_START_DATE,
            CASE WHEN E.CHECK_AMOUNT IS NOT NULL AND A.MONTH = 'JUL' THEN
                F.NAME + ' ' + CONVERT(VARCHAR(10), CHECK_DATE, 101) + ' ' + CASE WHEN CONVERT(VARCHAR(10), CHECK_NUMBER) IS NOT NULL THEN 'CHK# ' END 
                + CONVERT(VARCHAR(10), CHECK_NUMBER) ELSE /*START CODE IF PEOPLESOFT DATA DOESN'T EXIST*/
                (SELECT TOP 1 CASE WHEN STATUS = 1 THEN 'Approved-' WHEN STATUS = 3 THEN 'Hold-' ELSE 'Rejected-' END
                    + WORKFLOW_NAME + '-' + CONVERT(VARCHAR(10), DATE, 101) FROM FINANCE_WORKFLOW_DETAILS G, FINANCE_WORKFLOW H 
                    WHERE G.WORKKFLOW_ID_STATE = H.FINANCE_WORKFLOW_ID AND G.INVOICE_ID = A.INVOICE_NUMBER AND A.PROGRAM_ID = G.PROGRAM_ID
                    AND (A.MONTH = 'JUL') ORDER BY DATE DESC) END 'July',

            CASE WHEN E.CHECK_AMOUNT IS NOT NULL AND A.MONTH = 'AUG' THEN
                F.NAME + ' ' + CONVERT(VARCHAR(10), CHECK_DATE, 101) + ' ' + CASE WHEN CONVERT(VARCHAR(10), CHECK_NUMBER) IS NOT NULL THEN 'CHK# ' END 
                + CONVERT(VARCHAR(10), CHECK_NUMBER) ELSE /*START CODE IF PEOPLESOFT DATA DOESN'T EXIST*/
                (SELECT TOP 1 CASE WHEN STATUS = 1 THEN 'Approved-' WHEN STATUS = 3 THEN 'Hold-' ELSE 'Rejected-' END
                    + WORKFLOW_NAME + '-' + CONVERT(VARCHAR(10), DATE, 101) FROM FINANCE_WORKFLOW_DETAILS G, FINANCE_WORKFLOW H 
                    WHERE G.WORKKFLOW_ID_STATE = H.FINANCE_WORKFLOW_ID AND G.INVOICE_ID = A.INVOICE_NUMBER AND A.PROGRAM_ID = G.PROGRAM_ID
                    AND A.MONTH = 'AUG' ORDER BY DATE DESC) END 'August'

        FROM EXPENSES A
        INNER JOIN PROGRAMS B ON A.PROGRAM_ID = B.PROGRAM_ID
        INNER JOIN AGENCIES C ON B.AGENCY_ID = C.AGENCY_ID
        INNER JOIN CONTRACTS D ON B.CONTRACT_ID = D.CONTRACT_ID
        LEFT OUTER JOIN PEOPLE_SOFT_DATA E ON A.INVOICE_NUMBER = E.INVOICE_NUMBER_ID
        LEFT OUTER JOIN PEOPLE_SOFT_PAYMENT_STATUS_TYPE F ON E.STATUS = F.PEOPLE_SOFT_PAYMENT_STATUS_TYPE_ID

我得到以下结果:

AGENCY_NAME PROJECT_NUMBER PROJECT_NAME CONTRACT_NO PROJECT_START_DATE 七月 八月

Test Agency 1   proj54891   Test Project Name 1 C567HR8E    2012-05-23 00:00:00.000 NULL    NULL
Test Agency 1   proj54891   Test Project Name 1 C567HR8E    2012-05-23 00:00:00.000 NULL    Rejected- Grants-09/25/2013
Test Agency 1   proj54891   Test Project Name 1 C567HR8E    2012-05-23 00:00:00.000 Approved- Administrator-09/25/2013  NULL
Test Agency 1488    proj54891   Test Project Name 34    C5676542H9  2013-04-03 00:00:00.000 Approved-Agency-10/12/2013  NULL

如何更改它以获得以下结果:

AGENCY_NAME PROJECT_NUMBER  PROJECT_NAME    CONTRACT_NO PROJECT_START_DATE  July    August

Test Agency 1   proj54891   Test Project Name 1 C567HR8E    2012-05-23 00:00:00.000 Approved- Administrator-09/25/2013      Rejected- Grants-09/25/2013
Test Agency 1488    proj54891   Test Project Name 34    C5676542H9  2013-04-03 00:00:00.000 Approved-Agency-10/12/2013  NULL

这样类似的机构、项目编号和项目名称只在一行?我只需要将类似的项目组合成一行。

谢谢。

4

2 回答 2

0

您可以使用 GROUP BY 语句对结果进行分组。在这种情况下,我认为它会是这样的:

  GROUP BY(C.AGENCY_NAME, B.PROJECT_NUMBER)

如果你想选择一些聚合值,你可以使用它。例如某列的 SUM,或每组中行数的简单 COUNT。

否则,如果您不想聚合某些内容,而只想知道每个不同的条目,那么您将使用 SELECT DISTINCT。所以这将是这样的:

  SELECT DISTINCT(C.AGENCY_NAME, B.PROJECT_NUMBER)

所以,这有点取决于你想要做什么。

于 2013-10-20T08:35:51.430 回答
0

您可以使用 DISTINCT:

SELECT DISTINCT C.AGENCY_NAME, B.PROJECT_NUMBER, B.PROJECT_NAME, D.CONTRACT_NO, 
            B.PROJECT_START_DATE,
            CASE WHEN E.CHECK_AMOUNT IS NOT NULL AND A.MONTH = 'JUL' THEN
                F.NAME + ' ' + CONVERT(VARCHAR(10), CHECK_DATE, 101) + ' ' + CASE WHEN CONVERT(VARCHAR(10), CHECK_NUMBER) IS NOT NULL THEN 'CHK# ' END 
                + CONVERT(VARCHAR(10), CHECK_NUMBER) ELSE /*START CODE IF PEOPLESOFT DATA DOESN'T EXIST*/
                (SELECT TOP 1 CASE WHEN STATUS = 1 THEN 'Approved-' WHEN STATUS = 3 THEN 'Hold-' ELSE 'Rejected-' END
                    + WORKFLOW_NAME + '-' + CONVERT(VARCHAR(10), DATE, 101) FROM FINANCE_WORKFLOW_DETAILS G, FINANCE_WORKFLOW H 
                    WHERE G.WORKKFLOW_ID_STATE = H.FINANCE_WORKFLOW_ID AND G.INVOICE_ID = A.INVOICE_NUMBER AND A.PROGRAM_ID = G.PROGRAM_ID
                    AND (A.MONTH = 'JUL') ORDER BY DATE DESC) END 'July',

            CASE WHEN E.CHECK_AMOUNT IS NOT NULL AND A.MONTH = 'AUG' THEN
                F.NAME + ' ' + CONVERT(VARCHAR(10), CHECK_DATE, 101) + ' ' + CASE WHEN CONVERT(VARCHAR(10), CHECK_NUMBER) IS NOT NULL THEN 'CHK# ' END 
                + CONVERT(VARCHAR(10), CHECK_NUMBER) ELSE /*START CODE IF PEOPLESOFT DATA DOESN'T EXIST*/
                (SELECT TOP 1 CASE WHEN STATUS = 1 THEN 'Approved-' WHEN STATUS = 3 THEN 'Hold-' ELSE 'Rejected-' END
                    + WORKFLOW_NAME + '-' + CONVERT(VARCHAR(10), DATE, 101) FROM FINANCE_WORKFLOW_DETAILS G, FINANCE_WORKFLOW H 
                    WHERE G.WORKKFLOW_ID_STATE = H.FINANCE_WORKFLOW_ID AND G.INVOICE_ID = A.INVOICE_NUMBER AND A.PROGRAM_ID = G.PROGRAM_ID
                    AND A.MONTH = 'AUG' ORDER BY DATE DESC) END 'August'

        FROM EXPENSES A
        INNER JOIN PROGRAMS B ON A.PROGRAM_ID = B.PROGRAM_ID
        INNER JOIN AGENCIES C ON B.AGENCY_ID = C.AGENCY_ID
        INNER JOIN CONTRACTS D ON B.CONTRACT_ID = D.CONTRACT_ID
        LEFT OUTER JOIN PEOPLE_SOFT_DATA E ON A.INVOICE_NUMBER = E.INVOICE_NUMBER_ID
        LEFT OUTER JOIN PEOPLE_SOFT_PAYMENT_STATUS_TYPE F ON E.STATUS = F.PEOPLE_SOFT_PAYMENT_STATUS_TYPE_ID

或使用 GROUP BY

SELECT AGENCY_NAME, PROJECT_NUMBER, PROJECT_NAME, CONTRACT_NO, PROJECT_START_DATE, July, August
(
    SELECT C.AGENCY_NAME, B.PROJECT_NUMBER, B.PROJECT_NAME, D.CONTRACT_NO, 
                B.PROJECT_START_DATE,
                CASE WHEN E.CHECK_AMOUNT IS NOT NULL AND A.MONTH = 'JUL' THEN
                    F.NAME + ' ' + CONVERT(VARCHAR(10), CHECK_DATE, 101) + ' ' + CASE WHEN CONVERT(VARCHAR(10), CHECK_NUMBER) IS NOT NULL THEN 'CHK# ' END 
                    + CONVERT(VARCHAR(10), CHECK_NUMBER) ELSE /*START CODE IF PEOPLESOFT DATA DOESN'T EXIST*/
                    (SELECT TOP 1 CASE WHEN STATUS = 1 THEN 'Approved-' WHEN STATUS = 3 THEN 'Hold-' ELSE 'Rejected-' END
                        + WORKFLOW_NAME + '-' + CONVERT(VARCHAR(10), DATE, 101) FROM FINANCE_WORKFLOW_DETAILS G, FINANCE_WORKFLOW H 
                        WHERE G.WORKKFLOW_ID_STATE = H.FINANCE_WORKFLOW_ID AND G.INVOICE_ID = A.INVOICE_NUMBER AND A.PROGRAM_ID = G.PROGRAM_ID
                        AND (A.MONTH = 'JUL') ORDER BY DATE DESC) END 'July',

                CASE WHEN E.CHECK_AMOUNT IS NOT NULL AND A.MONTH = 'AUG' THEN
                    F.NAME + ' ' + CONVERT(VARCHAR(10), CHECK_DATE, 101) + ' ' + CASE WHEN CONVERT(VARCHAR(10), CHECK_NUMBER) IS NOT NULL THEN 'CHK# ' END 
                    + CONVERT(VARCHAR(10), CHECK_NUMBER) ELSE /*START CODE IF PEOPLESOFT DATA DOESN'T EXIST*/
                    (SELECT TOP 1 CASE WHEN STATUS = 1 THEN 'Approved-' WHEN STATUS = 3 THEN 'Hold-' ELSE 'Rejected-' END
                        + WORKFLOW_NAME + '-' + CONVERT(VARCHAR(10), DATE, 101) FROM FINANCE_WORKFLOW_DETAILS G, FINANCE_WORKFLOW H 
                        WHERE G.WORKKFLOW_ID_STATE = H.FINANCE_WORKFLOW_ID AND G.INVOICE_ID = A.INVOICE_NUMBER AND A.PROGRAM_ID = G.PROGRAM_ID
                        AND A.MONTH = 'AUG' ORDER BY DATE DESC) END 'August'

            FROM EXPENSES A
            INNER JOIN PROGRAMS B ON A.PROGRAM_ID = B.PROGRAM_ID
            INNER JOIN AGENCIES C ON B.AGENCY_ID = C.AGENCY_ID
            INNER JOIN CONTRACTS D ON B.CONTRACT_ID = D.CONTRACT_ID
            LEFT OUTER JOIN PEOPLE_SOFT_DATA E ON A.INVOICE_NUMBER = E.INVOICE_NUMBER_ID
            LEFT OUTER JOIN PEOPLE_SOFT_PAYMENT_STATUS_TYPE F ON E.STATUS = F.PEOPLE_SOFT_PAYMENT_STATUS_TYPE_ID
) a
GROUP BY AGENCY_NAME, PROJECT_NUMBER, PROJECT_NAME, CONTRACT_NO, PROJECT_START_DATE, July, August

http://www.learn-with-video-tutorials.com - IT 视频教程

于 2013-10-20T11:16:54.927 回答