3

我的查询结果:

SELECT SYS_Product, 
       CASE 
         WHEN MEMVendor_Name = 'Unavailable' OR MEMVendor_Name = 'unknown' 
           THEN 'Error' 
         ELSE MEMVendor_Name 
       END AS MEMVendor_Name, 
       COUNT(MEMVendor_Name) AS MEMVendor_NameCount
FROM  dbo.ahs_master_bc_system_mem_vendor AS V
WHERE (AHS_CurrentCfg = 1) 
and SYS_Product='Gen8'
GROUP BY SYS_Product, MEMVendor_Name

如下:

SYS_Product | MEMVendor_Name| MEMVendor_NameCount
_____________________________________________
Gen8          Elpida           1
Gen8          Micron           84
Gen8          Nanya            28
Gen8          Netlist          0
Gen8          Samsung          55
Gen8          SK-Hynix         86
Gen8          Error            0 <----
Gen8          Error            2 <----

问题:我怎样才能得到 JUST the Error 的“总和”为 2(0+2​​) 仍然保持其他人的计数:

SYS_Product | MEMVendor_Name| MEMVendor_NameCount
_____________________________________________
Gen8          Elpida            1
Gen8          Micron            84
Gen8          Nanya             28
Gen8          Netlist           0
Gen8          Samsung           55
Gen8          SK-Hynix          86
Gen8          Error             2 <===

我需要 group by,因为这些结果将与基于 SYS_Product 和 MEMVendor_Name 的另一个查询相结合

谢谢。

4

3 回答 3

2

更改您group by以平等对待所有错误:

GROUP BY 
        SYS_Product
,       case 
        when MEMVendor_Name in ('Unavailable', 'unknown') then 'Error' 
        else MEMVendor_Name
        end
于 2012-10-10T18:09:43.417 回答
1

尝试添加case语句group by如下

SELECT SYS_Product, 
       CASE 
         WHEN MEMVendor_Name = 'Unavailable' OR MEMVendor_Name = 'unknown' 
           THEN 'Error' 
         ELSE MEMVendor_Name 
      END AS MEMVendor_Name, 
COUNT(MEMVendor_Name) AS MEMVendor_NameCount
FROM dbo.ahs_master_bc_system_mem_vendor AS V
WHERE (AHS_CurrentCfg = 1) 
and SYS_Product='Gen8'
GROUP BY SYS_Product, 
      CASE 
        WHEN MEMVendor_Name = 'Unavailable' OR MEMVendor_Name = 'unknown' 
          THEN 'Error' 
        ELSE MEMVendor_Name 
      END
于 2012-10-10T18:11:37.820 回答
0

只需引用列位置而不是子句中的名称:GROUP BY

SELECT
    SYS_Product, 
    CASE 
        WHEN MEMVendor_Name in ('Unavailable', 'unknown') THEN 'Error'
        ELSE MEMVendor_Name
    END AS MEMVendor_Name, 
    COUNT(MEMVendor_Name) AS MEMVendor_NameCount
FROM dbo.ahs_master_bc_system_mem_vendor AS V
WHERE AHS_CurrentCfg = 1
AND SYS_Product = 'Gen8'
GROUP BY 1, 2 -- The SQL standard allows group-by to reference column position

还要注意案例陈述的简化和重新格式化为更易读的格式

于 2012-10-10T18:12:41.200 回答