I have a table that records market interactions by employees with customers. Relevant fields would be customerid, date and customer type.
I want to be able to get a count of interactions by 5 week blocks going back as far as the between dates that would be submitted by the user and discriminate by customer type a,b,c.
I want to be able to take the returned data and create an a,b and c series with counts of market interactions grouped by 5 week blocks.
Something like:
double[] byValues = { 14, 16, 18, 19, 15, 18, 19, 14, 15 };
string[] bxValues = { "50", "45", "40", "35", "30", "25", "20", "15", "10" };
which might be the A-Series. Once I have my A, B and C series i can then feed them into a stackedchart graph.
I would also like the user to be able to adjust X=5 from 1 to 20 so it would be nice to have that variable as dynamic. Any pointers or urls would be appreciated.
My initial thoughts were to query once and create a temp table with an additional field that was populated by an if statement with some math in it that calculated the block rank of the record according to its date. Then i could requery that with a grouping count for the other criteria. But Im really not sure what the most performant way of getting that first step done, is. It seems like the kind of thing i could get badly wrong perhaps by orders of magnitude.
UPDATE:
This is what I have done and it works. Its actually quite simple. There are some literals in there which I can easily convert to parameters.
SELECT mi1.[CAQ] AS CAQ, FLOOR(DATEDIFF(d, mi1.IDate, CONVERT(datetime, '11/07/2010', 103))/5) AS X_AXISBLOCK
INTO #myTempTable
FROM [ql10_crm].[MarketInteraction] mi1 INNER JOIN [ql10_crm].[TerritoryCustomer]
ON mi1.[CustomerId] =[ql10_crm].[TerritoryCustomer].[CustomerId]
WHERE mi1.[CAQ] IN(1,2,4) AND
[ql10_crm].[TerritoryCustomer].[TerritoryId] IN(19) AND (mi1.[IDate] BETWEEN CONVERT(datetime, '18/06/2009', 103) AND CONVERT(datetime, '11/07/2010', 103));
SELECT CAQ, X_AXISBLOCK , COUNT([CAQ])
FROM #myTempTable
GROUP BY CAQ, X_AXISBLOCK
ORDER BY CAQ, X_AXISBLOCK;
Not sure how performant or flexible it is in comparision to the answers. Will have to test.
This is my output (CAQ, X_AXISBLOCK , COUNT) Where a CAQ of 1=A, 2=B, 4=C:
2 0 2
2 1 6
2 6 2
4 0 3
4 1 5
4 6 4