我正在处理两个问题:
Barcode
FROM Sputum_Collection
输出只有一条记录的行。 Barcode | ID | SputumNo | Volume | Collect_Date
201003 | NCL37M | S1 | Yes | 03/15/16
201003 | NCL37M | S3 |Yes | 03/16/16
201002 | PCL75F | S1 | Yes | 03/14/16
201001 |MCL29M | S1 |Yes |03/14/16
201003 |NCL37M | S2 |Yes |03/16/16
201004 |LKM23M | S1 |Yes |03/16/16
Barcode | ID | SputumNo | Volume | Collect_Date
201001 |MCL29M | S1 |Yes |03/14/16
201004 |LKM23M | S1 |Yes |03/16/16
Barcode | ID | SputumNo | Volume | Collect_Date
201003 | NCL37M | S1 | Yes | 03/15/16
201003 | NCL37M | S3 |Yes | 03/16/16
201002 | PCL75F | S1 | Yes | 03/14/16
201003 |NCL37M | S2 |Yes |03/16/16
答案 0 :(得分:1)
您可以使用嵌套选择根据条形码对数据进行分组,并使用HAVING
语句在组中添加条件。
查询1
select * from Sputum_Collection
where Barcode in
(
select Barcode from Sputum_Collection
group by barcode
having count(*) = 1
)
查询2
select * from Sputum_Collection
where Barcode in
(
select Barcode from Sputum_Collection
group by barcode
having count(*) > 1
)
答案 1 :(得分:0)
1,试试这个
select Barcode, ID, SputumNo, Volume, Collect_date
from
(
select Barcode, ID, SputumNo, Volume, Collect_date,
Count(Barcode) over(partition by Barcode) as Count
)
where Count = 1;
和2,你知道该怎么做......
答案 2 :(得分:0)
查询#1:
SELECT
a.*
FROM
imt_database.test a
LEFT JOIN
(SELECT
Barcode, COUNT(ID) as ct
FROM
imt_database.test
GROUP BY
id) tmp ON tmp.Barcode = a.Barcode
WHERE
ct =1
查询#2:
SELECT
a.*
FROM
imt_database.test a
LEFT JOIN
(SELECT
Barcode, COUNT(ID) as ct
FROM
imt_database.test
GROUP BY
id) tmp ON tmp.Barcode = a.Barcode
WHERE
ct > 1
取决于您的DBMS