Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Grouping Count Question

Author  Topic 

sross81
Posting Yak Master

228 Posts

Posted - 2008-06-05 : 16:17:38
Hello,

I have a query that in the end is to return totals by racial groups. In addition to grouping and counting on race I want to group based on percents. For example I want totals for all the racial groups for <=7%, >7% - <=9%, and >9%. The percents are stored in the hga1cpercent field which is also referenced when the race count is got but it isn't grouping on the hga1cpercent because I am not sure how to set up a grouping that requires columns be assigned to different groups. Is that possible? The percents are actually stored as numbers so 7 not 7% if that matters. Any help would be greatly appreciated! :)

With Diabetes(person_id) as
(
select distinct person_id from
(select count(*) as countperson,a.person_id,a.date_of_birth,
(2008-year(a.date_of_birth)) as age
from person a
join diabetes_ b on a.person_id = b.person_id
join patient_encounter c on a.person_id = b.person_id
where year(c.create_timestamp) = '2008'
group by a.person_id,a.date_of_birth)tmp
where tmp.age >=18 and tmp.countperson >=2
)
select count(*), b.race,a.hga1cpercent,max(a.create_timestamp)
from Diabetes tmp
join diabetes_flwsheet_ a on tmp.person_id = a.person_id
join person b on tmp.person_id = b.person_id
group by b.race,a.hga1cpercent

raja_saminathan
Starting Member

12 Posts

Posted - 2008-06-06 : 00:43:59
Hi,


Can you provide the table schema and the Sample Datas and the expected Results from those datas,

rajesh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-06 : 00:45:37
Try like this:-
With Diabetes(person_id) as
(
select distinct person_id from
(select count(*) as countperson,a.person_id,a.date_of_birth,
(2008-year(a.date_of_birth)) as age
from person a
join diabetes_ b on a.person_id = b.person_id
join patient_encounter c on a.person_id = b.person_id
where year(c.create_timestamp) = '2008'
group by a.person_id,a.date_of_birth)tmp
where tmp.age >=18 and tmp.countperson >=2
)
select count(*), b.race,
case when a.hga1cpercent<=7 then 1
when a.hga1cpercent>7 and a.hga1cpercent=9 then 2
when a.hga1cpercent>9 then 3
end as GroupType,max(a.create_timestamp)
from Diabetes tmp
join diabetes_flwsheet_ a on tmp.person_id = a.person_id
join person b on tmp.person_id = b.person_id
group by b.race,case when a.hga1cpercent<=7 then 1
when a.hga1cpercent>7 and a.hga1cpercent=9 then 2
when a.hga1cpercent>9 then 3
end
Go to Top of Page
   

- Advertisement -