Solved

SQL Query - Results for each month

Posted on 2014-07-20
4
207 Views
Last Modified: 2014-07-21
I am trying to write a query for a sales type report and want to get a record for each month of the year for each sales person regardless if they have any sales or if the month has occurred.  This query gives me a record for each month, but when I try to add in my table with the sales data, I only get a record if the sales person has sales.  I would like to have 12 rows for each sales person.

 
 
 WITH daterange AS (SELECT CAST(DATEADD(yy,DATEDIFF(yy,0,DATEADD(dd, DATEDIFF(dd, 0, GETDATE()),0)),0)  AS datetime) AS DateValue
                                             UNION ALL
                                             SELECT     DateValue + 1
                                              AS Expr1
                                             FROM         daterange AS daterange_2
                                             WHERE     (DateValue + 1 <= DATEADD(dd,-1,DATEADD(yy, DATEDIFF(yy,-1,DATEADD(dd, DATEDIFF(dd, 0, GETDATE()),0)),0))))
                                              
     
     ,daterange2 AS (SELECT * FROM daterange WHERE DAY(DateValue) = 1)
     
     
     SELECT DateValue from daterange2
    

     OPTION (MAXRECURSION 0) 

Open in new window

0
Comment
Question by:red_75116
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Vikas Garg
ID: 40208230
Hi,

You can do Cross Join with the Dates (Month, Year) with the Sales Person name
and after that just do left join of that with the sales data.

Hope this will work
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40208243
Do you use SQL Server 2012 or later?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40208272
Here is a much simpler CTE, just provide a start data and a number of months. It provides 2 dates per row for use when joining to your data ( do NOT use "between" ). As stated above join you data through a left join.
declare @BeginsAt as datetime = '2012-01-12'
declare @numMonths as int = 36

set @BeginsAt  = dateadd(month,datediff(month,0,@BeginsAt),0) -- force to 1st of Month

;with MonthRange (id, StartAt, StopAt)
as (
    select 1 as id, @BeginsAt, dateadd(month,1,@BeginsAt)
    union all
    select (id + 1) , dateadd(month,1,StartAt) , dateadd(month,1,StopAt)
    from MonthRange
    where (id + 1) <= @numMonths
    )
select
      MonthRange.StartAt
      ...
from MonthRange
left join YourData as d on ( d.a_date_field >= StartAt and d.a_date_field < StopAt )
group by
      MonthRange.StartAt

Open in new window

This is not you only option.

You might consider creating a calendar table. for example see:
http://www.experts-exchange.com/Database/MS-SQL-Server/A_12267-Date-Fun-Part-One-Build-your-own-SQL-calendar-table-to-perform-complex-date-expressions.html

And there are  other alternatives for a very similar question here
http://www.experts-exchange.com/Database/MS-SQL-Server/Q_28478877.html#a40206767

regarding between, for more on this please see: "Beware of Between"
0
 

Author Closing Comment

by:red_75116
ID: 40208961
Thanks,  I think I can get this to work.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now