Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / April 2006

Tip: Looking for answers? Try searching our database.

Error on adding DISTINCT to a SELECT DB2 UDB v8.1.9 Linux

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Stearns - 19 Apr 2006 22:28 GMT
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, SQLSTATE: 42822, SQLERRMC: CASE...;ORDER BY;2
 Message: An expression in the ORDER BY clause in the following
position, or starting with "CASE..." in the "ORDER BY" clause is not
valid.  Reason code = "2". 

 More exceptions ... DB2 SQL error: SQLCODE: -727, SQLSTATE: 56098,
SQLERRMC: 2;-214;42822;CASE...|ORDER BY|2

The ORDER BY clause is below:

 ORDER BY CASE
              WHEN t01.lot_numb IS NULL AND
                   t01.lot_suffix IS NOT NULL
              THEN 0
              ELSE t01.lot_numb
         END,
          COALESCE(t01.lot_suffix, ''),
          animal_id

I didn't include the whole query because of its length.

My problem is that I don't understand DISTINCT's effect on ORDER BY. The
clause works when there is no DISTINCT.
Serge Rielau - 19 Apr 2006 22:55 GMT
> 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
[quoted text clipped - 23 lines]
> My problem is that I don't understand DISTINCT's effect on ORDER BY. The
> clause works when there is no DISTINCT.
I take a guess and assume that perhaps some of the columns/expressions
in the order by are not in the select list. That's why Db2 is cranky on
the DISTINCT. The hidden columns get in the way.
Push the DISTINCT into a subquery an then ORDER the result:
SELECT ... FROM (SELECT DISTINCT....) AS X
ORDER BY ...

Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Bob Stearns - 19 Apr 2006 23:26 GMT
>> I am getting unwanted duplicate rows in my result set, so I added the
>> DISTINCT keyword to my outermost SELECT. My working query then
[quoted text clipped - 31 lines]
> SELECT ... FROM (SELECT DISTINCT....) AS X
> ORDER BY ...

You got it in one. I'm selecting functions of t01.lot_numb and
t01.lot_suffix, but not those columns by themselves.

I had the sql reference manual open to page 555 ff. (for v8) and saw no
mention of having order by columns in the select list when distinct is
used. While the message correct in this case, it could be more informative.
Serge Rielau - 20 Apr 2006 01:21 GMT
>>> I am getting unwanted duplicate rows in my result set, so I added the
>>> DISTINCT keyword to my outermost SELECT. My working query then
[quoted text clipped - 38 lines]
> mention of having order by columns in the select list when distinct is
> used. While the message correct in this case, it could be more informative.
Punch the feedback button on the topic of the DB2 zOS information center :-)

Cheers
Serge

Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Bob Stearns - 20 Apr 2006 05:06 GMT
>>>> I am getting unwanted duplicate rows in my result set, so I added
>>>> the DISTINCT keyword to my outermost SELECT. My working query then
[quoted text clipped - 46 lines]
> Cheers
> Serge

I must have the wrong URL. I see no feedback button (but it's late). The
URL I'm using is:

http://publib.boulder.ibm.com/infocenter/db2luw/v8//index.jsp
Gert van der Kooij - 20 Apr 2006 08:37 GMT
> I must have the wrong URL. I see no feedback button (but it's late). The
> URL I'm using is:
>
> http://publib.boulder.ibm.com/infocenter/db2luw/v8//index.jsp

At the bottom of that page are a couple of links, one of them is the
feedback link.
Serge Rielau - 20 Apr 2006 12:11 GMT
>> I must have the wrong URL. I see no feedback button (but it's late). The
>> URL I'm using is:
[quoted text clipped - 3 lines]
> At the bottom of that page are a couple of links, one of them is the
> feedback link.
FYI, in DB2 Viper there actually is a button at the topic level I believe.

Cheers
Serge
Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.