Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Union Expand / Collapse
Author
Message
Posted Tuesday, September 9, 2008 11:02 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Today @ 9:27 AM
Points: 6,800, Visits: 1,917
Comments posted to this topic are about the item Union

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
Post #566598
Posted Wednesday, September 10, 2008 3:25 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 5:51 AM
Points: 328, Visits: 2,001
Except and Intersect - Fantastic, something I didn't know had been added for 2005

I've used (to great effect i might add) the extended UNION (INTERSECT and MINUS) features which have been available in Oracle since 8i and I really missed them when I made the shift to SQL Server 7/2000.

Good to find out they were added for SQL Server 2005 they are a boon in some cases where a join might not be practicle
Post #566691
Posted Wednesday, September 10, 2008 5:39 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, January 2, 2013 12:15 PM
Points: 1,443, Visits: 711
Nice article!
I've seen some weirdness mixing unions and union all with unexpected results in the past.

select ...
union
select ....
union
select ....
union all
select ....

something to beware of I guess in the future.
Post #566773
Posted Wednesday, September 10, 2008 7:42 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 5:51 AM
Points: 328, Visits: 2,001
If you wanted to use several unions like that then I think it would be wise to wrap sets together.

if you had 3 tables, each with identicle columns (just different row data)
Table1, table2 and table3
and you wanted data from table2 which was not in table1 or table3 then I think the following would be best:

select * 
from table2
except
select *
from (select *
from table1
union
select *
from table3) as ExTables

Post #566890
Posted Wednesday, September 10, 2008 8:18 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Today @ 9:27 AM
Points: 6,800, Visits: 1,917
Interesting how the one extra sentence at the end added more value than I thought!

Mixing UNION and UNION ALL - definitely interesting. I think I'd be inclined to separate them with parens to give a good visual signal about what I was trying to achieve and why.


Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
Post #566949
Posted Wednesday, September 10, 2008 10:28 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 10, 2014 9:37 AM
Points: 1,343, Visits: 7,187
it would be great if you wrote a little article like this one on the topics of EXCEPT and INTERSECT. i was aware of those, but have yet to find a use for them.

thanks,
bc


bc
Post #567097
Posted Wednesday, September 10, 2008 10:40 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Today @ 9:27 AM
Points: 6,800, Visits: 1,917
I will add them to my todo list!

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
Post #567106
Posted Wednesday, September 10, 2008 10:48 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 5:51 AM
Points: 328, Visits: 2,001
The except and intercept clauses are great if you want to compare 2 data sets but for whatever reason cannot use the primary keys. Or you need to compare 2 tables with many columns to try and find the difference between them when that difference could be in any column.
You could build a query with joins and then a where clause which compares all the columns. Or you can use the EXCEPT keyword and simply select from both tables.

For a real world example, imagine you have an ETL procedure. A large part of this is contained within some stored procedures which populate a pre-extraction table which being a flattened version of a normalised database has many many columns.
You've made a change to the scripts as some of the values weren't being transformed correctly.
You now need to check that a) the script has made the changes you wanted it to and b) the script isn't inadvertantly making any other changes.
You can copy the pre-extraction table, make the changes and then run the scripts. You now have two copies of the data, one pre change, the other post change.

To write the sql query, you can use something like the following to get the column names (saves typing)
select column_name + ','
from information_schema.columns
where table_name = ' '
order by ordinal_position

remove any columns you don't want to compare and then build the query:
select col1, col2, col3...col99
from postchange_table
EXCEPT
select col1, col2, col3...col99
from prechange_table

the return should only give the rows you expect to have changed. Any more and you've changed more than you expected, any less or zero and nothing has happened
Post #567112
Posted Wednesday, September 10, 2008 12:02 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, November 21, 2014 7:55 AM
Points: 925, Visits: 1,734
I was surprised the first time I encountered this feature of UNION. It's good to share this for others who may also be unaware.

I didn't know about Interest and Except were added in 2005 either. Thanks for a brief and helpful article.
Post #567179
Posted Thursday, September 11, 2008 1:20 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:47 AM
Points: 5,448, Visits: 1,401
Nice article...


Post #567516
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse