【问题标题】:combining data from two tables in a database to get total sum (C#)组合数据库中两个表中的数据以获得总和(C#)
【发布时间】:2019-01-01 01:51:21
【问题描述】:

我是数据库新手,需要帮助。我有一个包含两个表([tb_TotalH]、[tb_PricePerH])的数据库。第一个表 [tb_TotalH] 显示每天的总小时数,第二个表 [tb_PricePerH] 是小时价格及其创建日期。

我需要计算总金额(每天的小时数乘以每小时的价格)。 但诀窍是小时的价格不是固定的,它会发生变化,我需要在工作时间制定期间的价格。当我查询日期过滤器以获取标签中的价格时,我还需要获取价格。 我不知道如何开始! 这是我到目前为止所做的一些代码:

     SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");

    void DisplayData()
    {

        con.Open();
        DataTable dt = new DataTable();
        sda = new SqlDataAdapter("select * from tb_TotalH", con);
        sda.Fill(dt);
        dataGridView1.DataSource = dt;
        con.Close();

        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.AutoResizeColumns();
    }


    void BetweenDates()
    {
        DateTime dtFrom = Convert.ToDateTime(dtp1.Value);
        DateTime dtTo = Convert.ToDateTime(dtp2.Value);
        SqlDataAdapter mda = new SqlDataAdapter("select * from DailyDataEntry where Date_H between '" + dtFrom.ToShortDateString() + "' and '" + dtTo.ToShortDateString() + "' ", con);



        System.Data.DataSet ds = new System.Data.DataSet();
        con.Open();
        mda.Fill(ds, "root");
        dataGridView1.DataSource = ds.Tables["root"];
        dataGridView1.Refresh();
        con.Close();
    }

    void getPrice()
    {
        SqlDataReader rdr = null;
        SqlCommand cmd = null;

        con.Open();


        string CommandText = "SELECT Price, Tax FROM tb_PricePerH WHERE Date_M = @date"; 
        cmd = new SqlCommand(CommandText);
        cmd.Connection = con;

        cmd.Parameters.AddWithValue("@date", "4/22/2018");  

        rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            lbl1.Text = Convert.ToDouble(rdr["PricePerHour"].ToString());
            lbl2.Text = Convert.ToDouble(rdr["Tax"].ToString());
        }


        con.Close();
    }

【问题讨论】:

  • 你能显示示例输出吗?
  • 似乎没有人可以在午夜工作,所以问题变成了找到给定日期的适用小时费率之一。每小时费率是否适用于:(1) 相关日期(如果存在)或 (B) 相关日期之前的最新定价日期?由于您还没有解释如何计算总和,因此我们很难帮助您。旁白:您将数值转换为字符串,将双精度值转换为字符串以在标签中显示?提示:不使用图片的原因是here
  • @HABO 如果您不想帮助您不必帮助,而且问题不在于字符串、双精度、字符串,代码处于测试阶段。并且欢迎任何帮助,即使不在代码中,如果我有经验,我不会在这里问这个问题。
  • @Anu Viswan 例如,2018 年 5 月 7 日,这一天的总小时数为 9 小时,在那段时间(从 tb_PricePerH 开始),小时价格为 11,因为它从 06.19.2018 开始。所以这一天的利润是 99。
  • 您可能会发现this 的回答很有帮助。

标签: c# sql .net database


【解决方案1】:

我试图用 List 模拟你的场景。

var tablePricePerHours = new List<TablePricePerHours>
    {
        new TablePricePerHours{ Price = 10, Tax = 19, Date = new DateTime(2018,1,5)},
        new TablePricePerHours{ Price = 10.5, Tax = 21.5, Date = new DateTime(2018,4,22)},
        new TablePricePerHours{ Price = 11, Tax = 22.5, Date = new DateTime(2018,6,19)},
        new TablePricePerHours{ Price = 11.5, Tax = 23, Date = new DateTime(2018,10,30)},
        new TablePricePerHours{ Price = 12, Tax = 23.2, Date = new DateTime(2018,11,1)}
    };

    var tableTotalHours = new List<TableTotalHours>
    {
        new TableTotalHours{ Date = new DateTime(2018,02,15), TotalHours = 5},
        new TableTotalHours{ Date = new DateTime(2018,02,19), TotalHours =10},
        new TableTotalHours{ Date = new DateTime(2018,02,25), TotalHours = 8},
        new TableTotalHours{ Date = new DateTime(2018,03,29), TotalHours = 7.5},
        new TableTotalHours{ Date = new DateTime(2018,07,05), TotalHours = 9},
        new TableTotalHours{ Date = new DateTime(2018,07,06), TotalHours = 1.5},
        new TableTotalHours{ Date = new DateTime(2018,07,07), TotalHours = 12},
        new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
        new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
        new TableTotalHours{ Date = new DateTime(2018,11,1), TotalHours = 8},
        new TableTotalHours{ Date = new DateTime(2018,12,21), TotalHours = 8.5},
        new TableTotalHours{ Date = new DateTime(2018,12,22), TotalHours = 9},
    };

其中 TablePricePerHours 和 TableTotalHours 定义为

public class TableTotalHours
{
    public int Id {get;set;}
    public DateTime Date{get;set;}
    public double TotalHours{get;set;}
}


public class TablePricePerHours
{
    public int Id{get;set;}
    public double Price{get;set;}
    public double Tax{get;set;}
    public DateTime Date{get;set;}
}

现在您可以使用 Linq as 获得所需的结果

var result = tableTotalHours.Select(x=> new 
                                    {
                                        Date=x.Date,
                                        TotalHours=x.TotalHours,
                                        CalculatedPrice = tablePricePerHours.Where(c=> (x.Date - c.Date).Ticks>0)
                                                                            .OrderBy(c=> (x.Date - c.Date).Ticks)
                                                                            .First()
                                                                            .Price * x.TotalHours
                                    });

输出

你可以找到相同的here

【讨论】:

  • 是的! :) 1074.50 是一个正确的数字!非常感谢您的回答,很高兴还有人想帮助没有经验的程序员!但是告诉我我是否也应该将我的表格转移到列表中?您能否在普通的 c# 上而不是在 Linq 上写这个“result = tableTotalHours.Select(x=>”,这样我可以更好地理解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多