[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Extract QTD data

Posted on 2014-03-11
5
Medium Priority
?
481 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
[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
  • 3
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1500 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 70

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

650 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