【发布时间】:2018-07-07 15:52:54
【问题描述】:
我已经在这里声明了所有变量。
declare @FromDate as datetime;
declare @ToDate as datetime;
declare @OperID as varchar(20) = 'OP1';
declare @Year as int = 2018
declare @Month as int = 1
set @FromDate = convert(date,convert(varchar,@Year) + '-' +
convert(varchar,@Month) + '-01')
set @ToDate = dateadd(d,-1,DATEADD(m, 1, @FromDate))
这是查询的主体,我要输出 Branch_No,operid 是 Staff ID,Clock_date 是时钟 In 和时钟 Out 的日期,[I] 代表时钟输入, [O] 代表时钟输出。
select Branch_no, operid, clock_date, [I], [O]
from
( select Branch_no, operid,
convert(date, clock_date) as clock_date,
convert(time, clock_date) as clock_time,
clock_type, Workstation_no
from ROSTER_TIMECLOCK
where Clock_date >=CONVERT(DATETIME, @FromDate, 102)
and Clock_date <=CONVERT(DATETIME, @ToDate, 102)
and OperID=@OperID ) as TheClock
然后,我使用 Pivot 组合查询以显示这样的列中的数据
分行编号 |时钟日期 |员工证 |输入 |出局
PIVOT
( min(clock_time)
FOR clock_type in ([I],[O])
) as ThePivot
【问题讨论】:
-
您的意思是输出日期是从 1 到 28 还是 30 或 31,无论您的表中是否有这些日期的条目?
-
我的意思是,2 月 1 日至 28 日,基本上它遵循所选月份有多少天,假设用户选择 1 月和 2018 年,那么它将列出所有天数月。
标签: sql vb.net stored-procedures