劳务实名制系统首页数据统计
函数 | 案例 | 说明 |
---|---|---|
DATEDIFF(part,start,end) | select * from [表名] where DateDiff(dd,[字段DT],getdate())<=7 |
七天内的数据 |
CONVERT(varchar(20), date, 23) | select CONVERT(varchar(12) , getdate(), 23 ) |
2020-05-25(格式转换) |
聚合函数 | COUNT、SUM、MIN 和 MAX |
求记录数,求和,最小值,最大值 |
Case函数 | case type when '1' then '进场' else '退场' end |
条件判断 |
Select
(Select COUNT(Fid) from SM_Project where DATEDIFF(dd,startDate,GETDATE())>0 and DATEDIFF(dd,startDate,GETDATE())<30 ) 本月新开,
(Select COUNT(Fid) from SM_Project where DATEDIFF(dd,completeDate,GETDATE())>0 and DATEDIFF(dd,completeDate,GETDATE())<30 ) 本月退场,
(Select COUNT(Fid) from SM_Project where DATEDIFF(dd,completeDate,GETDATE())<0 ) 在建项目,
(select COUNT(Fid) from SM_ProjectWorker ) 工人总数
select COUNT(*) as 考勤人数,日期
from
(Select CONVERT(varchar(100), date, 23) as 日期,workerFid
from SM_WorkerAttendance where DateDiff(dd,date,getdate())<=30
group by CONVERT(varchar(100), date, 23),workerFid) as temp
group by 日期
select
COUNT(case type when '1' then '进场' end )as 进场,
COUNT(case type when '0' then '退场' end )as 退场,
CONVERT(varchar(10), date, 23) as 日期
from View_SM_WorkerEntryExit
where
Datediff(dd,date,GETDATE()) <30 group by CONVERT(varchar(10), date, 23)
select
case
when age>=1 and age <29 then '29'
when age>=30 and age <39 then '30~39'
when age>=40 and age <49 then '40~49'
when age>=50 and age <55 then '50~55'
else '>55'
end as 年龄范围,
count(*) as 人数
from (select DateDiff(YEAR,birthday ,getdate()) as age,birthday from
(select idCardNumber, CONVERT(datetime, substring(idCardNumber,7,4)+'-'+substring(idCardNumber,11,2)+'-'+substring(idCardNumber,13,2)) birthday from [View_SM_ProjectWorker] ) t )
a
group by
case
when age>=1 and age <29 then '29'
when age>=30 and age <39 then '30~39'
when age>=40 and age <49 then '40~49'
when age>=50 and age <55 then '50~55'
else '>55' end
order by 人数 Desc
SELECT
case left(idCardNumber,2)
when '11' then '北京市'
when '12' then '天津市'
when '13' then '河北省'
when '14' then '山西省'
when '15' then '内蒙古自治区'
when '21' then '辽宁省'
when '22' then '吉林省'
when '23' then '黑龙江省'
when '31' then '上海市'
when '32' then '江苏省'
when '33' then '浙江省'
when '34' then '安徽省'
when '35' then '福建省'
when '36' then '江西省'
when '37' then '山东省'
when '41' then '河南省'
when '42' then '湖北省'
when '43' then '湖南省'
when '44' then '广东省'
when '45' then '广西壮族自治区'
when '46' then '海南省'
when '50' then '重庆市'
when '51' then '四川省'
when '52' then '贵州省'
when '53' then '云南省'
when '54' then '西藏自治区'
when '61' then '陕西省'
when '62' then '甘肃省'
when '63' then '青海省'
when '64' then '宁夏回族自治区'
when '65' then '新疆维吾尔自治区'
when '71' then '台湾省'
when '81' then '香港特别行政区'
when '82' then '澳门特别行政区'
else '未知'
end AS 籍贯地,COUNT(*) 人数
from View_SM_ProjectWorker group by
case left(idCardNumber,2)
when '11' then '北京市'
when '12' then '天津市'
when '13' then '河北省'
when '14' then '山西省'
when '15' then '内蒙古自治区'
when '21' then '辽宁省'
when '22' then '吉林省'
when '23' then '黑龙江省'
when '31' then '上海市'
when '32' then '江苏省'
when '33' then '浙江省'
when '34' then '安徽省'
when '35' then '福建省'
when '36' then '江西省'
when '37' then '山东省'
when '41' then '河南省'
when '42' then '湖北省'
when '43' then '湖南省'
when '44' then '广东省'
when '45' then '广西壮族自治区'
when '46' then '海南省'
when '50' then '重庆市'
when '51' then '四川省'
when '52' then '贵州省'
when '53' then '云南省'
when '54' then '西藏自治区'
when '61' then '陕西省'
when '62' then '甘肃省'
when '63' then '青海省'
when '64' then '宁夏回族自治区'
when '65' then '新疆维吾尔自治区'
when '71' then '台湾省'
when '81' then '香港特别行政区'
when '82' then '澳门特别行政区'
else '未知' end order by 人数 desc
Select top(10) workType as 工种, COUNT(Fid)as 人数
from View_SM_ProjectWorker
Group by workType order by 人数 Desc