Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nested group function

Status
Not open for further replies.

latha9

Programmer
Dec 20, 2005
12
US
Hi,

I need to find the count according to some conditions.i have written the following query and i got the result.
select distinct i.INS_ID ,
count(CASE WHEN va.ANSWER <> 'CSS' or va.ANSWER <> 'RS' THEN 1 end) "Already in Treatment",
count(CASE WHEN va.ANSWER = 'CSS' THEN 1 end) "New to Treatment - CSS Only",
count(CASE WHEN va.ANSWER <> 'RS' THEN 1 end) "New to Treatment- RS Only",
count(CASE WHEN va.ANSWER = 'CSS' and va.ANSWER = 'RS' THEN 1 end) "New to Treatment - CSS and RS" ,
count(CASE WHEN (va.ANSWER <> 'CSS' or va.ANSWER <> 'RS') and pxa.ASSOC_ID=18 THEN 1 end) "CM AIL",
count(CASE WHEN (va.ANSWER <> 'CSS' or va.ANSWER <> 'RS') and pxa.ASSOC_ID !=18 THEN 1 end) "NCM AIL",
count(CASE WHEN va.ANSWER = 'CSS' and pxa.ASSOC_ID=18 THEN 1 end) "CM CSS",
count(CASE WHEN va.ANSWER = 'CSS' and pxa.ASSOC_ID !=18 THEN 1 end) "NCM CSS",
count(CASE WHEN va.ANSWER <> 'RS' and pxa.ASSOC_ID=18 THEN 1 end) "CM RS ",
count(CASE WHEN va.ANSWER <> 'RS' and pxa.ASSOC_ID !=18 THEN 1 end) "NCM RS" ,
count(CASE WHEN va.ANSWER = 'CSS' and va.ANSWER = 'RS' and pxa.ASSOC_ID=18 THEN 1 end) "CM NTT",
count(CASE WHEN va.ANSWER = 'CSS' and va.ANSWER = 'RS' and pxa.ASSOC_ID!=18 THEN 1 end) "NCM NIT"


from instrument i,dept_assessment da,v_assessment_answer
va,people_x_association pxa,association a
where pxa.PEO_ID= va.peo_id
and pxa.END_DATE is null
and a.assoc_id= pxa.ASSOC_ID
and a.assoc_id in (100,102,182,183,184,185)
and i.INS_NAME = 'MH Enrollment And Certification CSS RS'
and da.INS_ID=i.INS_ID
and va.INS_ID= i.INS_ID
and va.ASSMT_ID=da.ASSMT_ID
and va.INS_QUES_GRID_NAME= 'IF CONSUMER NEW TO SERVICE:'
and va.DATE_START >=to_date('01/18/2004','mm/dd/yyyy') and va.DATE_START <= to_date('02/10/2006','mm/dd/yyyy')
and va.DATE_END is null
group by i.INS_ID

now what i have to do is i have to find the sum of already in treatment ,new to treatment - css only ,new to treatment-RS only and new to treatment- css and rs only.
Is it possible to do it in a single SQL statement.
I have written a query in the following way.
select distinct i.INS_ID ,
count(CASE WHEN va.ANSWER <> 'CSS' or va.ANSWER <> 'RS' THEN 1 end) "Already in Treatment",
count(CASE WHEN va.ANSWER = 'CSS' THEN 1 end) "New to Treatment - CSS Only",
count(CASE WHEN va.ANSWER <> 'RS' THEN 1 end) "New to Treatment- RS Only",
count(CASE WHEN va.ANSWER = 'CSS' and va.ANSWER = 'RS' THEN 1 end) "New to Treatment - CSS and RS" ,
count(CASE WHEN (va.ANSWER <> 'CSS' or va.ANSWER <> 'RS') and pxa.ASSOC_ID=18 THEN 1 end) "CM AIL",
count(CASE WHEN (va.ANSWER <> 'CSS' or va.ANSWER <> 'RS') and pxa.ASSOC_ID !=18 THEN 1 end) "NCM AIL",
count(CASE WHEN va.ANSWER = 'CSS' and pxa.ASSOC_ID=18 THEN 1 end) "CM CSS",
count(CASE WHEN va.ANSWER = 'CSS' and pxa.ASSOC_ID !=18 THEN 1 end) "NCM CSS",
count(CASE WHEN va.ANSWER <> 'RS' and pxa.ASSOC_ID=18 THEN 1 end) "CM RS ",
count(CASE WHEN va.ANSWER <> 'RS' and pxa.ASSOC_ID !=18 THEN 1 end) "NCM RS" ,
count(CASE WHEN va.ANSWER = 'CSS' and va.ANSWER = 'RS' and pxa.ASSOC_ID=18 THEN 1 end) "CM NTT",
count(CASE WHEN va.ANSWER = 'CSS' and va.ANSWER = 'RS' and pxa.ASSOC_ID!=18 THEN 1 end) "NCM NIT" ,
sum(count(CASE WHEN va.ANSWER <> 'CSS' or va.ANSWER <> 'RS' THEN 1 end)+
count(CASE WHEN va.ANSWER = 'CSS' THEN 1 end)+count(CASE WHEN va.ANSWER <> 'RS' THEN 1 end)+
count(CASE WHEN va.ANSWER = 'CSS' and va.ANSWER = 'RS' THEN 1 end)) "total"



from instrument i,dept_assessment da,v_assessment_answer
va,people_x_association pxa,association a
where pxa.PEO_ID= va.peo_id
and pxa.END_DATE is null
and a.assoc_id= pxa.ASSOC_ID
and a.assoc_id in (100,102,182,183,184,185)
and i.INS_NAME = 'MH Enrollment And Certification CSS RS'
and da.INS_ID=i.INS_ID
and va.INS_ID= i.INS_ID
and va.ASSMT_ID=da.ASSMT_ID
and va.INS_QUES_GRID_NAME= 'IF CONSUMER NEW TO SERVICE:'
and va.DATE_START >=to_date('01/18/2004','mm/dd/yyyy') and va.DATE_START <= to_date('02/10/2006','mm/dd/yyyy')
and va.DATE_END is null
group by i.INS_ID

but i am getting an error ORA-00937 (not a single-group group function).

Can anyone suggest me how can i do this.please it's very urgent.

Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top