• If anyone is interested. I solved it by creating the following query. I will loop this through each quarter and populate another table.

    with res

    as

    (

    select

    o.[account_id]

    ,orderyear

    ,orderquarter

    from

    [dbo].[orders] o

    inner join [dbo].[orderlines] ol on o.[order_id] = ol.[order_id]

    cross apply(values(year(o.[ship_date]))) as a1(orderyear)

    cross apply(values(DatePart(quarter,(o.[ship_date])))) as a2(orderquarter)

    where

    product_code like ('ll%') and ship_date = (select min(ship_date) from [dbo].[orders] mo where [account_id] = o.account_id)

    and total_value>0 AND order_status NOT LIKE 'return%' AND order_status NOT LIKE 'cancel%' AND order_status NOT LIKE 'freeze%' and

    (SELECT CASE WHEN EXISTS(SELECT NULL FROM [FMTables].[dbo].[orderlines] WHERE order_id = (Select MIN(order_id) from [FMTables].[dbo].[orders] where account_id = o.account_id)

    AND weborder_prod_code IN('LL/CS50','LL/CS510','LL/CS520','LL/CS530','LL/CS540','wls/cs540/3pack', 'LL/S12','LL/HW291N','LL/HW251N','LL/JABRA/9450 PRO FLEX'))

    THEN 'Y' ELSE 'N' END) = 'Y' and

    orderyear = 2013 and orderquarter = 3

    )

    select

    '2013 Q3' as StartGroup

    ,(select count(*) from res) as totalStart

    ,datepart(year,[ship_date]) as [Year]

    ,datepart(q,[ship_date]) as [quarter]

    ,Sum([total_price]) / (select count(*) from res) as AvgSpend

    from

    [dbo].[orders] o

    inner join [dbo].[orderlines] ol on o.[order_id] = ol.[order_id]

    where

    [account_id] in(select account_id from res) and [ship_date] is not null

    group by

    datepart(year,[ship_date])

    ,datepart(q,[ship_date])

    order by

    datepart(year,[ship_date])

    ,datepart(q,[ship_date]);

    Thanks Scott