473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Selecting top 10 results using DISTINCT

Hi,

Can anyone confirm whether it is possible to:

a) use subqueries in mySQL
b) use DISTINCT TOP in mySQL?

I am trying to find a way to return the top 5 results in my table. For simplicity sake:

user I hits I date

for one user, there may be several records but with different 'hits' values. e.g

userA I 15 I ddmmyy
userB I 22 I ddmmyy
userC I 20 I ddmmyy
userA I 13 I ddmmyy
userM I 17 I ddmmyy
userB I 26 I ddmmyy

So, how would extract the top 10 users but NOT repeating a user that may have duplicate records? So i only want to return the distinct 'user' value, and skip them if they occur again. i.e. i would end up with the following ranked by hits:

user I hits I date
userB 26
userC 20
userM 17
userA 15

any help with achieving this in mySQL would be fantastic. the alternative might be to force this behaviour in the application code should it not be poss in mySQL

many thanks
m
Mar 5 '07 #1
5 13744
Hi
Can anyone help me with this? So far no comments. Thanks[


QUOTE=mikevde]Hi,

Can anyone confirm whether it is possible to:

a) use subqueries in mySQL
b) use DISTINCT TOP in mySQL?

I am trying to find a way to return the top 5 results in my table. For simplicity sake:

user I hits I date

for one user, there may be several records but with different 'hits' values. e.g

userA I 15 I ddmmyy
userB I 22 I ddmmyy
userC I 20 I ddmmyy
userA I 13 I ddmmyy
userM I 17 I ddmmyy
userB I 26 I ddmmyy

So, how would extract the top 10 users but NOT repeating a user that may have duplicate records? So i only want to return the distinct 'user' value, and skip them if they occur again. i.e. i would end up with the following ranked by hits:

user I hits I date
userB 26
userC 20
userM 17
userA 15

any help with achieving this in mySQL would be fantastic. the alternative might be to force this behaviour in the application code should it not be poss in mySQL

many thanks
m[/quote]
Mar 7 '07 #2
gauravgmbhr
107 100+
TRY THIS INSTEAD OF distinct top 10
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM `some table`
  3. WHERE condition
  4. Order BY `field that decides the top entries`
  5. LIMIT 10;
or try

SELECT *
FROM `ur table`
WHERE (if condition exists)
ORDER BY `hits`
LIMIT 5

dont use distint coz distinct will scan the whole table
plus in ur case if u use distinct ur users with same number of hits as some other user ,will not come in ur list
Mar 8 '07 #3
Anu1
1
Hi,

Can anyone help me to get the same with SQLServer? Limit is not applicable in SQLServer.

Thanks,
Anu
Mar 22 '07 #4
gauravgmbhr
107 100+
Hi,

Can anyone help me to get the same with SQLServer? Limit is not applicable in SQLServer.

Thanks,
Anu

Well In SQl SERVER u can use the top clause

TRY THIS

SELECT TOP 5 * FROM 'urtable'
WHERE [Some Condition];


If U wanna read More
Try this
Mar 23 '07 #5
sdlogy
1
Hey....thats an interesting thing actually........never thought of it........
Try this........

First Create a view selecting the distinct values from the table.....

create view <<View name>>
As
Select distinct from <<table name>>
where
<<filtering conditions if any>>;

Then select top 10 or 5 values from the view.

Select top 10 * from <<view name>> (nolock)
order by <<required column name >>

That way I guess you can ensure every time you select the values, you get the distinct value.

Not sure if this works......please let me know as well after trying!!


Havent used the limit command......will check it out.....

Cheers
Jul 18 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: MasterChief | last post by:
I have a Drop Down box that is being populated using the SQL Distinct command. How do I get it so it doesn't show Null values? Here is my SQL statement sSQL = "SELECT DISTINCT Title FROM...
4
by: Johnson, Shaunn | last post by:
Howdy: Can someone tell what the difference (and why you would use it) is between the following: select distinct on (col_1, col_2), col_1, col_2, col_3
1
by: Patrick.O.Ige | last post by:
I'm trying to do distinct below in a datatable but i get the error :- Syntax error: Missing operand after 'Code' operator. Any ideas? string strExpr = "DISTINCT Code LIKE '%ve%'" ; DataRow...
0
by: Benny | last post by:
I am trying to figure out the best way to create a filtering user control. I have 3 fields I am using and therefore have 3 combobox's that all need to display distinct values. The resulting...
2
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I have the following line of code (.NET 2.0): myDataView.RowFilter = "SELECT DISTINCT CYCLE_DATE"; where CYCLE_DATE is one of my column names. This throws the following exception: Syntax...
8
by: bnashenas1984 | last post by:
I'm using DISTINCT to get one of each title out of my table. Here is my table: +---------------------------+ | title | weather | +---------------------------+ | Monday | ...
5
by: valeron777 | last post by:
hi all, I am working on outputting data to be read into excel from access DB. The situation now is that im having problems filtering off rows of data which are the same but only having 1 field...
4
by: omnittha | last post by:
I would like to pull unique records of a SINGLE column but not the other related columns. When I use DISTINCT, it pulls up all the records which are unique across the columns. Here is the code.......
8
by: Jesse Jones | last post by:
I am successfully calling around half a million records, but I have a problem with duplicate records. Without using the DISTINCT function I can call these records in half a second, but when using the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.