?
Solved

Query Month Interval

Posted on 2014-03-26
3
Medium Priority
?
337 Views
Last Modified: 2014-03-26
I need a query to return the following columns:

Invoices Date,  Between 12 and 18 months
Invoices Date,  Between 18 and 24 months
Invoices Date,  Beyond 24 months

The filter is by date

Columns to consider: CustId, InvoiceDate, InvoiceNum, CreditAmount
0
Comment
Question by:rflorencio
[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 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 39955979
Something like...

SELECT whatever
WHERE invoicedate BETWEEN DATEADD (m, 12, GETDATE()) AND DATEADD (m, 18 , GETDATE())

Etc.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39956007
which format do you need? what will you do with the output?

SELECT CustId, InvoiceDate, InvoiceNum, CreditAmount, 'Between 12 and 18 months' as state
from yourtablename
WHERE invoicedate BETWEEN DATEADD (m, -12, GETDATE()) AND DATEADD (m, -18 , GETDATE())
union all
SELECT CustId, InvoiceDate, InvoiceNum, CreditAmount, 'Between 18 and 24 months' as state
from yourtablename
WHERE invoicedate BETWEEN DATEADD (m, -18, GETDATE()) AND DATEADD (m, -24, GETDATE())
union all
SELECT CustId, InvoiceDate, InvoiceNum, CreditAmount, 'Beyond 24 months' as state
from yourtablename
WHERE invoicedate < DATEADD (m, 24, GETDATE())
0
 
LVL 35

Accepted Solution

by:
ste5an earned 2000 total points
ID: 39956038
E.g.

WITH    Calculated
          AS ( SELECT   * ,
                        DATEDIFF(MONTH, InvoiceDate, GETDATE()) AS AgeInMonths
               FROM     yourTable
             )
    SELECT  * ,
            CASE WHEN AgeInMonths BETWEEN 12 AND 18 THEN '12-18'
                 WHEN AgeInMonths BETWEEN 18 AND 24 THEN '18-24'
                 WHEN AgeInMonths > 24 THEN '>24'
                 ELSE '<12'
            END AS IntervalText
    FROM    Calculated;

Open in new window

0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

762 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