If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > alais name in the group by clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-09-11, 10:25
thamin thamin is offline
Registered User
 
Join Date: Jul 2011
Posts: 13
alais name in the group by clause

Hello,
I have a situation where I have to use alais name for a field in the group by caluse. But Db2 is not allowing that. Can you please suggest this can be attained.
Below is the situation:
I have 2 tables tab1 and tab2. tab1 has fields A, B, C, D and tab2 has fields E, F, G,H. My query goes like this

SELECT count(*), COALESCE(tab2.G,tab1.C) as TID
FROM tab1
LEFT JOIN tab2 ON
tab2.E=tab1.A and tab2.F=tab1.B
WHERE tab1.d > 20
GROUP BY TID

But this query gives me error saying TID

Thanks,
Thamizh
Reply With Quote
  #2 (permalink)  
Old 09-09-11, 10:39
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by thamin View Post
I have to use alais name for a field in the group by caluse.
Why do you have to use it? Just use "GROUP BY COALESCE(tab2.G,tab1.C)"/
Reply With Quote
  #3 (permalink)  
Old 09-09-11, 10:57
thamin thamin is offline
Registered User
 
Join Date: Jul 2011
Posts: 13
Thank you very much......
Reply With Quote
  #4 (permalink)  
Old 09-09-11, 10:58
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Please see the topic...
subselect - IBM DB2 9.7 for Linux, UNIX, and Windows

You will see...
Quote:
The clauses of the subselect are processed in the following sequence:
1.FROM clause
2.WHERE clause
3.GROUP BY clause
4.HAVING clause
5.SELECT clause
6.ORDER BY clause
7.FETCH FIRST clause
GROUP BY clause is processed before SELECT clause.
So, "AS new-column-name" in SELECT clause are not known in GROUP BY clause.
Reply With Quote
  #5 (permalink)  
Old 09-09-11, 11:17
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Another way may be use of nested-table-expression, like ...
Code:
SELECT count(*) , tid
 FROM  (SELECT COALESCE(tab2.G , tab1.C) AS tid
         FROM  tab1
         LEFT  JOIN
               tab2
          ON   tab2.E = tab1.A
           AND tab2.F = tab1.B
         WHERE tab1.d > 20
       ) s
 GROUP BY tid
;
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On