May 15, 2014 at 11:42 am
I want to add a column to this query which displays the number of times each MwID is present in the result set.
This number is needed to divide into FteHrsAVG.
Any suggestions are welcome!
TIA,
Julian
SELECTMwID,NiveauZPT,AfdelingZPT,FTEHrsAVG
FROMDRPDATA
WHERELocatieCode='sb' AND
DATEPART(iso_week,Begindatum) = 1 AND
AfdelingZPT = 'a03' AND
FlexVast = 'VAST'
ORDER BY MwID
May 15, 2014 at 12:22 pm
JJR333 (5/15/2014)
I want to add a column to this query which displays the number of times each MwID is present in the result set.This number is needed to divide into FteHrsAVG.
Any suggestions are welcome!
TIA,
Julian
SELECTMwID,NiveauZPT,AfdelingZPT,FTEHrsAVG
FROMDRPDATA
WHERELocatieCode='sb' AND
DATEPART(iso_week,Begindatum) = 1 AND
AfdelingZPT = 'a03' AND
FlexVast = 'VAST'
ORDER BY MwID
I can come up with about 6 different possibilities. They all depend on what your actual table looks like. In order to help we will need a few things:
1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data
Please take a few minutes and read the first article in my signature for best practices when posting questions.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 15, 2014 at 12:25 pm
Add a new column to the select:
Count(*) over(partition by MwID) as MwID_Count
May 15, 2014 at 12:57 pm
@gbritton1: thanks that worked!
The code below gives me the correct results.
How do you get the sum of Hrs for each NiveauZPT?
An aggregate cannot be used on a Windowed function. (error msg)
Cheers,
Julian
SELECTNiveauZPT,FTEHrsAVG/(Count(*) over(partition by MwID)) AS Hrs
FROMDRPDATA
WHERELocatieCode='sb' AND
DATEPART(iso_week,Begindatum) = 1 AND
AfdelingZPT = 'a03' AND
FlexVast = 'VAST'
ORDER BY MwID
May 15, 2014 at 1:10 pm
Add another windowed function:
sum(hours) over (partition by NiveauZPT)
(Interresant om het nederlands in de schema te zien!)
May 15, 2014 at 1:24 pm
I am half Dutch...
The result set looks like the table below.
Now I would like to sum the Hrs for each Niveau.
Any ideas?
Thanks,
Julian
NIVHRS
37
37
37
37
39
39
39
39
34
34
34
34
34
34
34
34
27,5
27,5
27,5
27,5
23,11111111111111
23,11111111111111
23,11111111111111
23,11111111111111
23,11111111111111
23,11111111111111
23,11111111111111
23,11111111111111
23,11111111111111
2+6
2+6
2+6
2+6
2+6
218
218
2+3
2+3
2+3
2+3
2+3
2+3
2+3
38
38
38
38
28
23,33333333333333
310,6666666666667
310,6666666666667
310,6666666666667
35,33333333333333
35,33333333333333
35,33333333333333
95,14285714285714
95,14285714285714
95,14285714285714
29,33333333333333
29,33333333333333
29,33333333333333
23,5
23,5
23,5
23,5
23,5
May 17, 2014 at 11:27 am
@gbritton1, thank you for pointing me in the right direction.
Ended up with using the code below.
Cheers,
Julian
-- JJR 20140508 ADDED UPDATE USING OVER PARTITION, MUCH FASTER
;WITH Updater AS
(
SELECT
FTEHrsAVG,
NewFTEHrsAVG = AVG(CASE
WHEN FlexVast = 'VAST'
THEN FTEHrs
ELSE NULL
END)
OVER(PARTITION BY MWID, [Org eenheid code], DATEPART(ISO_WEEK,BEGINDATUM))
FROM DRPDATA o
)
UPDATE Updater SET FTEHrsAVG = NewFTEHrsAVG
-- /JJR
/*
Return Rows per Week, per MWID and per AfdelingZPT (or Org eenheid code)
FTEHrsAVG divided by RowsPerWeek gives contract hours for the week, for the AfdelingZPT
ONLY if one sums per Week, per AfdelingZPT
*/
;WITH Updater AS
(
SELECT
RowsPerWeek,
NewRowsPerWeek =
count(CASE
WHEN FlexVast = 'VAST'
THEN FTEHrs
ELSE NULL
END)
OVER(PARTITION BY MWID, [Org eenheid code], DATEPART(ISO_WEEK,BEGINDATUM))
FROM DRPDATA
--WHERE EXISTS
)
UPDATE Updater SET RowsPerWeek = NewRowsPerWeek
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply