|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 5:01 AM
Points: 4,
Visits: 28
|
|
Lets say ,There are 4 columns with Primary Key Constraints
L1 L2 L3 Date Forecast
1 a x 01-jan 1.2 1 a x 02-jan 2.0 1 a x 03-jan 3.0 1 a x 05-Jan 5.2
I want to group them, such that in the first group since it would retrieve 1 a x 03-jan 3.2 -- in the first group , which basically has the max(forecast) as the Date field is in a daily sequence.
In the Second group it would give 1 a x 05-Jan 5.2 , basically there is a break in the sequence of the date so its added in the second group.
Are there any suggestions for my problem.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 2,555,
Visits: 4,393
|
|
First of all I can suggest you to read article from the link found in the bottom of my signature. It will help your question to be answered by other forum members here From what I can see, one of the way you can go is to use "quirky" update method in order to calculate your groups: http://www.sqlservercentral.com/articles/T-SQL/68467/
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 8:15 AM
Points: 1,501,
Visits: 18,206
|
|
Have a look at the 'Group Islands of Contiguous Dates' article here
DECLARE @t TABLE(L1 INT,L2 CHAR(1), L3 CHAR(1), dt Date, Forecast NUMERIC(10,5)) INSERT INTO @t(L1,L2,L3,dt,Forecast) VALUES (1, 'a', 'x', '01-jan-2013', 1.2), (1, 'a', 'x', '02-jan-2013', 2.0), (1, 'a', 'x', '03-jan-2013', 3.0), (1, 'a', 'x', '05-Jan-2013', 5.2);
WITH CTE1 AS ( SELECT L1,L2,L3,dt,Forecast, ROW_NUMBER() OVER(ORDER BY dt DESC) AS rn1 FROM @t), CTE2 AS ( SELECT *, ROW_NUMBER() OVER(PARTITION BY DATEADD(Day,rn1,dt) ORDER BY rn1) AS rn2 FROM CTE1) SELECT L1,L2,L3,dt,Forecast FROM CTE2 WHERE rn2 = 1 ORDER BY dt;
____________________________________________________
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537
Never approach a goat from the front, a horse from the rear, or a fool from any direction.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 2,555,
Visits: 4,393
|
|
just small change to get record with MAX(Forecast), more sample data added:
DECLARE @t TABLE(L1 INT,L2 CHAR(1), L3 CHAR(1), dt Date, Forecast NUMERIC(10,5)) INSERT INTO @t(L1,L2,L3,dt,Forecast) VALUES (1, 'a', 'x', '01-dec-2012', 7.2), (1, 'a', 'x', '01-jan-2013', 1.2), (1, 'a', 'x', '02-jan-2013', 2.0), (1, 'a', 'x', '03-jan-2013', 3.0), (1, 'a', 'x', '05-Jan-2013', 5.2), (1, 'a', 'x', '06-Jan-2013', 5.2), (1, 'a', 'x', '06-Jan-2013', 4.2);
WITH CTE1 AS ( SELECT L1,L2,L3,dt,Forecast, ROW_NUMBER() OVER(ORDER BY dt DESC) AS rn1 FROM @t) ,CTE2 AS ( SELECT *, ROW_NUMBER() OVER(PARTITION BY DATEADD(Day,rn1,dt) ORDER BY dt, Forecast DESC) AS rn2 FROM CTE1)
select * from CTE2 where rn2 = 1
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 5:01 AM
Points: 4,
Visits: 28
|
|
I have a lot of data in my Table , Dont you think it would look clumsy if we insert all the data to @T
as done in DECLARE @t TABLE(L1 INT,L2 CHAR(1), L3 CHAR(1), dt Date, Forecast NUMERIC(10,5)) INSERT INTO @t(L1,L2,L3,dt,Forecast) VALUES (1, 'a', 'x', '01-dec-2012', 7.2), (1, 'a', 'x', '01-jan-2013', 1.2), (1, 'a', 'x', '02-jan-2013', 2.0), (1, 'a', 'x', '03-jan-2013', 3.0), (1, 'a', 'x', '05-Jan-2013', 5.2), (1, 'a', 'x', '06-Jan-2013', 5.2), (1, 'a', 'x', '06-Jan-2013', 4.2);
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 5:01 AM
Points: 4,
Visits: 28
|
|
Thanks for the reply Mark/Eugene , this works fine for this combination of Primary key contraints i.e 1 a x 01-jan 1 a x 02-Jan
but it fails when i introduce a new set of combination i.e. (1, 'a', 'x', '01-jan-2013', 1.2), (1, 'a', 'x', '02-jan-2013', 2.0), (1, 'a', 'x', '03-jan-2013', 3.0), (1, 'a', 'x', '05-Jan-2013', 5.2); (2, 'b', 'y', '01-Feb-2013', 4.0); (2, 'b', 'y', '02-Feb-2013', 4.0);
How can i get appropriate values in this case
|
|
|
|