Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Count all months with "0's" starting today Expand / Collapse
Author
Message
Posted Thursday, September 12, 2013 2:17 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, February 03, 2014 1:52 PM
Points: 13, Visits: 57
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

Post #1494359
Posted Thursday, September 12, 2013 3:04 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 12,034, Visits: 11,062
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1494378
Posted Thursday, September 12, 2013 7:07 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:39 PM
Points: 3,596, Visits: 5,113
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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1494416
Posted Friday, September 13, 2013 1:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, April 17, 2014 5:43 AM
Points: 170, Visits: 405
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.
Post #1494484
Posted Friday, September 13, 2013 1:38 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:12 AM
Points: 6,783, Visits: 12,893
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]



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Post #1494492
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse