Re: UNION ALL and GROUP BY
Posted in 1998
Pedro Antonio de Alarcon wrote: >Hi all, >Does anyone know how to apply a "group by" clause to a set of rows >derived from a UNION ALL operation?? >For instance, >Table A ( > id integer, > ... > ... >) > >(select id from A where condition_1) UNION ALL (select id from A where >condition_2) >, the ids we get from this statements are 1,1,2,2,3,4,4,4,5. >How to use "group by id" with this example? > >Thank you in advance. Pedro If you needn't any other columns but just unique id's you could use UNION (without ALL). If you are going to use some agregate functions on other columns in select I can see just a couple of ways: 1. Use a temporary table, and then use it for select. 2. Rewrite the select, so it will not include any UNIONs (if it is possible). You can apply the GROUP BY clause to any select in part in the UNION, but you cannot apply a GROUP BY clause to the UNION in whole. Kind Regards, Octav -- Octav Chiriac Phone: (373) 2 21 20 96 NetInfo S.R.L. Fax: (373) 2 21 36 59 Chisinau (373) 2 24 00 83 Moldova, Republic of mailto:com@netinfo-moldova.com