Unions and Grouping

Поиск
Список
Период
Сортировка
От Aaron Bono
Тема Unions and Grouping
Дата
Msg-id bf05e51c0612151315s59e6647etc2dd45e940d41a2a@mail.gmail.com
обсуждение исходный текст
Ответы Re: Unions and Grouping  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-sql
I have a question about the SQL Specifications in regards to Unions...<br /><br />I recently put together a query that
involvedunions similar to the following:<br /><br />SELECT<br />    'Query 1' as id,<br />    my_value<br />FROM
my_view<br />UNION<br />SELECT<br />    'Query 2' as id,<br />    my_value<br />FROM my_other_view<br />;<br /><br
/>Thefirst query in the union gave me 39 records and the second gave me 34 records.  I was expecting the union to give
me39 + 34 = 73 records. <br /><br />When I ran this against DB2, I got 35 records (not sure about PostgreSQL - will
haveto try it when I get home).  What I found was when I did a group by my_value on each query I got two values that
thenadded to 35.  The reason was, my_value was duplicated in my_view and in my_other_view.  What the Union appeared to
bedoing was to gather the data and then do a group by on the complete results.  I expected it to only eliminate
duplicatesBETWEEN the two queries, not WITHIN the queries. <br /><br />My question, what do the SQL Specifications say
shouldhappen on a Union?  Is it supposed to eliminate duplicates even WITHIN the individual queries that are being
unioned?<br/><br />Thanks!<br clear="all" /><br />-- <br />
==================================================================<br/>   Aaron Bono<br />   Aranya Software
Technologies,Inc.<br />   <a href="http://www.aranya.com">http://www.aranya.com</a><br />   <a
href="http://codeelixir.com">http://codeelixir.com</a><br
/>================================================================== 

В списке pgsql-sql по дате отправления:

Предыдущее
От: Ragnar
Дата:
Сообщение: Re: join and sort on 'best match'
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Unions and Grouping