循环使用12个月

时间:2012-05-09 17:06:13

标签: c# asp.net sql tsql

我需要用数据填充条形图。我有一切到位。我有用于收集数据的SQL查询。

我正在寻找的是提供当月和过去11个月(总共12个月)数据的最佳方式。

此代码将为我提供截至今天的当月信息。我该如何修改它以便它还能提供前11个月的信息?

是否会对查询进行此修改,还是会将查询循环12次?

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 );
}

查询当前返回以下结果:

Custom Programming and Consulting   Tech. Support / Data Maint. / Training
           90                                           105

编辑:

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%'

3 个答案:

答案 0 :(得分:1)

您只需要更改开始日期和结束日期:

DateTime startDate = now.AddYears( -1 );
DateTime endDate = now;

顺便说一句,最好使用两个日期时间的参数。所以你应该将它们传递给GetData方法并从中创建SqlParameters。

修改:如果您想获得每个月的总和,则需要按月分组。

GROUP BY DATEADD(month, DATEDIFF(month, 0, Work_Date),0)

如果你想要到目前为止获得所有记录,你可以省略enddate:

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'
     , DATEADD(month, DATEDIFF(month, 0, Work_Date),0) AS Month
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  @startDate  AND  GetDate()
AND r.Type NOT LIKE '%non%'
GROUP BY DATEADD(month, DATEDIFF(month, 0, Work_Date),0)
Order By Month

请注意,我已将@startDate添加为参数。

答案 1 :(得分:1)

如果我正确理解了要求,您可以按工作月份分组

Group by datepart(month, r.Work_Date) 

答案 2 :(得分:0)

也许我不理解这个问题,但听起来你需要将开始日期和结束日期作为FillChart()的参数。