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.
Author |
Topic |
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-11-23 : 10:06:37
|
I have the following querySelect blah,blah,blah,case when untildate > '31-march-2009' then datediff(d,fromdate,'31-march-2009') else totaldays end as sickdaysfrom absenceswhere blah = blah etc etc. this works correctly, however, I require to do a bit more of a calculation with the sickdays within the select statement. ie.Select blah,blah,blah,case when untildate > '31-march-2009' then datediff(d,fromdate,'31-march-2009') else totaldays end as sickdays,case when sickdays < 27 then sickdays else 0 end as shorttermsickfrom absenceswhere blah = blah etc etc. Now, this doesnt work as I cannot evaluate the sickdays field at this point.. any ideas what I can do for this?thanksC. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-23 : 10:15:44
|
it is with small modificationselect *,case when sickdays < 27 then sickdays else 0 end as shorttermsickfrom(Select blah,blah,blah,case when untildate > '31-march-2009' then datediff(d,fromdate,'31-march-2009') else totaldays end as sickdaysfrom absenceswhere blah = blah etc etc.)t |
|
|
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-11-23 : 10:24:22
|
error : Server: Msg 8155, Level 16, State 2, Line 1No column was specified for column 2 of 't'. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-11-23 : 10:42:48
|
[code]select *,case when sickdays < 27 then sickdays else 0 end as shorttermsickfrom(Select e.division,CONVERT(varchar,d.fromdate,103),e.employ_ref,case when d.untildate > '31-march-2009' then datediff(d,d.fromdate,'31-march-2009') else totaldays end as sickdays,case when d.totaldays < 27 then totaldays else 0 end as days_lost_nonlts,case when d.totaldays >= 27 then totaldays else 0 end as days_lost_lts,count(distinct (case when d.totaldays < 27 then d.absence_ref end)) as no_of_nonlts,count(distinct (case when d.totaldays >= 27 then d.absence_ref end)) as no_of_lts,count(distinct d.absence_ref) as no_of_absencesfrom absence d, employee_table e,where d.fromdate >= '1-april-2008' and d.fromdate <= '31-march-2009' and d.employ_ref = e.employ_ref and e.employ_ref = j.employ_refgroup by e.division, d.fromdate,e.employ_ref,e.department,d.totaldays,d.untildate) t[/code] |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-23 : 11:21:19
|
quote: Originally posted by cjonline
select *,case when sickdays < 27 then sickdays else 0 end as shorttermsickfrom(Select e.division,CONVERT(varchar,d.fromdate,103) as fromdate,e.employ_ref,case when d.untildate > '31-march-2009' then datediff(d,d.fromdate,'31-march-2009') else totaldays end as sickdays,case when d.totaldays < 27 then totaldays else 0 end as days_lost_nonlts,case when d.totaldays >= 27 then totaldays else 0 end as days_lost_lts,count(distinct (case when d.totaldays < 27 then d.absence_ref end)) as no_of_nonlts,count(distinct (case when d.totaldays >= 27 then d.absence_ref end)) as no_of_lts,count(distinct d.absence_ref) as no_of_absencesfrom absence d, employee_table e,where d.fromdate >= '1-april-2008' and d.fromdate <= '31-march-2009' and d.employ_ref = e.employ_ref and e.employ_ref = j.employ_refgroup by e.division, d.fromdate,e.employ_ref,e.department,d.totaldays,d.untildate) t
give 2nd field as an alias |
|
|
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-11-23 : 11:25:16
|
Thanks folks, that was the problem right enough!!!. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-11-23 : 11:32:23
|
welcome |
|
|
|
|
|
|
|