sql 单表查询语句 求高手帮忙
发布网友
发布时间:2022-04-08 19:49
我来回答
共5个回答
热心网友
时间:2022-04-08 21:18
--总出席的天数
DECLARE @day_count int
select @day_count=count(*) from
(select substring(convert( varchar(10),signtime,120),1,10) a from sign group by substring(convert( varchar(10),signtime,120),1,10)) s;
--考勤表中出现的人
select m1.userid,m1.应考天数,m2.签到次数,m3.签退次数,ISNULL(m4.迟到次数,0) 迟到次数,ISNULL(m5.早退次数,0) 早退次数 from (
select userid,@day_count 应考天数 from sign group by userid) m1 left join (
--每人签到的次数
select count(signid) 签到次数, userid from sign where signflag=1 group by userid ) m2 on m1.userid=m2.userid left join(
--每人签退的次数
select count(signid) 签退次数, userid from sign where signflag=0 group by userid ) m3 on m1.userid=m3.userid left join (
--每人迟到次数
select count(signid) 迟到次数, userid from sign where signflag=1 and convert(varchar(5) ,signtime,114)>'08:00' group by userid ) m4 on m1.userid=m4.userid left join (
--每人早退次数
select count(signid) 早退次数, userid from sign where signflag=0 and convert(varchar(5) ,signtime,114)<'18:00' group by userid) m5 on m1.userid=m5.userid
--把这些语句再用left join 连在一起想要啥就有啥了,都到这步了计算你自己来吧
热心网友
时间:2022-04-08 22:36
假设8点上班,18点下班。
出勤是指有签到,而旷工是指签退;
迟到的定义是指8点以后签到;
早退的定义是指18点以前签退;
另外,以下情况不作考虑:
输入的开始时间 < 结束时间
不考虑节假日,不考虑重名,
WITH cte_S([userId],[signTime],[mark]) AS (
SELECT [userId],[signTime],[mark]
FROM [Sign]
WHERE [signTime] BETWEEN @DateBegin AND @DateEnd
AND (DATEPART(DW,[signTime]) BETWEEN 2 AND 6)
)
SELECT DISTINCT [userId],
(SELECT COUNT(*)
FROM cte_S
WHERE ([userId] = tblM.[userId])
AND ([mark] = 1)
) AS [出勤率],
(SELECT COUNT(*)
FROM cte_S
WHERE ([userId] = tblM.[userId])
AND ([mark] = 1)
AND (DATEPART(HH,[signTime]) > 8)
) AS [迟到次数],
(SELECT COUNT(*)
FROM cte_S
WHERE ([userId] = tblM.[userId])
AND ([mark] = 0)
AND (DATEPART(HH,[signTime]) < 17)
) AS [早退次数],
(SELECT (DATEPART(WK,@DateEnd) - DATEPART(WK,@DateBegin)) * 5 + CASE
WHEN (DATEPART(DW,@DateBegin) < DATEPART(DW,@DateEnd))
THEN (DATEPART(DW,@DateEnd) - DATEPART(DW,@DateBegin) + 1)
ELSE
0 - (DATEPART(DW,@DateBegin) - DATEPART(DW,@DateEnd)) + 1
END
-
COUNT(*)
FROM cte_S
WHERE ([userId] = tblM.[userId])
AND ([mark] = 1)
) AS [旷工次数]
FROM [Sign] AS tblM
这个比较接近800个字符了,你再去掉些可有可无的关键字………
如果你可以再写一个函数的话,你可以把Case那段写在函数里。
热心网友
时间:2022-04-09 00:11
这个有点复杂,建议你在表中加一个状态的列,用于存放当天是早退,迟到,还是正常上班,就像签卡那一列一样,这样写出来就简单了
热心网友
时间:2022-04-09 02:02
我只能说很简单。
热心网友
时间:2022-04-09 04:10
楼上的答案 很占屏幕