Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi there!I've a dump question. What would be the best way without using cursors to get the following result?The query gives the following data:06:04:0006:19:0007:17:0007:24:0007:32:0007:54:0008:02:0008:09:0008:24:0008:39:0009:09:0009:24:00
select datepart(hh,fld), sum(datepart(mi,fld))from tblgroup by datepart(hh,fld)order by datepart(hh,fld)or if you want all hoursselect a.hr, sum(coalesce(datepart(mi,fld), 0)from (hr = select 1 union select 2 union slect 3 ... union select 12) as a left outer join tblon a.hr = datepart(hh,fld)group by a.hrorder by a.hr==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
netcop
Starting Member
12 Posts
Posted - 2002-09-02 : 08:20:27
The query with the sum - aggregate function isn't what i exactly wanted. I want the minute values grouped by the hour in which the result is in it.
select datepart(hh,fld), datepart(mi,fld)from tbl group by datepart(hh,fld), datepart(mi,fld)order by datepart(hh,fld)
This one gives me the correct result.
6 056 256 457 057 257 458 058 258 459 05
nr
SQLTeam MVY
12543 Posts
Posted - 2002-09-02 : 08:35:33
sorry. Didn't look at the results carefully enough.In that case you don't need the group by as it is grouping all the fields.Probably should put both in the order by tho.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
netcop
Starting Member
12 Posts
Posted - 2002-09-02 : 09:12:00
Maybe someone can tell me how to bring the result
hour minutes6 056 256 457 057 258 158 25
in this form...
hour minutes6 05 25 457 05 258 15 25
r937
Posting Yak Master
112 Posts
Posted - 2002-09-02 : 10:04:57
ah, ye olde crosstabyou'll have to do it in a stored proci just saw a neat trick this morning in another thread here, pointing to http://www.sqlteam.com/item.asp?ItemID=2368 which shows how to use COALESCE to suppress the comma for the first item in the denormalized comma-delimited list -- in your case you'll have a space delimited listrudyhttp://rudy.ca/
nr
SQLTeam MVY
12543 Posts
Posted - 2002-09-02 : 10:44:39
If you do a search on the site I think Rob wrote an article on generating a cross tab from any query.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
robvolk
Most Valuable Yak
15732 Posts
Posted - 2002-09-02 : 12:16:18
I think the cross-tab isn't what you need because you're consolidating everything into one column only. This thread should help:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=15651There are several other links in there with variations on Garth's article that Rudy posted earlier, check them all out.