artgal
asked on
Count (distinct ...) in SQL
I have a query that's supposedly counting distinct patients but when I count(distinct patientid) separate from the rest of the query it's obviously not working because the distinct patient count is lower. Can someone tell me what I'm doing wrong?
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,
AccountTypeCd, Activity_Flag, count(distinct PatientId) as Patcount, Activity_Name, Time_id, the_month, acuityflag
INTO dbo.FACT_ETMI_monthly_Data mart_PC
FROM dbo.FACT_ETMI_monthly_Data mart_PC1
GROUP BY PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,
AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
ORDER BY PayorName, time_id, LOBPAYOR, Program, Activity_Flag,AcuityFlag
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,
AccountTypeCd, Activity_Flag, count(distinct PatientId) as Patcount, Activity_Name, Time_id, the_month, acuityflag
INTO dbo.FACT_ETMI_monthly_Data
FROM dbo.FACT_ETMI_monthly_Data
GROUP BY PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,
AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
ORDER BY PayorName, time_id, LOBPAYOR, Program, Activity_Flag,AcuityFlag
One patient in multiple programs, timeIds,...
You might could do something like:
SELECT PayorID, PayorName, ..... SELECT A.* FROM ((your other count query here)A), ...
Not sure if that will work or not.
SELECT PayorID, PayorName, ..... SELECT A.* FROM ((your other count query here)A), ...
Not sure if that will work or not.
Please describe for us what you want in the Patcount column. Assuming the overall SELECT-INTO query is going to insert multiple rows, do you want Patcount to have the same value in each row, that is, the total of distinct patientid's without any conditions?
If so, try this:
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd, AccountTypeCd, Activity_Flag,
(SELECT count(distinct PatientId) FROM dbo.FACT_ETMI_monthly_Data mart_PC1) as Patcount,
Activity_Name, Time_id, the_month, acuityflag
INTO dbo.FACT_ETMI_monthly_Data mart_PC
FROM dbo.FACT_ETMI_monthly_Data mart_PC1
ORDER BY PayorName, time_id, LOBPAYOR, Program, Activity_Flag,AcuityFlag
If not, we need more explanation as to your goal.
If so, try this:
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd, AccountTypeCd, Activity_Flag,
(SELECT count(distinct PatientId) FROM dbo.FACT_ETMI_monthly_Data
Activity_Name, Time_id, the_month, acuityflag
INTO dbo.FACT_ETMI_monthly_Data
FROM dbo.FACT_ETMI_monthly_Data
ORDER BY PayorName, time_id, LOBPAYOR, Program, Activity_Flag,AcuityFlag
If not, we need more explanation as to your goal.
I am not sure you want count halves of patients, but maybe.
if
PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
columns are not null
then (else you must set ANSI_NULLS off or rewrite ON clause)
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd, AccountTypeCd, Activity_Flag, count(distinct A.PatientId)/Cnt as Patcount
, Activity_Name, Time_id, the_month, acuityflag
INTO dbo.FACT_ETMI_monthly_Data mart_PC
FROM dbo.FACT_ETMI_monthly_Data mart_PC1 A
JOIN
(
select PatientId,cast(count(*) as float) Cnt from (
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd, AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
, PatientId
FROM dbo.FACT_ETMI_monthly_Data mart_PC1
GROUP BY PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
) X
) B
ON
A.PayorID=B.PayorID
AND A.PayorName=B.PayorName
AND A.LOBPAYOR=B.LOBPAYOR
AND A.Program=B.Program
AND A.PayorTypeCd=B.PayorTypeC d
AND A.AccountTypeCd=B.AccountT ypeCd
AND A.Activity_Flag=B.Activity _Flag
AND A.Activity_Name=B.Activity _Name
AND A.Time_id=B.Time_id
AND A.the_month=B.the_month
AND A.acuityflag=B.acuityflag
GROUP BY PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd, AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
ORDER BY PayorName, time_id, LOBPAYOR, Program, Activity_Flag,AcuityFlag
if
PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,AccountTypeCd,
columns are not null
then (else you must set ANSI_NULLS off or rewrite ON clause)
--------------------------
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd, AccountTypeCd, Activity_Flag, count(distinct A.PatientId)/Cnt as Patcount
, Activity_Name, Time_id, the_month, acuityflag
INTO dbo.FACT_ETMI_monthly_Data
FROM dbo.FACT_ETMI_monthly_Data
JOIN
(
select PatientId,cast(count(*) as float) Cnt from (
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd, AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
, PatientId
FROM dbo.FACT_ETMI_monthly_Data
GROUP BY PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,AccountTypeCd,
) X
) B
ON
A.PayorID=B.PayorID
AND A.PayorName=B.PayorName
AND A.LOBPAYOR=B.LOBPAYOR
AND A.Program=B.Program
AND A.PayorTypeCd=B.PayorTypeC
AND A.AccountTypeCd=B.AccountT
AND A.Activity_Flag=B.Activity
AND A.Activity_Name=B.Activity
AND A.Time_id=B.Time_id
AND A.the_month=B.the_month
AND A.acuityflag=B.acuityflag
GROUP BY PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd, AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
ORDER BY PayorName, time_id, LOBPAYOR, Program, Activity_Flag,AcuityFlag
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
What I realized is that ispaleny was correct, there were patients in more than one program ... which is impossible. The person who wrote the original query is helping to eliminate those duplicates. I'm going to leave it open for another day to make sure that I don't need to eliminate the dupes myself (in which case I'll figure out what I need and come back here) but otherwise I'll give the points to ispaleny. Thanks so much. :)
Is your column called distinct PatientId? if no then you do it as below,
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,
AccountTypeCd, Activity_Flag, count(distinct (PatientId)) as Patcount......
SELECT PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,
AccountTypeCd, Activity_Flag, count(distinct (PatientId)) as Patcount......
Sorry.........I stupidly thought..................o k
see this
select count (PatientId) as Patcount , PayorID, PayorName, LOBPAYOR, Program, PayorType, AccountTypeCd, Activity_Flag, PatientId , Activity_Name, Time_id, the_month, acuityflag
INTO dbo.FACT_ETMI_monthly_Data mart_PC
FROM dbo.FACT_ETMI_monthly_Data mart_PC1 where PatientId in (SELECT distinct PatientId FROM dbo.FACT_ETMI_monthly_Data mart_PC1)
GROUP BY PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,
AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
ORDER BY PayorName, time_id, LOBPAYOR, Program, Activity_Flag,AcuityFlag
see this
select count (PatientId) as Patcount , PayorID, PayorName, LOBPAYOR, Program, PayorType, AccountTypeCd, Activity_Flag, PatientId , Activity_Name, Time_id, the_month, acuityflag
INTO dbo.FACT_ETMI_monthly_Data
FROM dbo.FACT_ETMI_monthly_Data
GROUP BY PayorID, PayorName, LOBPAYOR, Program, PayorTypeCd,
AccountTypeCd, Activity_Flag, Activity_Name, Time_id, the_month, acuityflag
ORDER BY PayorName, time_id, LOBPAYOR, Program, Activity_Flag,AcuityFlag