IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Query to select Null row on priority


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Thu Jan 28, 2016 9:13 am
Reply with quote

Rohit Umarjikar wrote:
Yes ,so Group by on first column should work then.


If he wants to select all Columns then he will need to group by on all columns which I feel will be the overhead.

CASE function will also work same as COALESCE.

@Mistah..is it possible for you to provide queries and output for both using ORDER BY DATE_COLUMN and ORDER BY 2

Thanks,
Chandan
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3051
Location: NYC,USA

PostPosted: Fri Jan 29, 2016 3:01 am
Reply with quote

Let us not guess how many columns he has in select.

However , this should work.

Code:
SELECT Key_Column, COALESCE(Date_Column,'0001-01-01') mod_dt
 FROM Date_table
 WHERE Key_Column = 'AA'
 ORDER BY [i]mod_dt[/i] ASC
 FETCH FIRST ROW ONLY

Quote:

@Mistah..is it possible for you to provide queries and output for both using ORDER BY DATE_COLUMN and ORDER BY 2


Results would be different, Null is lower which comes first .
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Fri Jan 29, 2016 9:07 am
Reply with quote

Rohit Umarjikar wrote:


Results would be different, Null is lower which comes first .


I guess you have missed below post from Enrico

Quote:
Ordering is performed in accordance with the comparison rules described in Language elements. The null value is higher than all other values. If your ordering specification does not determine a complete ordering, rows with duplicate values of the last identified sort-key have an arbitrary order. If you do not specify ORDER BY, the rows of the result table have an arbitrary order.


Also the solution is provided by you is the same as per provided by me and both will work, but as per Mistah it's not working for him

Anyways this post is going way longer, let wait for Mistah to get back


Thanks and regards,
Chandan
Back to top
View user's profile Send private message
mistah kurtz

Active User


Joined: 28 Jan 2012
Posts: 316
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Fri Jan 29, 2016 1:18 pm
Reply with quote

Hi Chandan

Sorry for the delay and confusion. I have restricted access to internet, so could not reply earlier. ORDER BY 2 is working just as I needed.

So as I understand the deifference is:
ORDER BY 2 ASC - ordering is done after the values are updated by COALESCE function
ORDER BY Date_Column ASC - ordering is done before COALESCE function is applied.

However if I don't use the COALESCE function, resulting rows will be same in both the cases.

Thanks a lot. It was new to me.
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Fri Jan 29, 2016 1:47 pm
Reply with quote

No worries. Glad to know its working for you and I could help
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Null values are considered in Total c... DFSORT/ICETOOL 6
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
Search our Forums:

Back to Top