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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 distinct. I want to sort it before i do distinct

Author  Topic 

sqldonkey
Starting Member

2 Posts

Posted - 2007-08-28 : 20:29:50
Hello,

I am trying to run a select query but it seems like when i try to sort by las (which is basically a number) i am getting inaccurate results.

It seems like I need to sort the data by las first then select with the distinct statement.

Can anyone make any suggestions?

Thanks

$result2 = $db->query('SELECT (name),id1 FROM table1 JOIN table2 on table2.ked = table1.id1 order by las DESC limit 20');

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-08-28 : 20:49:11
Is it t-sql?
Go to Top of Page

sqldonkey
Starting Member

2 Posts

Posted - 2007-08-28 : 21:04:49
I dont know.....my server has an sql link in the cpanel, i click it then select phpmyadmin and thats it.

Does that info help?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-29 : 01:49:01
SELECT TOP 20 Name, MAX(ID) AS ID FROM Table1 INNER JOIN Table2 ON Table2.KED = Table1.KED GROUP BY Name ORDER BY 2 DESC, 1

If this is not what you want, please provide proper sample data and expected output.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-08-29 : 06:54:00
Can you do something like this? (this would be possible in SQL Server)

SELECT *
FROM
(
SELECT DISTINCT Col1, COl2, ...
FROM MyTable
) AS X
ORDER BY las

Note that you can NOT do this:

SELECT DISTINCT Col1, Col2
FROM MyTable
ORDER BY ColX

because if you use DISTINCT then ColX MUST be in the column list of the SELECT, which you could get around [I think!] with SQL Server using:

SELECT Col1, Col2 -- No LAS column here
FROM
(
SELECT Col1, Col2, las
FROM MyTable
ORDER BY las
) AS X

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-29 : 07:36:44
quote:
Originally posted by sqldonkey

Hello,

I am trying to run a select query but it seems like when i try to sort by las (which is basically a number) i am getting inaccurate results.

It seems like I need to sort the data by las first then select with the distinct statement.

Can anyone make any suggestions?

Thanks

$result2 = $db->query('SELECT (name),id1 FROM table1 JOIN table2 on table2.ked = table1.id1 order by las DESC limit 20');


If you dont use SQL Server then post at www.dbforums.com

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -