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
Solved

Extract QTD data

Posted on 2014-03-11
5
463 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

809 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