|
|
|
SSCertifiable
       
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
|
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 2:54 AM
Points: 328,
Visits: 1,848
|
|
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
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, January 02, 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.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 2:54 AM
Points: 328,
Visits: 1,848
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 8:52 AM
Points: 1,304,
Visits: 7,119
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
|
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 2:54 AM
Points: 328,
Visits: 1,848
|
|
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
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 7:43 AM
Points: 807,
Visits: 1,600
|
|
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.
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Today @ 1:35 AM
Points: 4,787,
Visits: 1,336
|
|
|
|
|