SQL update from one Table to another based on a ID match

I have a database with account numbers and card numbers. I match these to a file to update any card numbers to the account number, so that I am only working with account numbers. I created a view linking the table to the account/card database to return the Table ID and the related account number, and now I need to update those … Read more

How do I perform an IF…THEN in an SQL SELECT?

The CASE statement is the closest to IF in SQL and is supported on all versions of SQL Server. You only need to use the CAST operator if you want the result as a Boolean value. If you are happy with an int, this works: CASE statements can be embedded in other CASE statements and even included in aggregates. SQL Server Denali (SQL … Read more

SQL Query with SUM with Group By

The GROUP BY clause forms rows for each unique combinaton of the columns you nominate in that clause. If you want to show a sum for each month, then only include s.month in the group by clause: e.g If you include reseller details in the select clause also include them in the group by clause … Read more

The SQL OVER() clause – when and why is it useful?

You can use GROUP BY SalesOrderID. The difference is, with GROUP BY you can only have the aggregated values for the columns that are not included in GROUP BY. In contrast, using windowed aggregate functions instead of GROUP BY, you can retrieve both aggregated and non-aggregated values. That is, although you are not doing that in your example … Read more