Showing posts with label TSQL. Show all posts
Showing posts with label TSQL. Show all posts

Saturday, 5 May 2012

MS SQL Correlated Subqueries


Well you can learn something new every day so I am finished up for the day and thought I would write about it.

I was writing a report for a customer and they wanted a count of values in a column. Simple stuff right, you just do a quick:

SELECT column1, COUNT(column3) FROM table1 GROUP BY column1

You end up with something like:
Value1  100
Value2  50
Value3  75

The problem here is what to do when you want to count the occurrences of the unique values in the column and have these counts output as individual columns in your report. Let’s say you have a data import job and the status of the each row you import is either going to be Pending, Completed or Failed.