Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Counting duplicates Expand / Collapse
Author
Message
Posted Saturday, May 18, 2013 6:44 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 22, 2013 3:12 PM
Points: 2, Visits: 5
newbie question
I am trying to count the number of diabetic patients who have had their blood pressure checked twice or more at the clinic in the last year but I am lost in a muddle of a SQL

SELECT p.first_name"Provider", COUNT(distinct demo.last_name, demo.first_name) TotalCount
FROM measurements m, demographic demo, dxresearch dx, provider p
WHERE m.type="BP" and m.dateObserved > ((PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE()),-12)*100)+1) and
dx.dxresearch_code = '250' and
demo.patient_status = 'AC' and demo.roster_status not in ('TE-terminated','transient') and
demo.provider_no in ('101','102','105','120','121') and dx.status = 'A' and
demographicNo =demo.demographic_no and dx.demographic_no = demo.demographic_no and p.provider_no = demo.provider_no
group by p.first_name
HAVING COUNT(*) > 1
ORDER BY p.provider_no;

gets me optimistic numbers as a result set
Provider TotalCount
Peter 111
Tom 156
Mark 124
Phillip 45
Femi 12

However when I manually check Femi I get 9 where there are multiple BP entries (the correct answer) and 3 where there are only one reading

What am I doing wrong?
Post #1454296
Posted Saturday, May 18, 2013 9:19 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 22, 2013 3:12 PM
Points: 2, Visits: 5
I have gotten a bit further on by nesting a query, but I am not sure I have the distinct right

SELECT C.first_name"Provider", Count(C.provider_no) "Diabetics with over 1 BP in 12 months"
FROM
(Select distinct CONCAT(demo.last_name, ',', demo.first_name) as name, p.provider_no, p.first_name, Count(*)
FROM measurements m, demographic demo, dxresearch dx, provider p
WHERE m.type='BP' and m.dateObserved > ((PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE()),-12)*100)+1) and
dx.dxresearch_code = '250' and demo.patient_status = 'AC' and demo.roster_status not in ('TE-terminated','transient') and
demo.provider_no in ('101','102','105','120','121') and dx.status = 'A' and m.demographicNo =demo.demographic_no and
dx.demographic_no = demo.demographic_no and p.provider_no = demo.provider_no group by name
HAVING Count(*) > 1 ) as C
GROUP BY C.provider_no
Post #1454305
Posted Sunday, May 19, 2013 8:06 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 1:36 AM
Points: 1,950, Visits: 20,197
this looks like MySQL code ??

______________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
Post #1454331
Posted Wednesday, May 22, 2013 3:51 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, June 11, 2013 8:55 AM
Points: 51, Visits: 27
is the query conditions listed correctly . It may happen some conditions are not applied correctly because of spaces or something in the source table column values
Post #1455393
Posted Wednesday, May 22, 2013 7:36 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, October 31, 2013 3:44 AM
Points: 314, Visits: 4,128
phc (5/18/2013)
I have gotten a bit further on by nesting a query, but I am not sure I have the distinct right

SELECT C.first_name"Provider", Count(C.provider_no) "Diabetics with over 1 BP in 12 months"
FROM
(Select distinct CONCAT(demo.last_name, ',', demo.first_name) as name, p.provider_no, p.first_name, Count(*)
FROM measurements m, demographic demo, dxresearch dx, provider p
WHERE m.type='BP' and m.dateObserved > ((PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE()),-12)*100)+1) and
dx.dxresearch_code = '250' and demo.patient_status = 'AC' and demo.roster_status not in ('TE-terminated','transient') and
demo.provider_no in ('101','102','105','120','121') and dx.status = 'A' and m.demographicNo =demo.demographic_no and
dx.demographic_no = demo.demographic_no and p.provider_no = demo.provider_no group by name
HAVING Count(*) > 1 ) as C
GROUP BY C.provider_no


"CONCAT"? I don't believe this is TSQL (well, certainly not SQL 2008).

You will be better served by posting in Forums for the dialect of SQL you are using.
Post #1455509
Posted Tuesday, May 28, 2013 3:56 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: 2 days ago @ 7:28 PM
Points: 651, Visits: 3,015
OTF (5/22/2013)
phc (5/18/2013)
I have gotten a bit further on by nesting a query, but I am not sure I have the distinct right

