I am trying to do a nice SQL statement inside a stored procedure.
I looked at the issue of seeing the number of days that events happened between two dates. My example is sales orders: for this month, how many days did we have sales orders?
Suppose this setup:
CREATE TABLE `sandbox`.`orders` ( `year` int, `month` int, `day` int, `desc` varchar(255) ) INSERT INTO orders (year, month, day, desc) VALUES (2009,1,1, 'New Years Resolution 1') ,(2009,1,1, 'Promise lose weight') ,(2009,1,2, 'Bagel') ,(2009,1,12, 'Coffee to go')
For this in-data the result should be 3, since there has been three days with sale. The best solution I found is as below.
However, making a temporary table, counting that then dropping it seemes excess. It ‘should’ be possible in one statement.
Anyone who got a ‘nicer’ solution then me?
/L
SELECT [Year], [Month], [Day] INTO #Some_Days FROM Quarter WHERE Start >= '2009-01-01' AND [End] < '2009-01-16' GROUP BY [Year], [Month], [Day] SELECT count(*) from #Some_Days
Apologies if I’m misunderstanding the question, but perhaps you could do something like this, as an option:
… to get around the temp-table creation and disposal?