Nov 27

So I have been doing a lot of SQL at work lately.  I have had the problem that has ended in some very ugly SQL.  Basically when someone calls us we create a record for this.  Some calls will get a sale value, when they are sales obviously.  So I needed to get a count of the calls and a total count of sales.  After speaking to our DBA of the time he said the only way was to create 2 queries and join them and use appropriate where clauses.

So being the DBA I took his advice.  After implementing all the stored procedures I found the following syntax which I am sure you all knew but thought I would share:

Select dbo.ContactLog.RedirectionDate, 103) AS Date,
COUNT(dbo. ContactLog.Calls) AS [TotalCalls],
Sum(case when SaleValue is not null then 1 else 0 end) as [ToatlSales]
from dbo.Contactlog
where Somewhere clause and Group by clause

Hopefully it helps someone to avoid the world of hurt I ended up in!  I am now busy making the SQL more logical and quicker.  I guess it is all a learning process but frustrating to have to revisit again!