Solved

Query Month Interval

Posted on 2014-03-26
3
330 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
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 33

Accepted Solution

by:
ste5an earned 500 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

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

Suggested Solutions

Title # Comments Views Activity
best counters for cpu high usage 3 32
This query failed in sql 2014 5 31
SQL SELECT query help 7 41
Find results from sql within a time span 11 32
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

777 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