我有一张包含客户任务详细信息的表格(附图)。我想要结果,例如每个客户在特定月份收到的任务数量,不。自开始日期起 5 天内完成的任务及其合规性。有人可以帮忙做一个 SQL 查询吗
client | No.of tasks of a month | No.of tasks completed on time | % of compliance
A | 5 | 4 | 75%
我有一张包含客户任务详细信息的表格(附图)。我想要结果,例如每个客户在特定月份收到的任务数量,不。自开始日期起 5 天内完成的任务及其合规性。有人可以帮忙做一个 SQL 查询吗
client | No.of tasks of a month | No.of tasks completed on time | % of compliance
A | 5 | 4 | 75%
假设您有一个相当新的 sql-server,请尝试这样的事情(根据需要更改表和列的名称)。
select
x.Client
, x.MonthTasks
, x.CompletedOnTime
, Compliance = x.CompletedOnTime * 100 / x.MonthTasks
from (
select
t.Client
, MonthTasks = Count(1)
, CompletedOnTime = SUM(CASE WHEN DATEDIFF(day, t.TaskStart, t.TaskEnd)<=5 THEN 1 ELSE 0 END)
from tasks as t
where
year(t.TaskStart) = 2018 -- put year
and month(t.TaskStart) = 10 -- put month
group by t.Client
) as x
order by x.Client
关于您的最后一条评论,这是一个最小的、未准备好生产的想法:
假设您有一个组合框,可以显示月份和年份
cbYear.DataSource = new int[] { 2017, 2018, 2019 };
cbMonth.DataSource = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dec" };
和一个 datagridview dgv,你可以做这样的事情(在一个按钮中)
SqlConnection con = new SqlConnection("Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=test;Integrated Security=SSPI");
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = @"
select
x.Client
, x.MonthTasks
, x.CompletedOnTime
, Compliance = x.CompletedOnTime * 100 / x.MonthTasks
from (
select
t.Client
, MonthTasks = Count(1)
, CompletedOnTime = SUM(CASE WHEN DATEDIFF(day, t.TaskStart, t.TaskEnd)<=5 THEN 1 ELSE 0 END)
from tasks as t
where
year(t.TaskStart) = @year
and month(t.TaskStart) = @month
group by t.Client
) as x
order by x.Client
";
cmd.Parameters.AddWithValue("@year", cbYear.SelectedItem);
cmd.Parameters.AddWithValue("@month", cbMonth.SelectedIndex +1);
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
var dt = new DataTable();
dt.Load(dr);
dgv.AutoGenerateColumns = true;
dgv.DataSource = dt;
dgv.Refresh();
我再说一遍,你必须捕获任何错误,也许在存储过程中转换查询,但这可以作为一个起点。