| Author |
Topic |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-06-06 : 19:00:56
|
The following query produces1,9101,2.002,1213,3.00which is correct, I just was wondering if their was a Better way to do this by possibly using a CTE or someother way method.Please note that what I need to accomplish isto add each Unique val items from @tmp1 which doesnot exist in Tmp. If the item does not exist, Ineed to add it to tmp2 filtering the items ( I justused the date as a example in the live table I have roughly4 columns that I need to filter on).THis is why I chose a top 1 clause to illustrate.Please let me know if there is a better way to get thesereults. Please factor in that I can't use a group by becauseI need to return a specific record (In the scenerio above I use the rate Declare @tmp Table (ID int not null Identity(1,1),Val varchar(4),Rate money)Declare @tmp1 Table (ID int not null Identity(1,1),Val varchar(4),dt datetime,Rate money)Declare @tmp2 Table (ID int not null Identity(1,1),Val varchar(4),Rate money)beginInsert Into @Tmp(Val,Rate)select '1234',5union allselect '5678',6endbeginInsert Into @Tmp1(Val,Rate,Dt)select '1234',1,'01/01/2005'union allselect '5678',2,'01/03/2005'union allselect '5678',3,'01/01/2005'union allselect '9101',2,'01/05/2006'union allselect '1213',2,'01/02/2005'union allselect '1213',1,'01/03/2005'union allselect '1213',3,'01/05/2005'end column to show that I need to get that value tied to the filtered item)*/ |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-06-06 : 19:55:30
|
I don't think your post is complete. You posted tables and sample data, but not the query you already have tried that produces the results you quote. or did you not mean to post it?edit: also, need sample data for your third table elsasoft.org |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-06-06 : 20:22:25
|
AHHHHHHHHH, here's the correct queryDeclare @tmp Table (ID int not null Identity(1,1),Val varchar(4),Rate money)Declare @tmp1 Table (ID int not null Identity(1,1),Val varchar(4),dt datetime,Rate money)Declare @tmp2 Table (ID int not null Identity(1,1),Val varchar(4),Rate money)beginInsert Into @Tmp(Val,Rate)select '1234',5union allselect '5678',6endbeginInsert Into @Tmp1(Val,Rate,Dt)select '1234',1,'01/01/2005'union allselect '5678',2,'01/03/2005'union allselect '5678',3,'01/01/2005'union allselect '9101',2,'01/05/2006'union allselect '1213',2,'01/02/2005'union allselect '1213',1,'01/03/2005'union allselect '1213',3,'01/05/2005'endInsert Into @Tmp2(Val,Rate)Select a.Val,a.Ratefrom @tmp1 awhere not exists (Select * from @Tmp aa where aa.val = a.Val)and a.ID = (Select top 1 ab.ID from @Tmp1 ab where ab.val = a.val order by dt asc)select * from @Tmp2 |
 |
|
|
phdiwakar
Starting Member
15 Posts |
Posted - 2007-06-07 : 15:24:20
|
| I think, it will be good to look at the exact query that you are using currently including the filtering columns. |
 |
|
|
|
|
|