473,382 Members | 1,404 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,382 software developers and data experts.

DISTINCT and ORDER BY

Colleagues:

Take a query like

SELECT
DISTINCT
X.COLA,
X.COLB
FROM
X
WHERE
X.COLN = 'CONSTANT'
ORDER BY
1,2

Can I get rid of the ORDER BY, as DB2 will sort the results in the
order I'm specifying to eliminate duplicates? Does DB2 do this anyway
as an optimization during re-write?

Env.: DB2 UDB LUW 8.2.3, AIX 5.x

Thanks,

--Jeff

Aug 1 '06 #1
7 15612
jefftyzzer wrote:
Colleagues:

Take a query like

SELECT
DISTINCT
X.COLA,
X.COLB
FROM
X
WHERE
X.COLN = 'CONSTANT'
ORDER BY
1,2

Can I get rid of the ORDER BY, as DB2 will sort the results in the
order I'm specifying to eliminate duplicates? Does DB2 do this anyway
as an optimization during re-write?

Env.: DB2 UDB LUW 8.2.3, AIX 5.x
You cannot get rid of the order by.
Not to worry. DB2 will not re-order if the distinct already caused a sort.

Here are three reasons why you can't remove the order by:
1. If you ran this query on a DPF system you would not get an ordered
result back because the DISTINCT would likely be done per node.
2. Future versions may use different technology to enforce DISTINCTness
than sort.
One such example would be hashing.
3. You could have an index on (COLB, COLA). DB2 may use that to enforce
the distinct and then resort the remaining (smaller) result set for the
ORDER BY.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Aug 1 '06 #2
Serge, thanks as always for your help and your absurdly-fast replies
:-).

Serge Rielau wrote:
jefftyzzer wrote:
Colleagues:

Take a query like

SELECT
DISTINCT
X.COLA,
X.COLB
FROM
X
WHERE
X.COLN = 'CONSTANT'
ORDER BY
1,2

Can I get rid of the ORDER BY, as DB2 will sort the results in the
order I'm specifying to eliminate duplicates? Does DB2 do this anyway
as an optimization during re-write?

Env.: DB2 UDB LUW 8.2.3, AIX 5.x
You cannot get rid of the order by.
Not to worry. DB2 will not re-order if the distinct already caused a sort.

Here are three reasons why you can't remove the order by:
1. If you ran this query on a DPF system you would not get an ordered
result back because the DISTINCT would likely be done per node.
2. Future versions may use different technology to enforce DISTINCTness
than sort.
One such example would be hashing.
3. You could have an index on (COLB, COLA). DB2 may use that to enforce
the distinct and then resort the remaining (smaller) result set for the
ORDER BY.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Aug 1 '06 #3
jefftyzzer wrote:
Serge, thanks as always for your help and your absurdly-fast replies
Don't tell my boss. He'll think I have nothing better to do ;-)
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Aug 1 '06 #4
jefftyzzer wrote:
Colleagues:

Take a query like

SELECT
DISTINCT
X.COLA,
X.COLB
FROM
X
WHERE
X.COLN = 'CONSTANT'
ORDER BY
1,2

Can I get rid of the ORDER BY, as DB2 will sort the results in the
order I'm specifying to eliminate duplicates?
The _only_ thing that guarantees the ordering of the results is the ORDER
BY. Every other behavior is just happenstance and you cannot rely on it.
So the order by is mandatory.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Aug 2 '06 #5
In addition to what Serge said, you should also stop writing positional
numbers in the ORDER BY -- use column names in case the table is
altered or you have to port the code.

Aug 2 '06 #6
"--CELKO--" <jc*******@earthlink.netwrote in message
news:11**********************@s13g2000cwa.googlegr oups.com...
In addition to what Serge said, you should also stop writing positional
numbers in the ORDER BY -- use column names in case the table is
altered or you have to port the code.
The column position only refers to the position within the select list of a
particular query, and is not affected by the columns in the table.
Aug 2 '06 #7
>The column position only refers to the position within the select list of a particular query, and is not affected by the columns in the table.<<

I was on ANSI X3H2 when we deprecated positional numbers. I also hope
you know that ORDER BY is never part of a query; it is a clause in a
CURSOR. When the query changes, so do the positions in the cursor.

But more than that, it is simply baaaad programming not to explicitly
tell the poor bastard who maintains the code what the sort order really
is.

Aug 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: blue | last post by:
I'm trying to order a varchar column first numerically, and second alphanumerically using the following SQL: SELECT distinct doc_number FROM doc_line WHERE product_id = 'WD' AND doc_type = 'O'...
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: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the...
9
by: Kelvin | last post by:
Okay so this is baking my noodle. I want to select all the attritbutes/fields from a table but then to excluded any row in which a single attributes data has been duplicated. I.E. Here's my...
6
by: John M | last post by:
Hi, The line below is used to feed a combobox. (It is from a database which is used to log pupil behaviour!) The 'incidents' table contains a list of students who have been involved in incidents....
5
by: Fred Zuckerman | last post by:
Can someone explain the difference between these 2 queries? "Select Distinct id, account, lastname, firstname from table1" and "Select DistinctRow id, account, lastname, firstname from table1" ...
6
by: Bob Stearns | last post by:
I am getting unwanted duplicate rows in my result set, so I added the DISTINCT keyword to my outermost SELECT. My working query then returned the following message: DB2 SQL error: SQLCODE: -214,...
4
by: monomaniac21 | last post by:
hi! is it possible to do the aforementioned query - selecting only distinct in 1 col but retrieving all other cols at the same time. regards marc
2
by: Techhead | last post by:
I need to run a SELECT DISTINCT query across multiple fields, but I need to add another field that is NON-DISTINCT to my record set. Here is my query: SELECT DISTINCT lastname, firstname,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...

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.