Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
marcoviy
Starting Member
6 Posts |
Posted - 2011-09-09 : 16:59:54
|
Hello Folks,Could you please help me?I have the following table. I also have the following query which gets the last reading before a given date (inclussive)DECLARE@wellTest TABLE (ID INT, WELL_ID INT, START_AT DATE, NET_OIL INT) INSERT INTO @wellTest SELECT1,1,'9/1/2011',3UNIONALL SELECT 2,2,'9/1/2011',8 UNIONALL SELECT 3,3,'9/1/2011',1 UNIONALL SELECT 4,1,'9/18/2011',5 UNIONALL SELECT 5,2,'9/18/2011',13 UNIONALL SELECT 6,3,'9/10/2011',2 UNIONALL SELECT 7,1,'9/21/2011',1 UNIONALL SELECT 8,2,'9/22/2011',7 UNIONALL SELECT 9,3,'9/22/2011',5 UNIONALL SELECT 10,1,'9/24/2011',2 UNIONALL SELECT 11,2,'9/25/2011',5 UNIONALL SELECT 12,3,'9/24/2011',2 select * from @wellTestID WELL_ID START_AT NET_OIL1 1 2011-06-05 32 2 2011-06-06 83 3 2011-06-07 14 1 2011-06-18 55 2 2011-06-18 136 3 2011-06-10 27 1 2011-06-21 18 2 2011-06-22 79 3 2011-06-22 510 1 2011-06-24 211 2 2011-06-25 512 3 2011-06-24 2 The query is:SELECT ID, WELL_ID, START_AT, NET_OIL FROM @wellTest wt_1 WHERE wt_1.ID IN (SELECT ID FROM @wellTest AS wt_2 WHERE (START_AT = (SELECT MAX(START_AT) AS Expr1 FROM @wellTest AS wt_3 WHERE (START_AT <= @fromDate) AND (WELL_ID = wt_2.WELL_ID)))) The result this query gives is:DECLARE@fromDate DATETIME SET @fromDate = '9/24/2011' ID WELL_ID START_AT NET_OIL8 1 2011-09-22 710 2 2011-09-24 212 3 2011-09-24 2What I need now is get the last reading not before a given date, but before every day within the month. That is to say as if the given date would be varying along the month day after day i.e.ID WELL_ID START_AT NET_OIL 1 1 2011-09-01 3 For day 12 2 2011-09-01 8 For day 13 3 2011-09-01 1 For day 11 1 2011-09-01 3 For day 22 2 2011-09-01 8 For day 23 3 2011-09-01 1 For day 2 … … … …4 1 2011-09-18 5 For day 185 2 2011-09-18 13 For day 186 3 2011-09-10 2 For day 18 … … … …10 1 2011-09-24 2 For day 3011 2 2011-09-25 5 For day 3012 3 2011-09-24 2 For day 30Thank you very much and your help is appreciated,Marco |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-10 : 00:55:01
|
shouldnt value for WELL_ID=3 be changing after day 10?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-10 : 00:55:01
|
shouldnt value for WELL_ID=3 be changing after day 10?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
marcoviy
Starting Member
6 Posts |
Posted - 2011-09-10 : 13:04:11
|
Hey Visakh16,No. All WELL_IDs (WELL_ID=1,WELL_ID=2,WELL_ID=3) should be displayed every day. What would change is the value for ID, START_AT and NET_OIL. For day 10, the query result will be:ID WELL_ID START_AT NET_OIL1 1 2011-09-01 32 2 2011-09-01 86 3 2011-09-10 2 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-10 : 14:10:12
|
or is it like unless you've a value for that day, you repeat the last available value?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|