SQL数据组的多个条件

时间:2012-08-14 16:19:56

标签: sql sql-server-2008

我正在尝试为以下数据集编写验证:

SSYS | Material_Number | Characteristic | Description
001  | 000000000001111 | SH_DESC        | TEST
001  | 000000000001111 | DESIGN_TYPE    | NULL
001  | 000000000001111 | VOLTAGE        | NULL
001  | 000000000009999 | SH_DESC        | TEST2
001  | 000000000009999 | OPER_METHOD    | LIGHT
001  | 000000000009999 | FILTER_TYPE    | Filter element,Air
001  | 000000000014560 | SH_DESC        | Horn,Signal
001  | 000000000014560 | DIMENSION_SIZE | NULL
001  | 000000000014560 | FILTER_TYPE    | NULL

如果在Material_Number组内,SH_DESC描述为NOT NULL且所有其他特征的描述为空,我想按Material_Number分组并计为1(即true)。所以,在这种情况下,我的结果将是:

SSYS | Material_Number | Characteristic | Description  | COUNT
001  | 000000000001111 | SH_DESC        | TEST         |  1
001  | 000000000009999 | SH_DESC        | TEST2        |  0 
001  | 000000000014560 | SH_DESC        | Horn,Signal  |  1

我的尝试:

Select COUNT (*), SSYS, Material_Number, Characteristic, Description
 From myDB where (Characteristic = 'SH_DESC' AND DESCRIPTION IS NOT NULL) AND (Characteristic NOT IN ('SH_DESC') IS NULL)
GROUP BY SSYS, Material_Number, Characteristic, Description HAVING COUNT (*) < 2

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:3)

尝试:

Select SSYS, 
       Material_Number, 
       'SH_DESC' Characteristic, 
       MAX(CASE WHEN Characteristic = 'SH_DESC' THEN Description END) Description,
       CASE WHEN MAX(CASE WHEN Characteristic = 'SH_DESC' THEN Description END) IS NOT NULL AND
                 MAX(CASE WHEN Characteristic <>'SH_DESC' THEN Description END) IS NULL 
            THEN 1
            ELSE 0
       END COUNT
From myDB
GROUP BY SSYS, Material_Number

答案 1 :(得分:0)

试试这个:

select ssys, material_number, 'SH_DESC' as characteristic,
       (case when sum(case when characteristic is not null and characteristic<> 'SH_DESC' and description is null then 1 else 0 end) = count(*) - 1
             then 1
             else 0
        end) as count
from t
group by ssys, material_number

按材质分组,并计算具有非null characterist的行数,其中描述为null。它相应地设定了数量。

答案 2 :(得分:0)

GROUP BYSUM(CASE WHEN)选项的替代方案......

 SELECT
  *,
  CASE WHEN Description IS NULL THEN 0
       WHEN EXISTS (SELECT *
                      FROM myDB as lookup
                     WHERE lookup.SSYS            = myDB.SSYS
                       AND lookup.Material_Number = myDB.Material_Number
                       AND lookup.Characteristic <> 'SH_DESC'
                       AND lookup.Description    IS NOT NULL) THEN 0
       ELSE 1 END as myCount
FROM
  myDB
WHERE
  Characteristic = 'SH_DESC'

答案 3 :(得分:0)

试试这个 - 在这里我想你不能得到描述bcos没有过滤特定描述。

CREATE TABLE yourtable(SSYS varchar(10),Material_Number varchar(100),Characteristics varchar(100),Description varchar(100))

INSERT INTO yourtable 
VALUES('001','000000000001111','SH_DESC','TEST'),
('001','000000000001111','DESIGN_TYPE','NULL'),
('001','000000000001111','VOLTAGE','NULL'),
('001','000000000009999','SH_DESC','TEST2'),
('001','000000000009999','SH_DESC','LIGHT'),
('001','000000000009999','FILTER_TYPE','Filter element,Air'),
('001','000000000014560','SH_DESC','Horn,Signal'),
('001','000000000014560','DIMENSION_SIZE','NULL'),
('001','000000000014560','FILTER_TYPE  ','NULL')

select max(SSYS),
       max(Material_Number),
       'SH_DESC' as Characteristic,
       CASE WHEN SUM(CASE WHEN Characteristic='SH_DESC' and Description is not null then 1 else 0 end) = 1 then 1 else 0 end  as cnt
from yourtable
group by Material_Number