Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
HelloI have a problem that I have no idea how to solveI have a table with 2 columnsUserID and CarIDI need the ability to provide a CarID and in return select all the CarIDs that the users who bought the carID I provided boughtfor exampleUserID CarID1 102 101 122 18when I provide CarID 10 I want my select to return 10,12,18is it possible ?? maybe in a stored procedure ??any help would be greatly appriciated
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2009-02-25 : 16:34:21
Yes, it is doable.The question is how long do you want the "evidence" chain to be?
DECLARE @Sample TABLE ( UserID INT, CarID INT )INSERT @SampleSELECT 1, 10 UNION ALLSELECT 2, 10 UNION ALLSELECT 1, 12 UNION ALLSELECT 2, 18DECLARE @CarID INTSET @CarID = 10SELECT DISTINCT s2.CarIDFROM @Sample AS s1INNER JOIN @Sample AS s2 ON s2.UserID = s1.UserIDWHERE s1.CarID = @CarID
E 12°55'05.63"N 56°04'39.26"
beza
Starting Member
16 Posts
Posted - 2009-02-25 : 16:48:26
First of all thank you for the quick replayI didn't understand what did you mean byThe question is how long do you want the "evidence" chain to be?second ... can you explain what did you do in your stored procedureI want it to be generic ... I used the numbers only for the exampleThanks a lot!!!
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2009-02-25 : 17:00:02
[code]CREATE PROCEDURE dbo.spMySearch( @CarID INT)ASSET NOCOUNT ONSELECT DISTINCT s2.CarIDFROM Table1 AS s1INNER JOIN Table1 AS s2 ON s2.UserID = s1.UserIDWHERE s1.CarID = @CarID[/code]E 12°55'05.63"N 56°04'39.26"