【问题标题】:Sum column rows where date == specific date in C#C# 中日期 == 特定日期的列行总和
【发布时间】:2018-05-14 07:36:23
【问题描述】:

我们在数据库表中有一个日期列。我们需要检查日期是否多次出现并添加另一列的总和,称为“hours_remaining”。

例如,在 2017 年 11 月 30 日,我们在“hours_remaining”列中有两个任务,分别是 3 和 4。我们需要将这些值相加并绘制到 Visual Studio 图表中。

目前它在图表上分别绘制两个值。

con.Open();
    SqlCommand cmd = new SqlCommand("SELECT * FROM SprintTask INNER JOIN Sprint ON SprintTask.sprint_id = Sprint.Id WHERE Sprint.Id = @sid", con);
    cmd.Parameters.AddWithValue("@sid", Request.QueryString["sid"]);

    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet st = new DataSet();
    sda.Fill(st, "task_start_date");

    Chart1.DataSource = st.Tables["task_start_date"];
    Chart1.Series["Series1"].XValueMember = "task_start_date";
    Chart1.Series["Series1"].YValueMembers = "hours_remaining";
    this.Chart1.Titles.Add("This is a test chart ");

    Chart1.Series["Series1"].ChartType = SeriesChartType.Line;
    Chart1.Series["Series1"].IsValueShownAsLabel = true;

【问题讨论】:

  • 请发布您的相关代码、sql 等...您有具体问题。另请参阅How do I ask a good questionHow to create a Minimal, Complete, and Verifiable example(如果适用)。
  • 向我们展示您检索数据的代码。这并不难做到,但如果不知道您拥有哪些数据类型、如何检索数据等,就很难知道该建议什么。
  • @Chris 添加了更多代码
  • 阅读 GROUP BYSUM 以了解 SQL Server。
  • 您想要“特定日期”,但该日期如何存储:有时间还是没有时间?

标签: c# sql asp.net visual-studio charts


【解决方案1】:

正如@mjwills 在他的评论中所写,您需要使用 group by 和 sum:

SELECT task_start_date, SUM(hours_remaining) As hours_remaining
FROM SprintTask 
INNER JOIN Sprint ON SprintTask.sprint_id = Sprint.Id 
GROUP BY task_start_date
WHERE Sprint.Id = @sid

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多