如何创建此枢轴SQL查询?

时间:2019-03-22 09:42:37

标签: sql sql-server

我在SQL 2012R2服务器上具有以下SQL表:

tblASSETS

------------------------------------
| ASSETID | ASSETTYPE | NAME       | 
|---------|-----------|------------|
| 1       | A         | Printer A  |
| 2       | A         | Printer B  |
| 3       | A         | Printer C  |
| 4       | B         | Laptop A   |
------------------------------------

tblASSETTYPES

--------------------------------------
| ASSETTYPE | TYPENAME | ICON        |
|-----------|----------|-------------|
| A         | Printer  | Printer.png |
| B         | Laptop   | Laptop.png  |
--------------------------------------

tblASSETCUSTOM

-------------------------------------------------------------
| CUSTOMID | ASSETID | MAKE | MODEL          | PRINTEDPAGES |
|----------|---------|------|----------------|--------------|
| 1        | 1       | HP   | Laserjet 4v    | 530          |
| 2        | 2       | HP   | Laserjet 4v    | 10000        |
| 3        | 3       | HP   | Officejet 1050 | NULL         |
| 4        | 4       | HP   | Probook 430 G3 | NULL         |
-------------------------------------------------------------

tblOIDDATA

---------------------------------------------
| OIDDATAID | ASSETID | LABEL        | DATA |
|-----------|---------|--------------|------|
| 1         | 1       | Black copies | 430  |
| 2         | 1       | Color copies | 110  |
| 3         | 2       | Black copies | 5300 |
| 4         | 2       | Scans        | 450  |
---------------------------------------------

我想构建一个查询,该查询以列的形式返回所有打印机及其所有详细信息。我已经创建了这个QUERY:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(label) 
                    from tblOIDData
                    group by label
                    order by label
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') 

set @query = 'Select * from (   Select Top 1000000 tAT.icon As icon, tat.typename as [ASSET TYPE] ,tA.Name as [ASSET NAME], tac.Model as [DEVICE MODEL], snmp.label as label, TRY_CONVERT(INT, TRY_CONVERT(NVARCHAR(100), snmp.data)) as PageCount, TA.Printepages as [PRINTED PAGES]   from tblAssets as tA,       tblAssetCustom as tAC,          tsysAssetTypes as tAT,      tblOIDData as SNMP  where tA.AssetType = tAT.AssetType      AND tAT.Typename = ''Printer''      AND tAC.AssetID = tA.AssetID        AND snmp.AssetID = tA.AssetID        ) as s PIVOT (     sum(PageCount)  for [LABEL] IN (' + @cols + ') ) AS pvt'

execute(@query);

这几乎可以得到想要的结果。我面对的唯一一件事是结果中没有ASSETID 3(打印机C)。可能是因为它不在tblOIDData表中。

如何在结果中也包含此资产?

1 个答案:

答案 0 :(得分:0)

您可以在动态SQL中使用LEFT JOIN

set @query = N'SELECT * 
FROM 
(
  SELECT TOP 1000000
   tAT.icon AS [icon],
   tat.typename AS [ASSET TYPE],
   tA.Name AS [ASSET NAME],
   tac.Model AS [DEVICE MODEL],
   snmp.label AS [label],
   TRY_CONVERT(INT, TRY_CONVERT(NVARCHAR(100), snmp.data)) AS [PageCount],
   TA.Printepages AS [PRINTED PAGES]
  FROM tblAssets AS tA
  JOIN tblAssetCustom AS tAC ON tAC.AssetID = tA.AssetID
  JOIN tsysAssetTypes AS tAT ON tAT.AssetType = tA.AssetType
  LEFT JOIN tblOIDData AS SNMP ON snmp.AssetID = tA.AssetID
  WHERE tAT.Typename = ''Printer''
) AS src 
PIVOT
(
  SUM([PageCount])
  FOR [label] IN (' + @cols + N') 
) AS pvt';