使用T_SQL将行注入查询结果

时间:2013-02-21 23:06:19

标签: sql sql-server tsql variables stored-procedures

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

如果我只是将@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
)   

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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),())
    )