Solved

time periods

Posted on 2014-09-30
8
115 Views
Last Modified: 2014-10-13
Assuming a person joined a firm on 10-01-10

I need to derive the following time periods - 10-1-10 , 9-30-11, 10-1-11 , 9-30-12, 10-1-12 , 9-30-13, pls suggest the SQL

for ex:

Customer_no        DateAdded
 123            10-01-10      

Result should be as below:


Customer_no        DateAdded           T1              T2             T3             T4              T5             T6  

   123            10-01-10            10-1-10       9-30-11     10-1-11      9-30-12     10-1-12     9-30-13
0
Comment
Question by:sqlcurious
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40353309
What will the relevant dates be if he had joined on 15 October; the same or different?
0
 

Author Comment

by:sqlcurious
ID: 40353348
10-15-10 , 10-14-11, 10-15-11 , 10-14-12, 10-15-12 , 10-14-13

These will be the relevant ones if he joined on 10-15-10
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40353369
SELECT Customer_no,
    DateAdded AS T1,    
    DATEADD(DAY, -1, DATEADD(YEAR, 1, DateAdded)) AS T2,
    DATEADD(YEAR, 1, DateAdded) AS T3,
    DATEADD(DAY, -1, DATEADD(YEAR, 2, DateAdded)) AS T4,
    DATEADD(YEAR, 2, DateAdded) AS T5,
    DATEADD(DAY, -1, DATEADD(YEAR, 3, DateAdded)) AS T6
FROM (
    SELECT 123 AS Customer_no, CAST('20101001' AS date) AS DateAdded UNION ALL
    SELECT 234, '20141015'
) AS test_data
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 40353374
Try this:
-- Given table 
CREATE TABLE Cust (Customer_no INT, DateAdded DATE);
INSERT INTO dbo.Cust ( Customer_no, DateAdded )
VALUES  ( 123, -- Customer_no - int
          '10/1/10'  -- DateAdded - date
          ),
		  ( 124, -- Customer_no - int
          '10/15/10'  -- DateAdded - date
          );

--Example 1
SELECT C.Customer_no, C.DateAdded, C.DateAdded T1, DATEADD(YEAR, 1, DATEADD(DAY, -1, C.DateAdded)) T2, DATEADD(YEAR, 1, C.DateAdded) T3, DATEADD(YEAR, 1, DATEADD(DAY, -1, C.DateAdded)) T4, DATEADD(YEAR, 1, C.DateAdded) T5, DATEADD(YEAR, 1, DATEADD(DAY, -1, C.DateAdded)) T6
FROM dbo.Cust C;

Open in new window

Results in this: Example of results
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:sqlcurious
ID: 40353508
Thanks so much Chris , but have a small issue here
requirement says that If the person was added 10-15-12, we would need time periods 10-15-12, 10-14-13 and 10-15-13 only.
So only T1, T2 and T3 will have the date values, rest of them will be null as we donot have 10-15-14 yet(it's a date in future), how would I handle that?
0
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 40353620
OK, with the same setup table you can set up a CTE with your ranges and then use a PIVOT to get these results:
Query Resultshere is code for the CTE and PIVOT:
WITH YearRanges(Label, YearsToAdd, DaysToSubtract) AS 
(
SELECT 'T1', 0, 0
UNION ALL
SELECT 'T2', 1, 1
UNION ALL
SELECT 'T3', 1, 0
UNION ALL
SELECT 'T4', 2, 1
UNION ALL
SELECT 'T5', 2, 0
UNION ALL
SELECT 'T6', 3, 1
)
SELECT * FROM 
(
SELECT  C.Customer_no, 
        C.DateAdded, 
		YearRanges.Label,
        DATEADD(YEAR, YearRanges.YearsToAdd, DATEADD(DAY, -YearRanges.DaysToSubtract, C.DateAdded)) d
FROM dbo.Cust C
CROSS JOIN YearRanges
WHERE DATEADD(YEAR, YearRanges.YearsToAdd, DATEADD(DAY, -YearRanges.DaysToSubtract, C.DateAdded)) <= GETDATE()
) p
PIVOT (MIN(d) FOR Label IN ([T1], [T2], [T3], [T4], [T5], [T6])) pvt

Open in new window


The CTE is just a convenient way to create the work table, you could just as easy create a permanent helper table to hold those time period range values and use it to accomplish the same results.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40354039
Nice :-)
0
 

Author Closing Comment

by:sqlcurious
ID: 40377360
Thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Login 17 38
SQL Query Syntax Join 4 27
Trying to identify overlapping date ranges 5 12
ms sql last 8 weeks as columns 5 17
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

914 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

18 Experts available now in Live!

Get 1:1 Help Now