question

2ramgopal avatar image
2ramgopal asked

How to select rows from a table from other table key column value +1

Hi, I just went through a situation where there are 2 tables and I need a select rows from first table based on the key column of second table incremented by 1. For example: Table1: 1 hari 123 2 hari2 321 3 hari3 432 4 hari4 234 5 hari5 543 Table2: 1 hari 123 4 hari4 234 The result selection must display 2 and 5 i.e.. 2 hari2 321 5 hari5 543 Please share your answers. Thanks, Ram
select
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Grant Fritchey avatar image
Grant Fritchey answered
You can just put that right into the JOIN criteria: SELECT * FROM dbo.Table1 AS t1 JOIN dbo.Table2 AS t2 ON t1.ID +1 = t2.ID; Now, that does act as a function on the column, so it may result in scans. Test it to be sure.
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

2ramgopal avatar image 2ramgopal commented ·
Thanks for your response Fritchey. My expectation is it should pick 1 nd 4 key values from table 1 and display 2 and 5 from first table.
0 Likes 0 ·
2ramgopal avatar image 2ramgopal commented ·
It worked. Did some minor changes based on my data and got it.Thanks Fritchey.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.