?
Solved

SQL Query - Results for each month

Posted on 2014-07-20
4
Medium Priority
?
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

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 49

Expert Comment

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

Accepted Solution

by:
PortletPaul earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

764 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