Link to home
Start Free TrialLog in
Avatar of detox1978
detox1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

TSQL: SELECT data based on another SELECT Statement

I have a select satement below;

select * from
(select *, row_number() over(order by [ID]) seq from [MyDatabase].[dbo].[MyTable]) x
where seq between 16 and 30

How do i filter the results above so that only ID are displayed that are also in the statment below?

SELECT [ID] FROM [MyDatabase].[dbo].[MySecondTable]) Where [best]='5'
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Sharath S
Sharath S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of detox1978

ASKER

Sharath_123 your's works, but i've made a mistake.


Currently it returns row 16 and 30 and filters them for best=5.


I need it to return all rows that are best=5 and filter the rows 16 to 30 fromt the results
Actually both work, but filter the results for best after they are returned.
check this one

select * from (
SELECT x.*,row_number() over(order by [x.ID]) seq
  from [MyDatabase].[dbo].[MyTable]) x
  JOIN [MyDatabase].[dbo].[MySecondTable] y on x.ID = y.ID 
 where y.[best] = '5')
where seq between 16 and 30

Open in new window

I guess ID is unique in both tables.
ID is unique in MyTable, but has multiples (or none)  in MySecondTable
I think there is an extra ) in the last snippet you posted

Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'.
try this

select * from
(select *, row_number() over(order by [ID]) seq from [MyDatabase].[dbo].[MyTable] A
join  [MyDatabase].[dbo].[MySecondTable] B
on B.[best] ='5'
and A.[ID} = B.[ID]) X
where seq between 16 and 30
previous one may give error try this

select * from
(select *, row_number() over(order by A.[ID]) seq from [MyDatabase].[dbo].[MyTable] A
join  [MyDatabase].[dbo].[MySecondTable] B
on B.[best] ='5'
and A.[ID} = B.[ID]) X
where seq between 16 and 30
The error has now moved to line 5
select * from
(select *, row_number() over(order by [ID]) seq from [MyDatabase].[dbo].[MyTable] A
join  [MyDatabase].[dbo].[MySecondTable] B
on B.[best] ='5'
and A.[ID} = B.[ID]) X
where seq between 16 and 30

Open in new window

Msg 4145, Level 15, State 1, Line 5
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
check this one
select * from (
SELECT x.*,row_number() over(order by [x.ID]) seq
  from [MyDatabase].[dbo].[MyTable] x
  JOIN [MyDatabase].[dbo].[MySecondTable] y on x.ID = y.ID 
 where y.[best] = '5') t1
where seq between 16 and 30

Open in new window

I still get an error.  Upped to 500 points....
Msg 207, Level 16, State 1, Line 2
Invalid column name 'x.ID'.

Open in new window

Do you have ID column in MyTable?
Sorry that should have said;

Msg 207, Level 16, State 1, Line 2
Invalid column name 'x.QuestionID'.


and yes there is a column called QuestionID
ignore post above.


yes there is a column called ID
I didn't use QuestionID in my query. then how can you get that error? Can  you post your exact error message for my query (Post ID: 23666666)
Msg 207, Level 16, State 1, Line 2
Invalid column name 'x.ID'.
can you post the column names of MyTable and MySecondTable
Sorted it myself, thanks for the pointers...