SUM OVER PARTITION BY

You could have used DISTINCT or just remove the PARTITION BY portions and use GROUP BY: Not sure why you are dividing the total by the count per BrandID, if that’s a mistake and you want percent of total then reverse those bits above to:

INSERT VALUES WHERE NOT EXISTS

You could do this using an IF statement: You could do it without IF using SELECT Both methods are susceptible to a race condition, so while I would still use one of the above to insert, but you can safeguard duplicate inserts with a unique constraint: Example on SQL-Fiddle ADDENDUM In SQL Server 2008 or later you can use MERGE with HOLDLOCK to remove the … Read more

Append table to an existing one: SQL Server

The basic form is: This may not work if, for instance, you have a unique constraint on the columns and the insert violates this constraint. This assumes that you actually want to add the rows to the table. If you just want to see the results together: EDIT: The goal seems to be to add columns one tableB. … Read more