1

不太清楚如何给这个标题,但我尽我所能。我有一份报告,其中显示了选定位置的所有资产。我们的问题之一是我们将一切都视为资产。因此,一个典型的计算机系统将有 5 台计算机、8 台显示器,但有 10 多根网线。我正在尝试编写一份报告,该报告将“汇总”任何不必要的项目并显示计数而不是实际项目。

例如,我想要这样:

Desc            | Manufacturer  | Serial Number
-----+-------    ----+--------------------
20" monitor     | Dell          | 123456
25" monitor     | Dell          | 123456ab
6ft Net. Cable  | N/A           | NA123
6ft Net. Cable  | N/A           | NA124
6ft Net. Cable  | N/A           | NA125
6ft Net. Cable  | N/A           | NA456
1TB SATA HD     | SeaGate       | SG125000
1.5TB SATA HD   | SeaGate       | SG100000

成为:

Desc            | Manufacturer  | Serial Number/Qnty
-----+-------    ----+-----------------------------
20" monitor     | Dell          | 123456
25" monitor     | Dell          | 123456ab
6ft Net. Cable  | N/A           | 4
1TB SATA HD     | SeaGate       | SG125000
1.5TB SATA HD   | SeaGate       | SG100000

这是我的 SQL,它让我获得最高部分,但我不太确定从哪里开始获得最低结果。

SELECT i.ItemDescription AS Desc, s.Name AS Manufacturer, inv.SerialNumber AS [Serial Number]
FROM Assets a 
LEFT OUTER JOIN Inventory inv ON a.InventoryID = inv.InventoryID 
LEFT OUTER JOIN Items i ON inv.ItemID = i.ItemID 
LEFT OUTER JOIN Rooms r ON a.RoomID = r.RoomID 
LEFT OUTER JOIN Locations l ON r.LocationID = l.LocationID 
LEFT OUTER JOIN Suppliers s ON i.ManufacturerID = s.SupplierID 
WHERE l.LocationID = 5

我需要让“分组”行基于标准,所以它必须在 WHERE 子句中,比如: WHERE l.LocationID = 5 AND inv.SerialNumber LIKE 'NA%'但我不太确定把它放在哪里,所以它不会过滤我所有的记录序列号如“NA%”的那些

任何帮助是极大的赞赏!

4

2 回答 2

1

您应该能够使用以下内容:

;with cte as
(
  SELECT i.ItemDescription AS Desc, s.Name AS Manufacturer, inv.SerialNumber AS [Serial Number]
  FROM Assets a 
  LEFT OUTER JOIN Inventory inv ON a.InventoryID = inv.InventoryID 
  LEFT OUTER JOIN Items i ON inv.ItemID = i.ItemID 
  LEFT OUTER JOIN Rooms r ON a.RoomID = r.RoomID 
  LEFT OUTER JOIN Locations l ON r.LocationID = l.LocationID 
  LEFT OUTER JOIN Suppliers s ON i.ManufacturerID = s.SupplierID 
  WHERE l.LocationID = 5
),
totals as
(
  select [Desc], count(*) TotalCount
  from cte
  group by [desc]
)
select c.[desc],
  c.Manufacturer,
  case 
    when t.TotalCount > 1 
    then cast(t.TotalCount as varchar(50))
    else [Serial Number]
  end [Serial Number/Qnty]
from cte c
inner join totals t
  on c.[desc] = t.[desc]
于 2013-02-26T20:36:27.693 回答
0

试试这个

;with cte as

(
    SELECT i.ItemDescription AS Desc, s.Name AS Manufacturer, 
    CASE WHEN isnull(inv.SerialNumber,'') like 'NA%' Then 'NA'
         Else inv.SerialNumber end AS [Serial Number]

     FROM Assets a 
     LEFT OUTER JOIN Inventory inv ON a.InventoryID = inv.InventoryID 
     LEFT OUTER JOIN Items i ON inv.ItemID = i.ItemID 
     LEFT OUTER JOIN Rooms r ON a.RoomID = r.RoomID 
     LEFT OUTER JOIN Locations l ON r.LocationID = l.LocationID 
     LEFT OUTER JOIN Suppliers s ON i.ManufacturerID = s.SupplierID 
     WHERE l.LocationID = 5

), cte2 as
(
     SELECT Desc, Manufacturer, [Serial Number], Count(*) cnt
     FROM cte2
     GROUP BY Desc, Manufacturer, [Serial Number]
)
SELECT Desc, Manufacturer, CASE WHEN isnull([Serial Number],'') = 'NA' 
                                THEN CAST(cnt as varchar)
                                else [Serial Number] end [Serial Number]
from cte2;
于 2013-02-26T20:26:13.097 回答