Solved

SQL Query - Results for each month

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
kill process lock Sql server 9 63
replicated - directional or bidirectional? 3 34
date diff with Fiscal Calendar 4 52
Re-appearing SQL Server Agent jobs 7 28
In this article I will describe the Detach & Attach 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

790 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