2

我目前有一个问题,我需要在数据库中报告几个销售领域的价值。第二个 `SELECT 给了我我想知道的东西,第一部分也给了我我想知道的东西。但是我很好奇是否有可能使第一个查询的结果在第二个查询中显示为一行...

如果我只是添加@something到第二个,SELECT那么它会显示为一列并给出如下结果:

TOTAL     | CATEGORY    | (NO COLUMN NAME)
-------------------------------------------
70000.11  | CATEGORY 1  | 118500
31200.11  | CATEGORY 2  | 118500
51114.11  | CATEGORY 3  | 118500
2215.11   | CATEGORY 4  | 118500
54321.11  | CATEGORY 5  | 118500

但是,基本上我想弄清楚是否可以将其注入结果中,所以它看起来像:

TOTAL     | CATEGORY  
----------------------
70000.11  | CATEGORY 1
31200.11  | CATEGORY 2
51114.11  | CATEGORY 3
2215.11   | CATEGORY 4
54321.11  | CATEGORY 5
118500.00 | SOMETHING

我修改了列名等,所以我希望它不会造成混淆。我想过尝试生成一个临时表,将值拉入其中,然后在其上执行 INSERT INTO @myTable?我对此有点熟悉,但不确定它是否是最有效的方法。

最终,这将进入存储过程。

USE Database1
GO
DECLARE @something NUMERIC
SET @something = (SELECT 
        ABS(SUM(ENTRY_AMT)) AS 'Total'

    FROM 
        Invoices AS gl

        LEFT JOIN Orders AS iod ON gl.ORDER_NO = iod.ORDER_NO AND gl.LINE_NO = iod.LINE_NO AND gl.APPEND = iod.APPEND

    WHERE 
        gl.YR = 13 
        AND gl.PERIOD = 1 
        AND ENTRY_SRC = 'revenue' 
        AND iod.CATEGORY_DESC IN ('GENERAL', 'OTHER', 'RANDOM') 

        AND (gl.GL_ACCOUNT_NO BETWEEN 7100000000 AND 7100999999)
        AND gl.ITEM_NO NOT LIKE '%RS'
        AND gl.ITEM_NO NOT LIKE '%RA'
)


(SELECT 
    ABS(SUM(ENTRY_AMT)) AS 'Total', 
    CATEGORY_DESC AS 'Category'


FROM 
    Invoices AS gl

    LEFT JOIN Orders AS iod ON gl.ORDER_NO = iod.ORDER_NO AND gl.LINE_NO = iod.LINE_NO AND gl.APPEND = iod.APPEND

WHERE 
        gl.YR = 13 
    AND gl.PERIOD = 1 
    AND ENTRY_SRC = 'revenue' 
    AND iod.CATEGORY_DESC NOT IN ('GENERAL', 'OTHER', 'RANDOM') 

    AND (gl.GL_ACCOUNT_NO BETWEEN 7100000000 AND 7100999999)
    AND gl.ITEM_NO NOT LIKE '%RS'
    AND gl.ITEM_NO NOT LIKE '%RA'


GROUP BY CATEGORY_DESC
)   
4

2 回答 2

1

假设 @something 不是其余部分的总和或类似的东西,在这种情况下,您想研究像 @xQbert 建议的分组集和多维数据集。如果不是,那么试试这个。

<-- Your Query Here -->
UNION ALL -- Make sure it's a UNION ALL not a UNION or you will lose duplicate rows.
SELECT @Something AS Total, 'Something' AS Category
于 2013-02-21T23:20:57.277 回答
1
    USE Database1
    GO
(SELECT 
                ABS(SUM(ENTRY_AMT)) AS 'Total',
'All' as Category

        FROM 
            Invoices AS gl

            LEFT JOIN Orders AS iod ON gl.ORDER_NO = iod.ORDER_NO AND gl.LINE_NO = iod.LINE_NO AND gl.APPEND = iod.APPEND

        WHERE 
            gl.YR = 13 
            AND gl.PERIOD = 1 
            AND ENTRY_SRC = 'revenue' 
            AND iod.CATEGORY_DESC IN ('GENERAL', 'OTHER', 'RANDOM') 

            AND (gl.GL_ACCOUNT_NO BETWEEN 7100000000 AND 7100999999)
            AND gl.ITEM_NO NOT LIKE '%RS'
            AND gl.ITEM_NO NOT LIKE '%RA'
    )
    UNION

    (SELECT 
        ABS(SUM(ENTRY_AMT)) AS 'Total', 
        CATEGORY_DESC AS 'Category'


    FROM 
        Invoices AS gl

        LEFT JOIN Orders AS iod ON gl.ORDER_NO = iod.ORDER_NO AND gl.LINE_NO = iod.LINE_NO AND gl.APPEND = iod.APPEND

    WHERE 
            gl.YR = 13 
        AND gl.PERIOD = 1 
        AND ENTRY_SRC = 'revenue' 
        AND iod.CATEGORY_DESC NOT IN ('GENERAL', 'OTHER', 'RANDOM') 

        AND (gl.GL_ACCOUNT_NO BETWEEN 7100000000 AND 7100999999)
        AND gl.ITEM_NO NOT LIKE '%RS'
        AND gl.ITEM_NO NOT LIKE '%RA'


    GROUP BY CATEGORY_DESC

)

或者,我认为这更容易......

    (SELECT 
        ABS(SUM(ENTRY_AMT)) AS 'Total', 
        CATEGORY_DESC AS 'Category'


    FROM 
        Invoices AS gl

        LEFT JOIN Orders AS iod ON gl.ORDER_NO = iod.ORDER_NO AND gl.LINE_NO = iod.LINE_NO AND gl.APPEND = iod.APPEND

    WHERE 
            gl.YR = 13 
        AND gl.PERIOD = 1 
        AND ENTRY_SRC = 'revenue' 
        AND iod.CATEGORY_DESC NOT IN ('GENERAL', 'OTHER', 'RANDOM') 

        AND (gl.GL_ACCOUNT_NO BETWEEN 7100000000 AND 7100999999)
        AND gl.ITEM_NO NOT LIKE '%RS'
        AND gl.ITEM_NO NOT LIKE '%RA'


    GROUP BY Grouping sets ((CATEGORY_DESC),())
    )   
于 2013-02-21T23:21:11.057 回答