查询同一班次内的数据个数

Posted

技术标签:

【中文标题】查询同一班次内的数据个数【英文标题】:query for number of data within same shift 【发布时间】:2021-07-21 07:58:42 【问题描述】:

这是我在 Access 中的计时器表:

我正在尝试使用相同的 ShiftLog 和 ModelLog 获取 CTLog 的 COUNT。有两个班次:A 班是早上 7 点到下午 6.59 点(当天内),而 B 班是晚上 7 点到早上 6.59 点(直到第二天)。

使用 Shift A 获取 CTLog 的 COUNT 没有问题

select COUNT(*) AS total from timer where ShiftLog = @sl and TodayDate = @dtl and ModelLog = @m;
OleDbCommand cmd = new OleDbCommand(sql, connection);

cmd.Parameters.Add("@sl", OleDbType.VarWChar).Value = shiftlabel.Text;
cmd.Parameters.Add("@td", OleDbType.VarWChar).Value = DateTime.Now.ToShortDateString();
cmd.Parameters.Add("@m", OleDbType.VarWChar).Value = mlb.Text;

但是通过这种查询我无法获得Shift B的计数。当日期更改为第二天时如何查询Shift B的数据(包括12am之后的数据,从12:01am到6.59am )?

【问题讨论】:

也许构建一个从和到的日期/时间。比如:ShiftLog = @s1 AND DateTimeLog BETWEEN @dt1 & " 19:00:00" AND @dt1 + 1 & " 6:00:00" @June7 嗨,我找到了解决方案! 【参考方案1】:

为 ShiftStart 和 ShiftEnd 创建新列并插入如下:

const string sql = @"INSERT INTO timer(DateTimeLog, ShiftLog, CTLog, WorkcellLog, ModelLog, StationLog, HourID, TodayDate, ShiftStart, ShiftEnd)VALUES(@d, @shift, @ct, @wc, @wm, @ws, @hid, @td, @ss, @se)";
OleDbCommand cmd = new OleDbCommand(sql, connection);

cmd.Parameters.Add("@d", OleDbType.Date).Value = d;
cmd.Parameters.Add("@shift", OleDbType.VarWChar).Value = shiftlb.Text;
cmd.Parameters.Add("@ct", OleDbType.VarWChar).Value = timerlb.Text;
cmd.Parameters.Add("@wc", OleDbType.VarWChar).Value = wclb.Text;
cmd.Parameters.Add("@wm", OleDbType.VarWChar).Value = mlb.Text;
cmd.Parameters.Add("@ws", OleDbType.VarWChar).Value = slb.Text;
cmd.Parameters.Add("@hid", OleDbType.VarWChar).Value = h;
cmd.Parameters.Add("@td", OleDbType.VarWChar).Value = today;
if (int.Parse(DateTime.Now.ToString("%H")) >= 7 && int.Parse(DateTime.Now.ToString("%H")) < 19) //Shift A

   cmd.Parameters.Add("@ss", OleDbType.VarWChar).Value = 7 + "&" + DateTime.Today.ToShortDateString();
   cmd.Parameters.Add("@se", OleDbType.VarWChar).Value = 1859 + "&" + DateTime.Today.ToShortDateString();

else if (int.Parse(DateTime.Now.ToString("%H")) >= 1 && int.Parse(DateTime.Now.ToString("%H")) < 7) //Shift B 1xxam to 659am

   cmd.Parameters.Add("@ss", OleDbType.VarWChar).Value = 19 + "&" + DateTime.Today.AddDays(-1).ToShortDateString();
   cmd.Parameters.Add("@se", OleDbType.VarWChar).Value = 659 + "&" + DateTime.Today.ToShortDateString();

   else if (int.Parse(DateTime.Now.ToString("%H")) >= 19 && int.Parse(DateTime.Now.ToString("%H")) <= 24) //Shift B 7xxpm to 12xxam

   cmd.Parameters.Add("@ss", OleDbType.VarWChar).Value = 19 + "&" + DateTime.Today.ToShortDateString();
   cmd.Parameters.Add("@se", OleDbType.VarWChar).Value = 659 + "&" + DateTime.Today.AddDays(1).ToShortDateString();

然后用类似的方法得到输出:

const string sql = @"SELECT COUNT(*) FROM timer WHERE  ShiftLog = @sl AND ShiftStart = @ss AND ShiftEnd = @se AND ModelLog = @m";
OleDbCommand cmd = new OleDbCommand(sql, connection);
    
cmd.Parameters.Add("@sl", OleDbType.VarWChar).Value = shiftlb.Text;    
if (int.Parse(DateTime.Now.ToString("%H")) >= 7 && int.Parse(DateTime.Now.ToString("%H")) < 19) //Shift A
    
        cmd.Parameters.Add("@ss", OleDbType.VarWChar).Value = 7 + "&" + DateTime.Today.ToShortDateString();
        cmd.Parameters.Add("@se", OleDbType.VarWChar).Value = 1859 + "&" + DateTime.Today.ToShortDateString();
    
else if (int.Parse(DateTime.Now.ToString("%H")) >= 1 && int.Parse(DateTime.Now.ToString("%H")) < 7) //Shift B 1xxam to 659am
    
        cmd.Parameters.Add("@ss", OleDbType.VarWChar).Value = 19 + "&" + DateTime.Today.AddDays(-1).ToShortDateString();
        cmd.Parameters.Add("@se", OleDbType.VarWChar).Value = 659 + "&" + DateTime.Today.ToShortDateString();
    
else if (int.Parse(DateTime.Now.ToString("%H")) >= 19 && int.Parse(DateTime.Now.ToString("%H")) <= 24) //Shift B 7xxpm to 12xxam
    
        cmd.Parameters.Add("@ss", OleDbType.VarWChar).Value = 19 + "&" + DateTime.Today.ToShortDateString();
        cmd.Parameters.Add("@se", OleDbType.VarWChar).Value = 659 + "&" + DateTime.Today.AddDays(1).ToShortDateString();
    
cmd.Parameters.Add("@m" ,OleDbType.VarWChar).Value = mlb.Text;

【讨论】:

以上是关于查询同一班次内的数据个数的主要内容,如果未能解决你的问题,请参考以下文章

sql文中。in(...) oracle对()内的参数 有没有个数的限制?

重载和重写的区别

[线段树]模板1

探知浏览器并发请求个数

SPOJ - DQUERY: D-query 离线处理 + 树状数组

luogu P1440 求m区间内的最小值