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 |
|
Sitka
Aged Yak Warrior
571 Posts |
Posted - 2002-07-12 : 11:58:44
|
| Hi,Hi this article was close to what I needed so I tried to apply it to what I was doing.http://www.sqlteam.com/item.asp?ItemID=6692Hours Worked tableIDx int Identity[Period Ending] smalldatetime[Job #] char[Sequence #] int[Std Hrs] float[Ot Hrs] float[DT Hrs] float[Total Hrs] char[Shift Prem] charOld charSubCode intThe [Shift Prem] is a flag.If the field is null thenday shiftelseafternoon or night shift[Period Ending] will eventually be a variable passed in (ie. ADO command Parameter collecton)Returns all non shift premium hoursSELECT a.[Employee #], Sum([Std Hrs]) as notshiftpremtotal FROM [Hours Worked] a WHERE a.[Shift Prem] is Null AND a.[Period Ending] >= '10/05/2001' AND a.[Period Ending] <= '10/11/2001' Group by a.[Employee #]Employee # notshiftpremtotal ----------- -------------------------------3510045 51.53510366 42.753520180 8.03510010 54.03510133 40.03510260 57.53510052 44.753510074 16.253520288 52.53520245 8.0......................______________________________________________________________________Returns all non shift premium hoursSELECT b.[Employee #], Sum([Std Hrs]) as shiftpremtotal FROM [Hours Worked] b WHERE b.[Shift Prem] is NOT Null AND b.[Period Ending] >= '10/05/2001' AND b.[Period Ending] <= '10/11/2001' Group by b.[Employee #]Employee # shiftpremtotal ----------- -------------------------35100240 44.535100260 40.035100430 38.035100470 39.035100580 40.035100610 42.535100690 39.035100740 34.035100810 32.2535100860 46.535101050 18.035101120 26.035101240 37.535101520 28.2535101740 39.7535102070 26.0........................____________________________________________________________________________Put the two together Select distinct [Hours Worked].[Employee #], c.notshiftpremtotal, d.shiftpremtotalFROM [Hours Worked]JOIN (SELECT a.[Employee #], Sum([Std Hrs]) as notshiftpremtotal FROM [Hours Worked] a WHERE a.[Shift Prem] is Null AND a.[Period Ending] >= '10/05/2001' AND a.[Period Ending] <= '10/11/2001' Group by a.[Employee #]) as cON [Hours Worked].[Employee #] = c.[Employee #]JOIN (SELECT b.[Employee #], Sum([Std Hrs]) as shiftpremtotal FROM [Hours Worked] b WHERE b.[Shift Prem] is NOT Null AND b.[Period Ending] >= '10/05/2001' AND b.[Period Ending] <= '10/11/2001' Group by b.[Employee #]) as dON [Hours Worked].[Employee #] = d.[Employee #]Employee # notshiftpremtotal shiftpremtotal ----------- ------------------ -------------------3510024 8.0 44.53510026 8.0 40.03510043 8.0 38.03510047 8.0 39.03510058 8.0 40.03510061 8.0 42.53510069 8.0 39.03510074 16.25 34.03510081 24.0 32.253510086 8.0 46.53510105 30.5 18.03510112 22.0 26.03510124 8.0 37.5_________________________________________________________________________________________________<HOMER VOICE> !doh! </HOMER VOICE>That isn't what I wanted! This returns just the employees have worked both shifts within the weekI need the Employee list to be complete at each stage like(Select distinct [Hours Worked].[Employee #] from [Hours Worked]) and the aggregate SUMs, notshiftpremtotal and shiftpremiumtotal to be zero if there is no value.But the conditional on the [shift prem] flag has to be put somewhere and I got mixed up.How to fix?edit for group by b. vs a. in second select, it was wrongEdited by - Sitka on 07/12/2002 13:33:00 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-07-12 : 12:14:23
|
| The CASE expression, once again, to the rescue:SELECT b.[Employee #], Sum(CASE WHEN b.[Shift Prem] IS NOT NULL THEN [Std Hrs] ELSE 0 END) as shiftpremtotal,Sum(CASE WHEN b.[Shift Prem] IS NULL THEN [Std Hrs] ELSE 0 END) as notshiftpremtotal FROM [Hours Worked] b WHERE b.[Period Ending] >= '10/05/2001' AND b.[Period Ending] <= '10/11/2001' Group by a.[Employee #] |
 |
|
|
Sitka
Aged Yak Warrior
571 Posts |
Posted - 2002-07-12 : 13:13:50
|
Like a 69 Polara Ragtop [url]http://www.moparpicturebook.com/newpage202.htm[/url] **Clean!!!...**Thanks so much.More than this specific solution,was a committment to search out the non cursor way.Got a little off track there, (wow...look out...big clumsy drunk guy..comin thru.. )The failure came in not having experience/skillswith uses of CASE. Gonna try and grind it home now, with some extrastudy. Hope it sticks and other things will just have to wait. What was the time on that reply? ~6 minutes 45 seconds. Unreal.Thanks again.Edited by - Sitka on 07/12/2002 13:29:03 |
 |
|
|
|
|
|
|
|