Solved

SQL Query - Results for each month

Posted on 2014-07-20
4
211 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 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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