Solved

Extract QTD data

Posted on 2014-03-11
5
462 Views
Last Modified: 2014-04-04
I need to help with the following syntax.  I need to capture sales for the current  Quarter To Date and sales for the same quarter To Date last year.

Current calender  QTD : ?

QTD  Last year: ?

Invoice_date           Customer_ ID               total_amount
1/1/13                      1                                   100
2/11/14                    1                                   200
3/31/13                    1                                   500
4/1/13                       2                                  100

I thought the thread below resolved it,  But it's giving full quarter for last year not last year QTD.

http://www.experts-exchange.com/Database/MS-SQL-Server/Q_28381957.html
0
Comment
Question by:tips54
  • 3
  • 2
5 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39921916
SELECT
    --CustomerId,
    DATEADD(QUARTER, DATEDIFF(QUARTER, 0, invoice_date), 0) AS Date,
    SUM(total_amount) AS QTD_Total
FROM dbo.tablename
WHERE
    --current qtd OR ...
    ((invoice_date >= DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()), 0)) OR
    --... prev year qtd thru today's date.
     (invoice_date >= DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()) - 4, 0) AND
      invoice_date < DATEADD(YEAR, -1, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 1, 0))))
GROUP BY
    --CustomerId,
    DATEADD(QUARTER, DATEDIFF(QUARTER, 0, invoice_date), 0)
ORDER BY
    --CustomerId,
    DATEADD(QUARTER, DATEDIFF(QUARTER, 0, invoice_date), 0)
0
 

Author Comment

by:tips54
ID: 39921988
Scott,
Could this same code be used to capture MTD,  YTD?
Expected outcome:

Weekly,  Weekly LY,  MTD, MTD LY, QTD ,  QTD LY, YTD,  YTD LY.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39923575
Sure.  You just adjust the DATEADD(...) computations accordingly.

The MTD, QTD and YTD are easy.

Would need slightly more details on weekly/wtd.
0
 

Author Comment

by:tips54
ID: 39932605
I'm making progress. I'm not certain of the results yet.
0
 

Author Closing Comment

by:tips54
ID: 39979086
thanks.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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.
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

770 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