Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Union All

Author  Topic 

Timmy6
Starting Member

8 Posts

Posted - 2009-03-03 : 10:33:32
Hi

I am currently writing my first proper peice of SQL to manipulate a sizeable data set and need to query 1 table twice and combine the results to create a new table

However if I run my first select query I get 59,337 records, i then run my second select query and get 378,474 records. I then apply a union all into the script to compine the 2 select stamtements into a single data set but end up with more records than the 2 numbers above combined!

I know that union alone will remove duplicates but I dont understand how union all can increase the number of records

Does anyone have any tips on how to error check this, bearing in mind my 2 select statements in isolation seem to be OK

Thanks

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-03-03 : 10:48:32
not possible. Can you post the unioned statement ?? Also, how did you find out the counts ?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-03-03 : 10:50:18
UNION alone can double the number of records returned.

Post your code here and we will spot the other errors.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-03 : 10:59:06
quote:
Originally posted by Timmy6

Hi

I am currently writing my first proper peice of SQL to manipulate a sizeable data set and need to query 1 table twice and combine the results to create a new table

However if I run my first select query I get 59,337 records, i then run my second select query and get 378,474 records. I then apply a union all into the script to compine the 2 select stamtements into a single data set but end up with more records than the 2 numbers above combined!

I know that union alone will remove duplicates but I dont understand how union all can increase the number of records

Does anyone have any tips on how to error check this, bearing in mind my 2 select statements in isolation seem to be OK

Thanks


it would be better if you can explain this with some sample data to illustrate how you get more records in output
Go to Top of Page

Timmy6
Starting Member

8 Posts

Posted - 2009-03-03 : 11:27:31
Sorry to waste you guys time but I had duplicate records in my joined table, I had already checked once too!

All working as expected now
Go to Top of Page
   

- Advertisement -