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.
Hello,I have a table of words and a table of definitions. the table ofdefinitions can have multiple records per word. I would like to getthe record with the lowest ID from the defs table for each word andjoin them so I have id, word, defwhen I try this it doesn't work right, I only get the same definitionfor all the wordsselect id, word, def = (select top 1 def from defs where word_id = id)FROM wordswords table has id, worddefs table has word_id, defany help would be appreciated!Mike
Lamprey
Master Smack Fu Yak Hacker
4614 Posts
Posted - 2010-08-13 : 13:29:57
I'm finding your data model confusing (or the description of what you want out of it). Perhaps some sample data and expected output would help. Here is a link that can help you prepare that information so we can run queries against it:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Humate
Posting Yak Master
101 Posts
Posted - 2010-08-13 : 17:51:50
like this?
SELECT w.id, w.word, z.defFROM words wINNER JOIN (SELECT * FROM (SELECT word_id, def, ROW_NUMBER() OVER(PARTITION BY word_id ORDER BY word_id) AS RecID FROM defs) AS d WHERE RecID = 1) AS z ON w.id = z.word_id
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2010-08-14 : 01:28:53
[code]select w.id, w.word, d.def FROM words wCROSS APPLY (select top 1 def from defs where word_id = w.id order by id asc)d[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/