所以我有一个类似于下面的数据:
CREATE TABLE EXAMPLE (
PERSONID int,
Diagnosis varchar(255),
AdmissionDate date
)
INSERT INTO EXAMPLE
VALUES
('1','Broken Bone','2019-12-01'),
('2','Headache','2019-12-02'),
('3','Cancer','2020-01-05'),
('4','Broken Bone','2020-02-01'),
('5','Broken Bone','2020-03-01'),
('6','Headache','2020-03-06'),
('7','Cancer','2020-04-05')
我想使用group by子句,如下所示:
SELECT
Diagnosis,
COUNT(*) AS NumberOfDiagnosis
FROM EXAMPLE
GROUP BY Diagnosis
但是,我想只选择入院日期在2020-03-1之前的数据按子句分组。我有一个理解,我应该使用子查询或嵌套查询,但我不确定如何执行。我的期望输出类似于下表:
Diagnosis | NumberOfDiagnosis | 骨折 | 2 | 头痛
---|---|
1 | |
1 |
就用where clause
SELECT
Diagnosis,
COUNT(*) AS NumberOfDiagnosis
FROM EXAMPLE
where AdmissionDate<'2020-03-01'
GROUP BY Diagnosis
我想你只是想要一个where
条款:
SELECT Diagnosis, COUNT(*) AS NumberOfDiagnosis
FROM EXAMPLE
WHERE AdmissionDate < '2020-03-01'
GROUP BY Diagnosis;
在聚合之前过滤数据,因此只有在该日期之前的记录才包含在结果集中。