-- This query gets the SQL queries per quarter for a given year (2012) for a given SQL product (DR8) in the LogSource table.

select datepart(qq,s.thetime) as Qtr, count(*) as Queries 
into mydb.QtrlyUsageDR8 
from sqllogall s join logsource l on s.logid=l.logid
where s.yy = 2012 and l.product='DR8'
group by datepart(qq,s.thetime) 
order by Qtr

-- To get the same data for multiple years, you can do something like:

select (cast(yy as varchar(4)) + '-Q' + cast(datepart(qq,s.thetime) as varchar(1))) as Qtr, 
       count(*) as Queries 
into mydb.QtrlyUsageDR8 
from sqllogall s join logsource l on s.logid=l.logid
where s.yy > 2010 and l.product='DR8'
group by yy, datepart(qq,s.thetime) 
order by Qtr


