You don't necessarily need the derived table.In your case you are doing a COUNT of the derived tables COUNT..which may not be correct for what your stated purpose is. I read that you want to reduce many quotes to basically a true/false that they recieved 1 or more?Either this would provide you with the needed rows where at least 1 quote was had (you would have to add columns for more info like Policy ID or something and group by that:SELECT dateadd(dd,datediff(dd,0,dbo.policy.adddate),0)) as OverallAddDay FROM dbo.Insured INNER JOIN dbo.Policy ON dbo.Insured.insuredID = dbo.Policy.insuredIDWhere dateadd(dd,datediff(dd,0,dbo.policy.adddate),0) >= '20090701' and dateadd(dd,datediff(dd,0,dbo.policy.adddate),0)< '20090901' GROUP BY dateadd(dd,datediff(dd,0,dbo.policy.adddate),0))
OR This would use your existing syntax to something similar.Select OverallADDDay,CASE WHEN UniqueQuotecount > 0 then 1 else 0 end) as HasRecievedQuoteFROM ( SELECT dateadd(dd,datediff(dd,0,dbo.policy.adddate),0) as OverallADDDay ,count(distinct dbo.policy.insuredid) as UniqueQuoteCount FROM dbo.Insured INNER JOIN dbo.Policy ON dbo.Insured.insuredID = dbo.Policy.insuredID WHERE dbo.Policy.policytype = '4' GROUP BY dateadd(dd,datediff(dd,0,dbo.policy.adddate),0)) as dWhere OverallADDDay >= '20090701' and OverallADDDay < '20090901'
It is easy to get things in your mind's eye overcomplicated. Start with a SELECT that returns the rows/records you need to work with, THEN figure out how to aggregate then the way you need to.I would presume this: SELECT dateadd(dd,datediff(dd,0,dbo.policy.adddate),0) as OverallADDDay ,Policy.InsuredIDFROM dbo.Insured INNER JOIN dbo.Policy ON dbo.Insured.insuredID = dbo.Policy.insuredIDWHERE dbo.Policy.policytype = '4'
Gives you the correct records you want?If so, then you can simply
Poor planning on your part does not constitute an emergency on my part.