SELECT C.first_name"Provider", Count(C.provider_no) "Diabetics with over 1 BP in 12 months"
FROM
(Select distinct CONCAT(demo.last_name, ',', demo.first_name) as name, p.provider_no, p.first_name, Count(*)
FROM measurements m, demographic demo, dxresearch dx, provider p
WHERE m.type='BP' and m.dateObserved > ((PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE()),-12)*100)+1) and
dx.dxresearch_code = '250' and demo.patient_status = 'AC' and demo.roster_status not in ('TE-terminated','transient') and
demo.provider_no in ('101','102','105','120','121') and dx.status = 'A' and m.demographicNo =demo.demographic_no and
dx.demographic_no = demo.demographic_no and p.provider_no = demo.provider_no group by name
HAVING Count(*) > 1 ) as C
GROUP BY C.provider_no


"CONCAT"? I don't believe this is TSQL (well, certainly not SQL 2008).

You will be better served by posting in Forums for the dialect of SQL you are using.


FYI: CONCAT is a T-SQL SQL 2012 feature.


-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001

My blog
Post #1457535
Posted Tuesday, May 28, 2013 4:38 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:41 AM
Points: 20,897, Visits: 32,932
Alan.B (5/28/2013)
OTF (5/22/2013)
phc (5/18/2013)
I have gotten a bit further on by nesting a query, but I am not sure I have the distinct right

SELECT C.first_name"Provider", Count(C.provider_no) "Diabetics with over 1 BP in 12 months"
FROM
(Select distinct CONCAT(demo.last_name, ',', demo.first_name) as name, p.provider_no, p.first_name, Count(*)
FROM measurements m, demographic demo, dxresearch dx, provider p
WHERE m.type='BP' and m.dateObserved > ((PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE()),-12)*100)+1) and
dx.dxresearch_code = '250' and demo.patient_status = 'AC' and demo.roster_status not in ('TE-terminated','transient') and
demo.provider_no in ('101','102','105','120','121') and dx.status = 'A' and m.demographicNo =demo.demographic_no and
dx.demographic_no = demo.demographic_no and p.provider_no = demo.provider_no group by name
HAVING Count(*) > 1 ) as C
GROUP BY C.provider_no


"CONCAT"? I don't believe this is TSQL (well, certainly not SQL 2008).

You will be better served by posting in Forums for the dialect of SQL you are using.


FYI: CONCAT is a T-SQL SQL 2012 feature.


Cool! However, EXTRACT and CURDATE do not appear to be new functions in SQL Server 2012.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1457545
Posted Tuesday, May 28, 2013 5:11 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: 2 days ago @ 7:28 PM
Points: 651, Visits: 3,015
Lynn Pettis (5/28/2013)
Alan.B (5/28/2013)
OTF (5/22/2013)
phc (5/18/2013)
I have gotten a bit further on by nesting a query, but I am not sure I have the distinct right

SELECT C.first_name"Provider", Count(C.provider_no) "Diabetics with over 1 BP in 12 months"
FROM
(Select distinct CONCAT(demo.last_name, ',', demo.first_name) as name, p.provider_no, p.first_name, Count(*)
FROM measurements m, demographic demo, dxresearch dx, provider p
WHERE m.type='BP' and m.dateObserved > ((PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE()),-12)*100)+1) and
dx.dxresearch_code = '250' and demo.patient_status = 'AC' and demo.roster_status not in ('TE-terminated','transient') and
demo.provider_no in ('101','102','105','120','121') and dx.status = 'A' and m.demographicNo =demo.demographic_no and
dx.demographic_no = demo.demographic_no and p.provider_no = demo.provider_no group by name
HAVING Count(*) > 1 ) as C
GROUP BY C.provider_no


"CONCAT"? I don't believe this is TSQL (well, certainly not SQL 2008).

You will be better served by posting in Forums for the dialect of SQL you a re using.


FYI: CONCAT is a T-SQL SQL 2012 feature.


Cool! However, EXTRACT and CURDATE do not appear to be new functions in SQL Server 2012.


I was replying to "'CONCAT'? I don't believe this is TSQL (well, certainly not SQL 2008)." Purely FYI


-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001

My blog
Post #1457551
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse