Author |
Topic |
adit
Starting Member
8 Posts |
Posted - 2012-03-12 : 12:19:59
|
Hi,My data is as below:NEW_ID SOURCE_ID ACTIVE_ID1 1 31 2 31 3 32 1 42 2 42 4 4I am joining this table based on source_id and fetching new_id values. So for source_id=1, I get two distinct new_id values - 1 & 2.But I need new_id=1 if i fetch source_id=1 from active_id=3 pathand new_id=2 if i fetch source_id=1 from active_id=4 path.How can I do this? Should I alter my join condition or will have to do group by or something? Pls help here.Thanks. |
|
KlausEngel
Yak Posting Veteran
85 Posts |
Posted - 2012-03-12 : 12:32:23
|
Can you post both tables and your query? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-12 : 12:34:44
|
its just a matter of adding the extra filter on Active_IDDECLARE @ActiveID int,@SourceID intSELECT @ActiveID = 4,@SourceID = 1SELECT *FROM tableWHERE Source_ID = @SourceIDAND ACTIVE_ID = @ActiveID just change the value of @ActiveID and @SourceID to see how output varies------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
adit
Starting Member
8 Posts |
Posted - 2012-03-12 : 13:21:29
|
Thanks for responding.My query is below:select a.e_id,b.l_name,c.new_idfrom ainner join b on a.s_id=b.s_idinner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_idHere c corresponds to the table posted. Also there is one other table 'd' which contains the field - 'active_id' from where in data flows to 'active_id' column of table c.Thanks. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-12 : 13:59:28
|
quote: Originally posted by adit Thanks for responding.My query is below:select a.e_id,b.l_name,c.new_idfrom ainner join b on a.s_id=b.s_idinner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_idwhere c.source_id = <yourvalue>and c.active_id = <activeidvalue>Here c corresponds to the table posted. Also there is one other table 'd' which contains the field - 'active_id' from where in data flows to 'active_id' column of table c.Thanks.
its still the same------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
adit
Starting Member
8 Posts |
Posted - 2012-03-13 : 16:11:39
|
Hi,Can you please suggest a way I can do it by query instead of stored procedure with cursors, because the data is very huge and I have to map it the way as described. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-13 : 23:06:38
|
quote: Originally posted by adit Hi,Can you please suggest a way I can do it by query instead of stored procedure with cursors, because the data is very huge and I have to map it the way as described.
there's no need for cursor. you can just have parameters for passing values and create a procedure with themlikeCREATE PROC MyProc@source_id int,@active_id intASselect a.e_id,b.l_name,c.new_idfrom ainner join b on a.s_id=b.s_idinner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_idwhere c.source_id = @source_id and c.active_id = @active_id GO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
adit
Starting Member
8 Posts |
Posted - 2012-03-14 : 13:00:12
|
quote: Originally posted by visakh16
quote: Originally posted by adit Hi,Can you please suggest a way I can do it by query instead of stored procedure with cursors, because the data is very huge and I have to map it the way as described.
there's no need for cursor. you can just have parameters for passing values and create a procedure with themlikeCREATE PROC MyProc@source_id int,@active_id intASselect a.e_id,b.l_name,c.new_idfrom ainner join b on a.s_id=b.s_idinner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_idwhere c.source_id = @source_id and c.active_id = @active_id GO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I am sorry if i did not get you, but actually i need the query to run on all distinct active_id's in the table. the data posted is only a sample. without a cursor, how can we pass values to parameters? |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-14 : 15:47:45
|
quote: Originally posted by adit
quote: Originally posted by visakh16
quote: Originally posted by adit Hi,Can you please suggest a way I can do it by query instead of stored procedure with cursors, because the data is very huge and I have to map it the way as described.
there's no need for cursor. you can just have parameters for passing values and create a procedure with themlikeCREATE PROC MyProc@source_id int,@active_id intASselect a.e_id,b.l_name,c.new_idfrom ainner join b on a.s_id=b.s_idinner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_idwhere c.source_id = @source_id and c.active_id = @active_id GO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I am sorry if i did not get you, but actually i need the query to run on all distinct active_id's in the table. the data posted is only a sample. without a cursor, how can we pass values to parameters?
just pass likeEXEC MyProc sourceidvalue,activeidvalue------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|