我目前有一个问题,我需要在数据库中报告几个销售领域的价值。第二个 `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
)