|
This has stumped me. Not sure how to get a distinct sum inside a case statement. Here's a sample of my data in the two fields (I'm actually doing this for a year but just 2 months here for easier reading): I need a count of each month in a column but each emp needs to count as one, like this: but instead I get 4 and 4. Here is my code I've tried putting the word distinct in between Select and sum but that made it a 1 and 1. Any help would be much appreciated. Cyndi
(comments are locked)
|
|
Try adding a group by. Without a dataset in front of me I would try a group by month(hist_date)
(comments are locked)
|
|
This is a two-step problem that you can solve with ROW_NUMBER. Here's a sample solution: The first part of the query is a common table expression which gets the row number for each employee ID based on the month and year in question. This is what you can use to filter out "duplicate" rows in the bottom expression: only get the ones where rownum = 1. The end results are what you are expecting, and it results in one scan of the table. One thing you might think about doing is flipping the query around, though, especially if you're presenting it in a presentation layer like an SSRS matrix or Excel table. The query as you have it now works, but you would need to update each month (or write a somewhat-nasty dynamic SQL statement to display each month). Instead, you could use the natural solution: Then, put it in an SSRS matrix and you'll automatically get the month and year as columns. Or if it's going out to Excel, you can do the same in a pivot table. EDIT - Fixed a bug in the partitioning. Now a person can show up in multiple months. I tried your first one with a change of adding my table data into the @table instead of by hand it worked perfect. Thanks for the quick answer, Cyndi
Nov 20 '12 at 10:43 PM
clynch
Huuummm...need help again I have a test query to ensure I am getting the correct numbers (empid, count(histdate) - then look at the number of rows there are for that month - run one month at a time). The correct amounts are:
When I use your first query for July only I get 14. When I run it for Aug only I get 10 and so on. When I broaden my dates from 7/1/11 to 12/31/11, my results are different. I get instead:
What is causing this discrepency?
Nov 21 '12 at 09:56 PM
clynch
I think it has to do with the datetime...I have sql server 2005 so it would not accept date, I had to change it to datetime....I changed my date range to hist_date BETWEEN CONVERT(DATETIME, '2011-12-01 00:00:00', 102) AND CONVERT(DATETIME, '2011-12-31 23:59:59', 102)) hoping it was the time throwing it all off...but that did not change the results...
Nov 21 '12 at 10:01 PM
clynch
Something the test data didn't have: an employee ID in multiple months. The original partition was by employee ID and ordering by month and year. Because you have employee IDs showing up in different months, month and year need to be part of the partition statement and not just the ordering.
Nov 21 '12 at 10:07 PM
Kevin Feasel
I think I may have found it but not sure...when it assigns the row numbers it is not starting back over with row number 1 at the next month for a person... so how do I adjust the query to do this?
Nov 21 '12 at 10:08 PM
clynch
(comments are locked)
|
|
If you don't need the separate dates in columns, you could write the query as: I need to go across not down...
Nov 21 '12 at 10:13 PM
clynch
I've updated the final select so the records go across, but this query will then need to be updated to accommodate any changes in months/years.
Nov 21 '12 at 10:26 PM
Beandon10
(comments are locked)
|

