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 |
chenko
Starting Member
24 Posts |
Posted - 2007-04-16 : 11:43:34
|
Here is what I have so farSELECT MIN(tblDayDetail.intIn) / 60 AS ClockInHour INTO #HoursClockedInFROM tblDayDetailWHERE dteDate = '13/04/2007'GROUP BY intEmpIDSELECT ClockInHour, Count(*) As TotalFROM #HoursClockedInGROUP BY ClockInHourI do a group by to get multiple clockin times from an employee per day to get the first one, I then want to group them all per hour and get a count of how many clocked in at that hour. I cannot figure how I can do this by using a new group by. For now I have just used a temporary table and feel as if I have cheated.Whats the best way to do this?Next part is it returns a list of hours, and the amount clocked in durning that hour, some hours are blank so it wont return it so I get hours of like 3, 4, 5, 6, 13, 14. But what would be nicer is if it would list 0 1 2 3, all of them with 0 for the count. Silly idea to try?thanks :) |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-16 : 11:46:26
|
Please post sample data and expected output.Peter LarssonHelsingborg, Sweden |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-04-16 : 12:00:32
|
Well the current test table outputsClockInHour Total3 14 85 116 117 510 211 312 713 6 Where as I want it to output all 24 hoursClockInHour Total0 01 02 03 14 85 11.....22 023 0 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-16 : 12:58:52
|
SELECT x.number as clockinhour, coalesce(t1.total, 0) as Totalfrom (select number from master..spt_values where name is null and number between 0 and 23) as xleft join table1 as t1 on t1.clockinhour = x.numberorder by x.numberPeter LarssonHelsingborg, Sweden |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-04-16 : 17:31:38
|
Forgive me as I dont understand this.Table1 needs to be my temp table? #ClockedInHours (alias t1), where is t1.total from? since I did not have this field created (its not been possible for me to create the 2nd group by to get this total, the first issue above)I find that if I used my 2nd peice of code at the top, just inserted a null value with a clocked in hour for 0 to 23, and -1 from the count i could get it, but im not sure about inserting 24 records each time (unless there is a better way along these lines?) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-17 : 01:47:33
|
[code]SELECT x.number AS ClockInHour, COALESCE(t1.Total, 0) AS TotalFROM ( SELECT Number FROM master..spt_values WHERE Name IS NULL AND Number BETWEEN 0 AND 23 ) AS xLEFT JOIN ( SELECT ClockInHour, COUNT(*) AS Total FROM ( SELECT MIN(tblDayDetail.intIn) / 60 AS ClockInHour FROM tblDayDetail WHERE dteDate = '13/04/2007' GROUP BY intEmpID ) AS y GROUP BY ClockInHour ) AS t1 on t1.ClockInHour = x.NumberORDER BY x.Number[/code]Peter LarssonHelsingborg, Sweden |
 |
|
chenko
Starting Member
24 Posts |
Posted - 2007-04-17 : 11:33:18
|
Thanks for that, works perfectly and understood fully. :) |
 |
|
|
|
|
|
|