- Joined
- Jul 30, 2008
- Posts
- 635
- Reaction score
- 8
I am actually using SQL Server 2005, but I am sure this is such a fundamental simple problem that any Transact SQL would do. If anyone knows the answer I'd really appreciate the help - I am missing something very simple here!
I have 3 or more tables with various columns, but all having 1 column in common. I want to perform a query that picks the top x records, or from x+1 to y, from table 1 when ordered by the common column, then data from table 2 and table 3 using the same column. I can do it with 2 tables using Top and a Join, but struggle extending it beyond 2 tables. So I currently have:
SELECT TOP 6 table1.CommonColumn, table1.AnotherColumn, table2.DifferentColumn1, table2.DifferentColumn2
FROM table1
Join table2
On table1.CommonColumn = table2.CommonColumn
WHERE table1.SomeColumn = Value
AND table1.CommonColumn NOT IN (SELECT TOP 12 table1.CommonColumn
FROM table1
WHERE table1.SomeColumn = Value)
order by table1.CommonColumn
I need to extend it to return table3.DifferentColumn3, table3.DifferentColumn4 with table1.CommonColumn = table3.CommonColumn. It's POETS day and I would rather be sloping off to the pub for a sneaky pint. Any help very gratefully received....
I have 3 or more tables with various columns, but all having 1 column in common. I want to perform a query that picks the top x records, or from x+1 to y, from table 1 when ordered by the common column, then data from table 2 and table 3 using the same column. I can do it with 2 tables using Top and a Join, but struggle extending it beyond 2 tables. So I currently have:
SELECT TOP 6 table1.CommonColumn, table1.AnotherColumn, table2.DifferentColumn1, table2.DifferentColumn2
FROM table1
Join table2
On table1.CommonColumn = table2.CommonColumn
WHERE table1.SomeColumn = Value
AND table1.CommonColumn NOT IN (SELECT TOP 12 table1.CommonColumn
FROM table1
WHERE table1.SomeColumn = Value)
order by table1.CommonColumn
I need to extend it to return table3.DifferentColumn3, table3.DifferentColumn4 with table1.CommonColumn = table3.CommonColumn. It's POETS day and I would rather be sloping off to the pub for a sneaky pint. Any help very gratefully received....