|
|
Subject:
Select top N per unique columnName in mySQL
Category: Computers > Programming Asked by: lticket-ga List Price: $4.50 |
Posted:
05 Jun 2006 02:54 PDT
Expires: 07 Jun 2006 06:31 PDT Question ID: 735386 |
I'm looking for a mySQL query which will give me N records per unique columnName. Consider the following table; Name, Company, Other info... Dave, RBS, x Rob, RBS, x Jim, RBS, x Bob, RBS, x Karen, RBS, x Dave, Digex, x Lee, Digex, x Mike, FDR, x Wayne, FDR, x Sarah, FDR, x Yasmin, FDR, x I would expect the query to return; Dave, Rob, Jim, Dave, Lee, Mike, Wayne and Sarah (and their accompanying company/otherInfo) I've managed to achieve this is Microsoft Access using the following SQL query; SELECT Company , Name , OtherStuff FROM someTable WHERE DCount("name","someTable","Name <= '" & [name] & "' AND company = '" & [company] & "'")<=3 ORDER BY Company, Name; but now want to do it in mySQL (i'm using version 4.1.10) it would be a bonus too if anyone knows a way of doing it in SQL Server or Oracle. I do not want to use; a) a php script with a for each loop b) a stored procedure Hope you can help! Thanks |
|
There is no answer at this time. |
|
Subject:
Re: Select top N per unique columnName in mySQL
From: neomeneo-ga on 06 Jun 2006 10:40 PDT |
I'm just playing with google answers ... it turns out I have an answer for your question :) I have to post it as a comment because the registration for researcher accounts is closed. SET @a:=0;SELECT p.*, (@a:=@a+1) AS rownum FROM person p GROUP BY company, rownum HAVING rownum <= 3 |
Subject:
Re: Select top N per unique columnName in mySQL
From: neomeneo-ga on 06 Jun 2006 10:42 PDT |
actually ejemm ... my previous post is not a complete answer for your question, altough I hope it will help ;) |
Subject:
Re: Select top N per unique columnName in mySQL
From: neomeneo-ga on 06 Jun 2006 10:47 PDT |
any how, here is the complete answer: SET @a:=0;SELECT p.*, IF(@c=p.company, @a:=@a+1, @a:=1) AS rownum, (@c:=p.company) FROM person p GROUP BY company, rownum HAVING rownum <= 3 |
Subject:
Re: Select top N per unique columnName in mySQL
From: lticket-ga on 07 Jun 2006 01:49 PDT |
Thanks neomeneo! That looks like it is on the right lines but doesn't appear to be returning what I want. I'm just getting 1 row from each company- though it does look a bit like it could be a bug in phpMyAdmin as it says; Showing rows 30 - 59 (123 total, Query took 0.0408 sec) ...and yet there are only 11records on the screen (plus the 30records from page 1)... so 41 records (1/3rd the amount should be returned?) I'll see what happens if i execute from php or a gui mySQL front-end. |
Subject:
Re: Select top N per unique columnName in mySQL
From: lticket-ga on 07 Jun 2006 06:31 PDT |
Yessssssssssss! I just cracked it! Many, many, many thanks neomeneo, couldn't have done it without your help! SET @c:=0; SET @a:=0; SELECT p.*, IF(@c=p.company, @a:=@a+1, @a:=1) AS rownum, (@c:=p.company) FROM person p GROUP BY company, rownum HAVING rownum <= 3 I looked and @c was returning NULL for every record because it hadn't been dimmed i guess? Work now though! Awesome! |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |