SQL 2012代码改进连接表和总和计数

时间:2016-01-11 15:56:53

标签: sql-server tsql join views

我正在查询SQL数据库,需要连接两个表(SCCM 2012 R2)。我下面的代码有效,但我想知道这是否可以通过另一种方法改进或实现,而不是创建一个视图并从中查询。

当我只使用没有视图的连接时,输出不正确,因为它显示了多个“产品”列,当我希望它是唯一的时,并且当多个“产品”列具有相同时,将“计数”列合并价值存在。最后一部分是计算'effectivequantity'(AI_NON_MS_LICENSE)列与先前计算的计数之间的差异。

(编辑 - 现在是临时表)

DROP TABLE #VIEW_LICENSES


CREATE TABLE #VIEW_LICENSES (

"Total Used" int,
"Product" char(30),
"License Status" char(30),
"Version" char(30)  )

INSERT INTO #VIEW_LICENSES ("Total Used", "Product", "License Status", "Version")

SELECT SUM("COUNT") AS 'Total Used', Tag1Name AS 'Product', Tag2Name AS 'Version', Tag3Name AS 'License Status'

FROM v_LU_SoftwareList_Editable LEFT JOIN AI_NON_MS_LICENSE

ON v_LU_SoftwareList_Editable.Tag1Name = Name

GROUP BY Tag1Name, Tag2Name, Tag3Name

GO


select "Total Used" AS 'Total Used' , Product AS 'Product',  "License Status" AS 'License Status', EffectiveQuantity AS 'Licensed Amount', (EffectiveQuantity)-("Total Used") AS "Remaining Licenses"

from #VIEW_LICENSES LEFT JOIN AI_NON_MS_LICENSE

ON #VIEW_LICENSES.Product = Name

GROUP BY [Product], [Total Used],[License Status],[EffectiveQuantity]

GO

0 个答案:

没有答案