I need to fill a bar chart with data. I have everything in place. I have the sql query used to gather the data.
What I am looking for is the best way to provide data for the current month and the last 11 months (12 months total).
This code will give me the information for the current month up until today. How can I modify this so it will also give me the information for the previous 11 months?
Would this modification be made to the query or would we loop over the query 12 times?
protected void FillChart ()
{
DateTime now = DateTime.Now;
DateTime startDate = now.AddDays( 1 - now.Day );
DateTime endDate = now;
string query;
query = "SELECT SUM(case when r.Type in ('Programming-Billable', 'Consulting-Billable') then r.Minutes_Spent";
query += " end) AS 'Custom Programming and Consulting',";
query += " SUM(case when r.Type in ('Data Maintenance', 'Tech Support-Billable', 'Training') then r.Minutes_Spent";
query += " end) AS 'Tech. Support / Data Maint. / Training'";
query += " FROM TK_Project p ";
query += " INNER JOIN TK_Task t";
query += " ON p.Project_ID = t.Project_ID ";
query += " JOIN TK_Time_Record r";
query += " ON t.ID = r.Task_ID";
query += " WHERE p.Company = " + Session[ "Distid" ];
query += " AND p.Name ='Technical Support' ";
query += " AND r.Work_Date BETWEEN '" + startDate + "' AND '" + endDate + "' ";
query += " AND r.Type NOT LIKE '%non%'";
DataTable data = GeneralFunctions.GetData( query );
}
The query currently returns the following results:
Custom Programming and Consulting Tech. Support / Data Maint. / Training
90 105
EDITED:
SELECT SUM(case when r.Type in ('Programming-Billable', 'Consulting-Billable') then r.Minutes_Spent
end) AS 'Custom Programming and Consulting',
SUM(case when r.Type in ('Data Maintenance', 'Tech Support-Billable', 'Training') then r.Minutes_Spent
end) AS 'Tech. Support / Data Maint. / Training'
FROM TK_Project p
INNER JOIN TK_Task t
ON p.Project_ID = t.Project_ID
JOIN TK_Time_Record r
ON t.ID = r.Task_ID
WHERE p.Company = 162 AND p.Name ='Technical Support'
AND r.Work_Date BETWEEN '04/01/2001' AND '04/30/2012'
AND r.Type NOT LIKE '%non%'
You only need to change start and end date:
By the way, it would probably better to use parameters for both datetimes. So you should pass them to the
GetDatamethod and create SqlParameters from it.Edit: If you want to have the sum for each month, you need to group by month.
You can omit the enddate if you want to get all records until now anyway:
Note that i’ve added the @startDate as parameter.