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
 General SQL Server Forums
 New to SQL Server Programming
 Unable to use convert in group by

Author  Topic 

lbeese
Starting Member

24 Posts

Posted - 2009-02-23 : 12:29:34
Hi,

I am trying to run the following query:

select a.member_id,
a.event_seq,
u.ccms_user_last_name,
a.admission_date,
a.actual_discharge_date,
case when a.actual_los > 19 then a.actual_los else 0 end as [Actual LOS],
case when a.discharge_status = '62' then a.discharge_status else 0 end as [DC to Inpt Rehab Facility],
case when a.discharge_status = '63' then a.discharge_status else 0 end as [DC to Long Term Care Hosp],
case when a.admission_source = '4' then a.admission_source else 0 end as [Transfer from a Hospital],
case when a.admission_source = '5' then a.admission_source else 0 end as [Transfer from a SNF],
case when a.admission_source = '6' then a.admission_source else 0 end as [Transfer from Another Facility],
case when d.clinical_day_type = 'IC' then convert (int, d.clinical_day_type) else 0 end as [ICU]
from ccmsdba.event_admission a, ccmsdba.event_admission_days d, ccmsdba.ccms_all_users u
where (a.admission_date between '2009-01-01 00:00:00' and '2009-01-31 00:00:00'
or a.actual_discharge_date between '2009-01-01 00:00:00' and '2009-01-31 00:00:00')
and a.member_id = d.member_id
and a.event_seq = d.event_seq
and u.ccms_id = a.event_manager
and a.event_manager not in (150, 222, 148, 175, 173, 335, 400, 169, 176, 166, 162, 178, 159, 157, 413, 155, 152, 151)
group by a.member_id, a.event_seq, u.ccms_user_last_name, a.admission_date, a.actual_los,
a.actual_discharge_date, a.discharge_status, a.admission_source, convert(int, d.clinical_day_type)

If I don't use the convert function in the group by I receive error: "Conversion failed when converting the varchar value 'IC ' to data type int."

When I do use the convert in the Group By I receive: "Column 'ccmsdba.event_admission_days.clinical_day_type' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

Any assistance is appreciated.

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-23 : 12:42:14
What is datatype for clinical_day_type ?
Go to Top of Page

lbeese
Starting Member

24 Posts

Posted - 2009-02-23 : 13:05:42
char
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-02-23 : 13:38:39
why are you converting a CHAR value like 'IC' to INT. Its definitely going to fail.

case when d.clinical_day_type = 'IC' then convert (int, d.clinical_day_type) else 0 end as [ICU]

what is the value you need for [ICU]? Should it be like 1 or 0 BIT type field?
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-23 : 13:38:47
Looks like you are looking for this:
Select a.member_id,
a.event_seq,
u.ccms_user_last_name,
a.admission_date,
a.actual_discharge_date,
Max(case when a.actual_los > 19 then a.actual_los else 0 end) as [Actual LOS],
Max(case when a.discharge_status = '62' then a.discharge_status else 0 end) as [DC to Inpt Rehab Facility],
Max(case when a.discharge_status = '63' then a.discharge_status else 0 end) as [DC to Long Term Care Hosp],
Max(case when a.admission_source = '4' then a.admission_source else 0 end) as [Transfer from a Hospital],
Max(case when a.admission_source = '5' then a.admission_source else 0 end) as [Transfer from a SNF],
Max(case when a.admission_source = '6' then a.admission_source else 0 end) as [Transfer from Another Facility],
Max(case when d.clinical_day_type = 'IC' then d.clinical_day_type else 0 end) as [ICU]
from ccmsdba.event_admission a, ccmsdba.event_admission_days d, ccmsdba.ccms_all_users u
where (a.admission_date between '2009-01-01 00:00:00' and '2009-01-31 00:00:00'
or a.actual_discharge_date between '2009-01-01 00:00:00' and '2009-01-31 00:00:00')
and a.member_id = d.member_id
and a.event_seq = d.event_seq
and u.ccms_id = a.event_manager
and a.event_manager not in (150, 222, 148, 175, 173, 335, 400, 169, 176, 166, 162, 178, 159, 157, 413, 155, 152, 151)
Group by a.member_id, a.event_seq, u.ccms_user_last_name, a.admission_date,a.actual_discharge_date
Go to Top of Page

lbeese
Starting Member

24 Posts

Posted - 2009-02-23 : 14:43:06
Thanks for your help. I think I have figured it out.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-23 : 14:53:54
quote:
Originally posted by lbeese

Thanks for your help. I think I have figured it out.



Excellent . Care to show how you solved or not?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-24 : 09:54:32
it was because of this case staement i guess
case when d.clinical_day_type = 'IC' then convert (int, d.clinical_day_type) else 0 end as [ICU]

the use of 0 in else was causing the problem as all conditions in case should return the value of same type.
Go to Top of Page
   

- Advertisement -