[英]Sql Server group by sets of columns
I have a data set where I need to count patient visits with such rules: 我有一个数据集,我需要用这样的规则计算患者就诊次数:
Example data: 示例数据:
DoctorId PatientId VisitDate ReasonCode RowId
-------- --------- --------- ---------- -----
1 100 2014-01-01 200 1
1 100 2014-01-01 210 2
2 100 2014-01-01 200 3
2 100 2014-01-11 300 4
1 100 2014-01-15 200 5
2 400 2014-01-15 200 6
In this example, my final count would be based on grouping rowId 1, 2, 3 for 1 visit; 在这个例子中,我的最终计数将基于对1次访问的rowId 1,2,3进行分组; grouping row 4 as 1 visit, grouping row 5 as 1 visit for a total of 3 visits for patient 100. Patient 400 has 1 visit as well.
将第4行分组为1次访问,将第5行分组为1次访问,对患者100进行总共3次访问。患者400也有1次访问。
patientid visitdate numberofvisits
--------- --------- --------------
100 2014-01-01 3
100 2014-01-11 1
100 2014-01-15 1
400 2014-01-15 1
Where I'm stuck is how to handle the group by so that I get the different scenarios covered. 我被困在哪里是如何处理组,以便我得到涵盖的不同场景。 If the grouping were doctor, date, I'd be fine.
如果分组是医生,约会,我会没事的。 If it were doctor, date, ReasonCode, I'd be fine.
如果是医生,日期,ReasonCode,我会没事的。 It's the logic of the doctorId and the ReasonCode in the scenario where 2 doctors are involved, and doctorid and date in the other when it's the same doctor.
这是在涉及2名医生的情况下的doctorId和ReasonCode的逻辑,并且当它是同一位医生时,医生和日期在另一位医生。 I've not been deeply into Sql Server in a long time, so it's possible that a common table expression is the solution and I'm not seeing it.
我很久没有深入研究过Sql Server了,所以有一种常见的表表达式是解决方案,而我却没有看到它。 I'm using Sql Server 2014 and there's a decent lattitude in performance.
我正在使用Sql Server 2014,并且在性能上有相当的优势。 I would be looking for a sql server query that produces the results above.
我会寻找一个产生上述结果的sql server查询。 As best I can tell, there's no way to group this the way I need it counted.
我能说的最好,没有办法按照我需要的方式对它进行分组。
The answer was an except clause and grouping each of the sets before a final count. 答案是一个except子句,并在最终计数之前对每个集合进行分组。 Sometimes, we over-complicate things.
有时,我们过于复杂。
DECLARE @tblAllData TABLE
(
DoctorId INT NOT NULL
, PatientId INT NOT NULL
, VisitDate DATE NOT NULL
, ReasonCode INT NOT NULL
, RowId INT NOT NULL
)
INSERT @tblAllData
SELECT
1,100,'2014-01-01',200,1
UNION ALL
SELECT
1,100,'2014-01-01',210,2
UNION ALL
SELECT
2,100,'2014-01-01',200,3
UNION ALL
SELECT
2,100,'2014-01-11',300,4
UNION ALL
SELECT
1,100,'2014-01-15',200,5
UNION ALL
SELECT
2,400,'2014-01-15',200,6
DECLARE @tblTempCountedRows AS TABLE
(
PatientId INT NOT NULL
, VisitDate DATE
, ReasonCode INT
)
INSERT @tblTempCountedRows
SELECT PatientId, VisitDate,0
FROM @tblAllData
GROUP BY PatientId, DoctorId, VisitDate
EXCEPT
SELECT PatientId, VisitDate, ReasonCode
FROM @tblAllData
GROUP BY PatientId, VisitDate, ReasonCode
select * from @tblTempCountedRows
DECLARE @tblFinalCountedRows AS TABLE
(
PatientId INT NOT NULL
, VisitCount INT
)
INSERT @tblFinalCountedRows
SELECT
PatientId
, count(1) as Member_visit_Count
FROM
@tblTempCountedRows
GROUP BY PatientId
SELECT * from @tblFinalCountedRows
Here's a Sql Fiddle with the results: Sql Fiddle 这是一个Sql Fiddle的结果: Sql Fiddle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.