Solved

SQL Query - Results for each month

Posted on 2014-07-20
4
213 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 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