Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server Loop and Group By

Posted on 2013-01-30
8
Medium Priority
?
181 Views
Last Modified: 2013-01-31
Hello, Experts.
I want to do following for SQL Server looping.

I have several dates that needs to be broken down to intervals depending on days between.
For example, lets say I have some expired dates

01-01-13
01-15-13
01-30-13
02-02-13
03-15-13
03-22-13


I need that to be broken down into this format
Dates           Condition          Good From                 Group
01-01-13   |    Good      |            N/A            |            1
01-15-13   |    Good      |        01-01-13        |            1
01-30-13   |    Good      |        01-01-13        |            1
02-02-13   |    Good      |            N/A            |            2
02-17-13   |    Good      |        02-17-13        |            2
03-15-13   |    Good      |            N/A            |            3
03-22-13   |    Good      |        03-15-13        |            3

Can you guys help me how to do this in SQL Server 2005?

Thank you in advance!
0
Comment
Question by:kmc10314
[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
  • 5
  • 3
8 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 38837676
Please clearly and explicitly state the rule(s) to be used in determining the grouping and the value for the 'Good From' column.
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 38837993
Assuming that:
"Group" is determined by calendar month
Earliest date in a given month has no Good From value

Then this appears to do the trick:

CREATE TABLE SomeTable (Expiry datetime)

INSERT INTO SomeTable (Expiry) VALUES
('01-01-2013'),
('01-15-2013'),
('01-30-2013'),
('02-02-2013'),
('02-17-2013'),
('03-15-2013'),
('03-22-2013')

SELECT t1.Expiry AS Dates, 'Good' AS Condition, 
    CASE WHEN t1.Expiry = x.MinDate THEN NULL ELSE x.MinDate END AS [Good From],
    x.[Group]
FROM SomeTable t1 INNER JOIN
    (SELECT MIN(Expiry) AS MinDate, MAX(Expiry) AS MaxDate,
        ROW_NUMBER() OVER(ORDER BY DATEADD(month, DATEDIFF(month, 0, Expiry), 0)) AS [Group]
    FROM SomeTable
    GROUP BY DATEADD(month, DATEDIFF(month, 0, Expiry), 0)) x ON t1.Expiry >= x.MinDate AND t1.Expiry <= x.MaxDate
ORDER BY t1.Expiry

DROP TABLE SomeTable

Open in new window

0
 

Author Comment

by:kmc10314
ID: 38837994
Only critical point will be starting point.
For example, I used 30 days.

So if the initial starting point was 01-01-13, it will be grouped under 01-01-13 until 01-31-13.
Then it can restart on any date after that.
Let's say if it started on 02-02-13, the grouping will last until 03-04-13.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:kmc10314
ID: 38838080
I'll try this tomorrow and see how it works out.
Quick question though.
Would it work if the dates are random?  (i.e. some months may not have dates at all)
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 38839205
You're going to have to add more specifics--and a better example--around that thirty day rule.

As for random dates, my approach will work just fine, so long as the frame of reference is the calendar month.  So, the earliest month automatically gets Group 1, the next earliest Group 2, etc.
0
 

Author Comment

by:kmc10314
ID: 38839896
Hello,
I have tried it and it seems like it's always grouping at 1st of the month.
However, that is not the case for this example.
I did realize I made a mistake on the example above, but the concept should be same.
(Good From date for 2-17-13 should actually be 2-02-13)

Good From Date or the initial date of the group does is not always the 1st of the month.
As you see the, the groups can start on 2nd or 15th of the month.

01-01-13   |    Good      |            N/A            |            1                     -> Good From Date 1
01-15-13   |    Good      |        01-01-13        |            1
01-30-13   |    Good      |        01-01-13        |            1
02-02-13   |    Good      |            N/A            |            2                     -> Good From Date 2
02-17-13   |    Good      |        02-02-13        |            2
03-15-13   |    Good      |            N/A            |            3                     -> Good From Date 3
03-22-13   |    Good      |        03-15-13        |            3
0
 

Author Comment

by:kmc10314
ID: 38839931
NM, it did work.
Thank you for your help!
0
 

Author Comment

by:kmc10314
ID: 38840199
One more question, though
I just realized if the "Good From" Date starts from middle of the month and is stretched to next month, it does not pick up

For example,
03-15-13   |    Good      |            N/A            |            3      
03-22-13   |    Good      |        03-15-13        |            3                  -> Works
04-10-13   |    Good      |        03-15-13        |            3                  -> Does not Work.

Can you help me with this?
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

722 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