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

时间:2016-10-14 16:28:59

标签: c# sql asp.net sql-server

当没有数据时我想要零。我怎么能这样做我会尝试,但它不起作用它只给我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());

我的图表数据..

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值我需要做什么?

3 个答案:

答案 0 :(得分:0)

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

SELECT COALESCE(SUM(Add_id), 0)

答案 1 :(得分: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

答案 2 :(得分:0)

这是我的回答它的工作很好......

//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