September 12, 2013 at 2:17 pm
we are tracking safety events and would like to count the # of consecutive months of 0 events starting from today backward. Maybe I'm overthinking it...but
For example:
Today is September and we had 0 events for Falls, it would be 1,
Today is September and we had 2 events for Falls in September and 0 in August, it would be 0
Today is September and we had 0 events for Falls in September and 0 in August, it would be 2
Select SafetyIndicator, count(safetyevent)
From SafetyEventTable
Group by SafetyIndicator
Would like to have it displayed as:
Indicator, Count
SafetyEvent1, 4
SafetyEvent2, 3
SafetyEvent3, 0
September 12, 2013 at 3:04 pm
There isn't enough information to provide an answer. 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/
September 12, 2013 at 7:07 pm
You might want to Google on calendar tables, because I believe you're going to need one to generate a sequence of months back in time as far as you want to look, before you start counting when events did not occur.
As Sean suggested, DDL, consumable sample data and expected results are sure to get you a tested solution.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
September 13, 2013 at 1:12 am
Sounds like you need to create a Pivot Table and groupings with a count of number months at zero ....
I will look into an example as this is something already done before.
________________________________________________________________________________________________
Regards
Steve
SQL 2008 DBA/DBD - MCTS/MCITP
Please don't trust me, test the solutions I give you before using them.
September 13, 2013 at 1:38 am
Check if this works. It should generate a count of events per month, including months with no events. If it works then it's a trivial matter to use it as a table source for a query returning your output:
SELECT
safetyevent,
c.[Month],
[Rows] = COUNT(*)
FROM SafetyEventTable si
RIGHT OUTER JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) c ([Month])
ON c.[Month] = MONTH(si.[Date])
GROUP BY safetyevent, c.[Month]
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply