0

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

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 描述不为空且所有其他特性的描述为空,我想按 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

4 回答 4

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
于 2012-08-14T16:28:35.650 回答
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

它按材料分组并计算描述为空的具有非空特征的行数。它相应地设置计数。

于 2012-08-14T16:28:54.720 回答
0

GROUP BY和选项的替代SUM(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'
于 2012-08-14T16:31:42.377 回答
0

试试这个——在这里我猜你不能得到描述,因为没有什么可以过滤特定的描述。

CREATE TABLE yourtable(SSYS varchar(10),Material_Number varchar(100),Characteristic 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
于 2012-08-14T17:10:01.877 回答