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 |
|
Shrews14
Starting Member
37 Posts |
Posted - 2009-05-01 : 11:08:36
|
| Hi,Thanks for looking.I have two tables (table Location & values) where there are two joins and i need to transfer a value from one table (Values) into a new query and matching up to the corresponding location, sounds easy but there a number of different 'values' for the same 'Location'.Each 'location' has a date and so does each 'value' so i need a way of matching where the location date come in the data range 'value' and ONLY select that value example;-'Location table''Loc','sample_date'K01,2001-01-01K01,2002-01-01K01,2003-01-01'Value ref table''Loc','Value','Starting date'K01,1.23,1995-01-01K01,1.67,2001-06-01K01,2.7,2004-01-01Need the resulting view/query to be'Loc','Sample_date','Value'K01,2001-01-01,1.23K01,2002-01-01,1.67K01,2003-01-01,1.67I started creating a temp table which would send back this data:K01,2001-01-01,1.23,1995-01-01K01,2001-01-01,1.67,2001-06-01K01,2001-01-01,2.7,2004-01-01K01,2002-01-01,1.23,1995-01-01K01,2002-01-01,1.67,2001-06-01K01,2002-01-01,2.7,2004-01-01K01,2003-01-01,1.23,1995-01-01K01,2003-01-01,1.67,2001-06-01K01,2003-01-01,2.7,2004-01-01Then i thought i could update/delete the temp table to select the records i needed (indicated in bold above).My idea is to use a case/if statement so that each record has to fit a number of conditions to NOT be deleted from the temp table.But i can't figure out the logic, any pointer/or questions would be greatfully accepted. |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-05-01 : 12:10:11
|
| I don't see how you are selecting the ones you need. What are your conditions for keeping a record?Why keep (K01,2002-01-01,1.67,2001-06-01) over (K01,2002-01-01,1.23,1995-01-01 and K01,2002-01-01,2.7,2004-01-01) ?[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-05-01 : 12:14:51
|
This gives you what you want, but i do not know if it follows your criteria.declare @LT table (Loc char(3),[Sample_date] datetime)declare @VRT table (Loc char(3), Value decimal(3,2),[Starting_date] datetime)insert into @LT (Loc,Sample_date)select 'K01','2001-01-01' union all select 'K01','2002-01-01' union all select 'K01','2003-01-01' insert into @VRT (Loc,Value,Starting_date)select 'K01',1.23,'1995-01-01' union all select 'K01',1.67,'2001-06-01' union all select 'K01',2.7,'2004-01-01' select l.loc,l.Sample_date,max(v.value),max(v.starting_date)from @LT linner join @VRT vON l.loc = v.locand v.starting_date <= l.sample_date group by l.loc,l.Sample_date Output:K01 2001-01-01 00:00:00.000 1.23 1995-01-01 00:00:00.000K01 2002-01-01 00:00:00.000 1.67 2001-06-01 00:00:00.000K01 2003-01-01 00:00:00.000 1.67 2001-06-01 00:00:00.000[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
Shrews14
Starting Member
37 Posts |
Posted - 2009-05-01 : 12:28:10
|
| Cheers Don will look at this now,(I knew i'd struggle to convey this)The selection of the value is dependent on the sample_date and where it is the the ranges of the values.Loc,starting dateK01,1995-01-01K01,2001-06-01K01,2004-01-01The ending date of the first record would be 2001-05-31 23:59:59The ending date of the first record would be 2003-12-31 23:59:59The ending date of the first record would be open as it's the current value.Then the record in the Location table has date which will then fit into only one of the above date ranges hope that answers why the particular records were selected. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-05-01 : 12:39:08
|
Here is another way to do this. I tried to break the problem out a little bit so that it *might* be easier to understand (although probably not as effecient as Don's). The first part is a CTE with just orders the Value table by StartingDate. This is then used to LEFT join to itself to get the date ranges. Fianlly, this derived table of date ranges is joined to the Location table to get the Value for that location. Hopefully, it makes sense:DECLARE @Location table (Loc VARCHAR(3), sample_date DATETIME)INSERT @LocationSELECT 'K01','2001-01-01'UNION ALL SELECT 'K01','2002-01-01'UNION ALL SELECT 'K01','2003-01-01'DECLARE @Value table (Loc VARCHAR(3), Value NUMERIC(10,2), StartingDate DATETIME)INSERT @ValueSELECT 'K01',1.23,'1995-01-01'UNION ALL SELECT 'K01',1.67,'2001-06-01'UNION ALL SELECT 'K01',2.7,'2004-01-01';WITH MyValue AS( SELECT *,ROW_NUMBER() OVER (ORDER BY StartingDate) AS RowNum FROM @Value )SELECT L.Loc, L.sample_date, T.ValueFROM @Location AS LINNER JOIN ( SELECT A.StartingDate AS StartDate, COALESCE(B.StartingDate, '99991231') AS EndDate, A.Value FROM MyValue AS A LEFT OUTER JOIN MyValue AS B ON A.RowNum + 1 = B.RowNum ) AS T ON L.sample_date >= StartDate AND L.sample_date < EndDate |
 |
|
|
Shrews14
Starting Member
37 Posts |
Posted - 2009-05-01 : 12:48:19
|
quote: Originally posted by Lamprey Here is another way to do this. I tried to break the problem out a little bit so that it *might* be easier to understand (although probably not as effecient as Don's). The first part is a CTE with just orders the Value table by StartingDate. This is then used to LEFT join to itself to get the date ranges. Fianlly, this derived table of date ranges is joined to the Location table to get the Value for that location. Hopefully, it makes sense:DECLARE @Location table (Loc VARCHAR(3), sample_date DATETIME)INSERT @LocationSELECT 'K01','2001-01-01'UNION ALL SELECT 'K01','2002-01-01'UNION ALL SELECT 'K01','2003-01-01'DECLARE @Value table (Loc VARCHAR(3), Value NUMERIC(10,2), StartingDate DATETIME)INSERT @ValueSELECT 'K01',1.23,'1995-01-01'UNION ALL SELECT 'K01',1.67,'2001-06-01'UNION ALL SELECT 'K01',2.7,'2004-01-01';WITH MyValue AS( SELECT *,ROW_NUMBER() OVER (ORDER BY StartingDate) AS RowNum FROM @Value )SELECT L.Loc, L.sample_date, T.ValueFROM @Location AS LINNER JOIN ( SELECT A.StartingDate AS StartDate, COALESCE(B.StartingDate, '99991231') AS EndDate, A.Value FROM MyValue AS A LEFT OUTER JOIN MyValue AS B ON A.RowNum + 1 = B.RowNum ) AS T ON L.sample_date >= StartDate AND L.sample_date < EndDate
That certainly makes sense. Thank you loads. That was the sort of proc i was thinking of but couldn't quite work out in my head!!!!! Plus has the added enddate section which is quite important.Cheers for spending some of your time helping me this should be enough to get me going on this gigantic SPROC i writing. |
 |
|
|
|
|
|
|
|