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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count and Case statement help 1

Status
Not open for further replies.

mongol648

Programmer
Nov 30, 2009
2
US
I have the following query I am trying to run:

select datefilled,
case len(timefilled)
when 7 then SUBSTRING(CAST(timefilled as varchar),1,1)
else SUBSTRING(CAST(timefilled as varchar),1,2)
end A
from ScriptHistory
where datefilled > getdate()-7
group by timefilled,datefilled

I basically want to count the number of fills by hour. the timefilled column is an INT field with data similar to this 9054035. I am trying to pull out the first or first and second characters depending on the length of the data. I can get everything but grouping and getting a count other than 1. Any suggestions??

Thanks
 
Try
Code:
select DateFilled, count(cast(Fill as int)) as FillPerDate from (select datefilled,   case len(timefilled)   when 7 then SUBSTRING(CAST(timefilled as varchar),1,1)   else SUBSTRING(CAST(timefilled as varchar),1,2)   end as Fill
from ScriptHistorywhere datefilled > dateadd(day,-7, getdate())) X group by DateFilled
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top