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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Filter records based on another column value

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_ID
1 1 3
1 2 3
1 3 3
2 1 4
2 2 4
2 4 4

I 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 path
and 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?
Go to Top of Page

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_ID


DECLARE @ActiveID int,@SourceID int

SELECT @ActiveID = 4,@SourceID = 1


SELECT *
FROM table
WHERE Source_ID = @SourceID
AND ACTIVE_ID = @ActiveID


just change the value of @ActiveID and @SourceID to see how output varies

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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_id
from a
inner join b on a.s_id=b.s_id
inner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_id

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.
Go to Top of Page

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_id
from a
inner join b on a.s_id=b.s_id
inner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_id
where 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-13 : 17:16:25
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.



Your question is not clear. Nobody said to use a cursor.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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 them

like


CREATE PROC MyProc
@source_id int,
@active_id int
AS
select a.e_id,b.l_name,c.new_id
from a
inner join b on a.s_id=b.s_id
inner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_id
where c.source_id = @source_id
and c.active_id = @active_id
GO


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 them

like


CREATE PROC MyProc
@source_id int,
@active_id int
AS
select a.e_id,b.l_name,c.new_id
from a
inner join b on a.s_id=b.s_id
inner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_id
where c.source_id = @source_id
and c.active_id = @active_id
GO


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://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?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-14 : 13:24:36
Can't you just remove active_id from the query so that it runs against the whole thing?

CREATE PROC MyProc
@source_id int
AS
select a.e_id,b.l_name,c.new_id
from a
inner join b on a.s_id=b.s_id
inner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_id
where c.source_id = @source_id
GO

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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 them

like


CREATE PROC MyProc
@source_id int,
@active_id int
AS
select a.e_id,b.l_name,c.new_id
from a
inner join b on a.s_id=b.s_id
inner join c on b.l_id=c.l_id and b.r_id=c.r_id and b.source_id=c.source_id
where c.source_id = @source_id
and c.active_id = @active_id
GO


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://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 like
EXEC MyProc sourceidvalue,activeidvalue

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -