如果没有特定日期的数据,则返回0作为该日期的结果

本文关键字:日期 结果 返回 数据 如果没有 | 更新日期: 2023-09-27 18:13:10

我希望没有数据时为零。我怎么能做到这一点,我会尝试,但它不工作,它只给我30天的数据,如果有可用的,不能显示任何值在我的图表。

这里是代码…

对于日期我使用这个代码..

List<string> ChartDates = new List<string>();
int i =-29, j = 0;
while (i <= j)
{
    ChartDates.Add(DateTime.Now.AddDays(i).ToShortDateString());
    i++;
}
StringBuilder s = new StringBuilder();
s.Append("<script>");
s.Append("var DatesArray = new Array;");
foreach (string str in ChartDates)
{
    s.AppendFormat("DatesArray.push('"+str+"');");
}
s.Append("</script>");
//sending data through client script register
ClientScript.RegisterStartupScript(this.GetType(), "Dates", s.ToString());

For My Chart..

List<int> Addmissions = new List<int>();
SqlCommand cmd = new SqlCommand("select count(Add_id), Std_submit_date from IMS_Addmission WHERE Std_submit_date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0)GROUP BY Std_submit_date", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    Addmissions.Add(Convert.ToInt32(dr[0].ToString()));
}
dr.Close();
con.Close();
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("var Addmission_No = new Array;");
foreach (int str in Addmissions)
{
    sb.AppendFormat("Addmission_No.push({0});", str);
}
sb.Append("</script>");
//sending data through client script register
ClientScript.RegisterStartupScript(this.GetType(), "AddmissionNo", sb.ToString());

当没有数据时,我必须为每个日期获得0值做些什么。

如果没有特定日期的数据,则返回0作为该日期的结果

我想你要更新sql字符串到这个

SELECT COALESCE(SUM(Add_id), 0)

试试这个,也许可以为你工作。

if exists (select * from IMS_Addmission WHERE Std_submit_date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0) GROUP BY Std_submit_date)
begin
select  count(Add_id), Std_submit_date from IMS_Addmission WHERE Std_submit_date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0) GROUP BY Std_submit_date
end
else
begin
select 0 as Result
end

这是我的答案,它工作得很好…

//Date for loop
        List<string> ChartDates = new List<string>();
        int i = -29, j = 0;
        while (i <= j)
        {
            ChartDates.Add(DateTime.Now.AddDays(i).ToShortDateString());
            i++;
        }
        //Addmission Data Value for Chart
        List<int> AddmissionData = new List<int>();
        int k = -29, l = 0;
        while (k <= l)
        {
           AddmissionData.Add(0);
            k++;
        }
        //Counting Addmission Per Day 
        List<int> Addmissions = new List<int>();
        List<string> AddmissionDate = new List<string>();
        SqlCommand cmd = new SqlCommand("select count(Add_id), Std_submit_date from IMS_Addmission WHERE Std_submit_date >= dateadd(day, datediff(day, 0, GetDate()) - 30, 0)GROUP BY Std_submit_date", con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Addmissions.Add(Convert.ToInt32(dr[0].ToString()));
            DateTime cd7 = Convert.ToDateTime(dr["Std_submit_date"].ToString(), System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
            string datenow = cd7.ToShortDateString();
            AddmissionDate.Add(datenow);
        }
        dr.Close();
        con.Close();
        int b = 0;
        foreach (string str in ChartDates)
        {
            int c = 0;
            foreach(string str2 in AddmissionDate)
            {
                if(str == str2)
                {
                    AddmissionData[b] = Addmissions[c];
                }
                c++;
            }
            b++;
        }
        // Sending Addmission Data For Chart
        StringBuilder sb = new StringBuilder();
        sb.Append("<script>");
        sb.Append("var Addmission_No = new Array;");
        foreach (int str in AddmissionData)
        {
            sb.AppendFormat("Addmission_No.push({0});", str);
        }
        sb.Append("</script>");
        //sending data through client script register
        ClientScript.RegisterStartupScript(this.GetType(), "AddmissinNo", sb.ToString());
        // (Addmission_No) is the array containg Value for Chart.
        //Addmission Data For Chart End