Solved

Extract QTD data

Posted on 2014-03-11
5
455 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:
ScottPletcher 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:ScottPletcher
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now