按日期对数据进行分组并将DateTime转换为Double

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按日期对数据进行分组并将DateTime转换为Double相关的知识,希望对你有一定的参考价值。

任何人都可以帮助我,我现在得到的结果:

enter image description here

我希望Stunden列中的数据转换为Double并按日期排序最终结果为01.03.2017 = 9.0; 13.12.2017 = 8.5;

这是我目前的代码

q.ZPZ_LPE_ID = userID;
            if (db.State == ConnectionState.Closed)
                db.Open();
            string query = "SELECT zei.ZPZ_Von, zei.ZPZ_Bis, per.LPE_Nr, zei.ZPZ_LPE_ID, zei.ZPZ_Datum, SUM (zei.ZPZ_Std100) AS ZPZ_Std100" +
                           " FROM DB.dbo.Z_PRAESENZZEIT zei INNER JOIN DB.dbo.A_PERSONAL per ON zei.ZPZ_LPE_ID = per.LPE_ID" +
                            $" WHERE zei.ZPZ_Datum BETWEEN '{dtFromDate.Value}' AND '{dtToDate.Value}' AND zei.ZPZ_LPE_ID='{userID.ToString()}' GROUP BY per.LPE_Nr, zei.ZPZ_LPE_ID, zei.ZPZ_Datum, zei.ZPZ_Von, zei.ZPZ_Bis ORDER BY zei.ZPZ_Datum, per.LPE_Nr;";

            using (SqlCommand cmd = new SqlCommand(query, db))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        var items = new BindingList<PRAESENZZEIT>();
                        while (dr.Read())
                        {
                            PRAESENZZEIT pra = new PRAESENZZEIT();

                            pra.ZPZ_Datum = Convert.ToDateTime(dr["ZPZ_Datum"]);
                            pra.ZPZ_Von = Convert.ToDateTime(dr["ZPZ_Von"]);
                            if (pra.ZPZ_Von.TimeOfDay < new TimeSpan(8, 5, 0))
                                pra.ZPZ_Von = new DateTime(pra.ZPZ_Von.Year, pra.ZPZ_Von.Month, pra.ZPZ_Von.Day, 8, 0, 0);

                            // DateTime gehen = DateTime.Now;
                            pra.ZPZ_Bis = Convert.ToDateTime(dr["ZPZ_Bis"]);
                            pra.arbeitszeit = pra.ZPZ_Bis - pra.ZPZ_Von;
                           // Convert.ToString(Convert.ToInt32(arbeitszeit));

                            items.Add(pra); 
                        }
                        pRAESENZZEITBindingSource.DataSource = items;

                    }
                }
            }
        }

这是PRAESENZEIT课程

public class PRAESENZZEIT
{
    public int LPE_Nr { get; set; }
    public DateTime ZPZ_Datum { get; set; }
    public double ZPZ_Std100 { get; set; }
    public int ZPZ_LPE_ID { get; set; }

    public DateTime ZPZ_Von { get; set; }
    public DateTime ZPZ_Bis { get; set; }
    public DateTime ZPZ_Std { get; set; }

    public int ZPZ_ID { get; set; }
    public int ZPZ_Jahr { get; set; }
    public int ZPZ_Monat { get; set; }
    public int ZPZ_Tag { get; set; }
    public DateTime ZPZ_ERFDAT { get; set; }
    public string ZPZ_ERFUSER { get; set; }
    public DateTime ZPZ_MUTDAT { get; set; }
    public string ZPZ_MUTUSER { get; set; }
    public TimeSpan arbeitszeit { get; set; }
}

谢谢大家的帮助

mysql查询结果enter image description here

答案

我猜你想白天分组。您可以检查是否已添加日期并将时间添加到arbeitszeit。然而,字段'von'和'bis'是无用的。这基本上是因为课堂设计非常糟糕。你可以有一个类ArbeitsTag,其中包含属性List<Anwesenheit>和属性Gesamtzeit aka Totaltime,你总结了所有的时间。但这是一个不同的故事;-)

var items = new BindingList<PRAESENZZEIT>();
                    while (dr.Read())
                    {
                        PRAESENZZEIT pra = null;


                        DateTime datum = Convert.ToDateTime(dr["ZPZ_Datum"]);

                        //calculate parse from and to. Don't store it to Präsenzzeit as it will be accumulated. Therefore it will always be wrong. possible solution would be to store each "phase" separatly and calculate a Total time from there...
                        DateTime von = Convert.ToDateTime(dr["ZPZ_Von"]);
                        if (von.TimeOfDay < new TimeSpan(8, 5, 0))
                            von = new DateTime(von.Year, von.Month, von.Day, 8, 0, 0);

                        DateTime bis = Convert.ToDateTime(dr["ZPZ_Bis"]);

                         pra = items.FirstOrDefault(x => x.ZPZ_Datum == datum);
                        //check if day was already added
                        if (pra != null)
                        {
                            pra.arbeitszeit = pra.arbeitszeit + (bis - von);
                        }
                        else
                        {

                            pra = new PRAESENZZEIT();

                            pra.ZPZ_Datum = datum;

                            // DateTime gehen = DateTime.Now;

                            pra.arbeitszeit = bis - von;
                            // Convert.ToString(Convert.ToInt32(arbeitszeit));

                            items.Add(pra);
                        }
                    }
另一答案

如果学生是TimeSpan,您可以这样累加。

var dataSource = new List<grdata> {
     //         StartDate         , EndDate
     new grdata("01/03/2017 04:00","01/03/2017 08:00"),
     new grdata("01/03/2017 09:00","01/03/2017 14:00"),
     new grdata("13/12/2017 04:30","13/12/2017 09:00"),
     new grdata("13/12/2017 10:00","13/12/2017 14:00")
};

var opdata = dataSource.Select(x => new
{
    date = DateTime.Parse(x.start.ToShortDateString())                ,
    time = x.end-x.start
});

var result = opdata.GroupBy(x => x.date)
                    .Select(g => new
                    {
                        date = g.Key,
                        sumTime = new TimeSpan(g.Sum(y => y.time.Ticks))
                    });

var totalSum = new TimeSpan(opdata.Sum(y => y.time.Ticks));

结果:

{ date = {01/03/2017 00:00:00}, sumTime = {00:09:00} }
{ date = {13/12/2017 00:00:00}, sumTime = {00:08:30} }

以上是关于按日期对数据进行分组并将DateTime转换为Double的主要内容,如果未能解决你的问题,请参考以下文章

使用 Python,如何按小时对 Dataframe 中的列进行分组?

按日期分组时将整数行转换为列

有没有办法从 min(date) 开始按 30 天的间隔对时间戳数据进行分组并将它们添加为列

如何使用 Linq 按日期时间和平均结果对字典进行分组

Pandas:按日历周分组,然后为实际日期时间绘制分组条形图

如何重塑数据框并将重复列转换为数据框行?