1

我需要用数据填充条形图。我已经准备好了一切。我有用于收集数据的 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%'
4

3 回答 3

2

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

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 添加为参数。

于 2012-05-09T17:09:24.217 回答
1

如果我正确理解要求,您将按 Work_Date 月份分组

Group by datepart(month, r.Work_Date) 
于 2012-05-09T17:23:20.450 回答
0

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

于 2012-05-09T17:07:58.977 回答