快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

sqlserver月几天,sqlserver当前时间

用 sqlserver 怎样查询出 数据表里 某月上班连续打卡15天的人

以下以2013年11月为例

创新互联建站主营淮南网站建设的网络公司,主营网站建设方案,手机APP定制开发,淮南h5小程序定制开发搭建,淮南网站营销推广欢迎淮南等地区企业咨询

1、使用横向连接,以5天为例,简单但不易扩展

with data as ( select * from yourtable where date='2013-11-01' and date'2013-12-01')

select distinct name

from data t1 join data t2 on t1.name=t2.name and t1.date=t2.date+1

join data t3 on t2.name=t3.name and t2.date=t3.date+1

join data t4 on t3.name=t4.name and t3.date=t4.date+1

join data t5 on t4.name=t5.name and t4.date=t5.date+1

2、使用纵向分组统计

with t1(id,rq) as (

select distinct 人员, date from 表 where date='2013-11-01' and date'2013-12-01' ),

--t1求出指定月的人员编号及不同的打卡日期

t2 as (select s2.* from t1 s1 join t1 s2 on s1.id=s2.id and s1.rq=s2.rq-1),

--t2求出所有上一日也打过卡的日期

t3 as (select * from t1 except select * from t2),

--t3求出所有上一日未打过卡的日期

t as (

select id,rq,1 days from t3

union all

select t1.id,t1.rq,t.days+1 from t1 join t on t1.id=t.id and t1.rq=t.rq+1

)

--t4递归调用,每连续一日days+1,就是求每一打卡时间是连续的第几天

select id

from t

group by id

having max(days)=5

order by id

以上就不删了,以下可以改短点吧

with t as (

select 人员 id, date rq, 1 days from 表 t1

where not exists(select * from 表 t2 where t2.date=t1.date-1)

union all

select t1.id,t1.rq,t.days+1 from 表 t1 join t on t1.id=t.id and t1.rq=t.rq+1

)

select id

from t

group by id

having max(days)=5

order by id

每个月的工作日有多少天,每个月剩余多少天(去除工作日,法定节假日)的sql语句。

select year(日期),month(日期),is_working_day,count(*) from table group by is_working_day order by year(日期),month(日期);

mysql的写法,如果sqlserver,

year(日期),month(日期) 换成

DateName(year,日期),DateName(month,日期)

sqlserver如何根据当前日期获得上月某一天的日期

试试select

dateadd

例如:向日期加上2天

select

dateadd(day,2,'2004-10-15')

--返回:2004-10-17

00:00:00.000

dateadd函数说明:

DateAdd

(interval,number,date)

以interval指定的方式,加上number之后的日期

参数

interval的设定值如下:

写(Sql

Server)

(Access

ASP)

说明

Year

Yy

yyyy

1753

~

9999

Quarter

Qq

q

1

~

4

Month

Mm

m

月1

~

12

Day

of

year

Dy

y

一年的日数,一年中的第几日

1-366

Day

Dd

d

日,1-31

Weekday

Dw

w

一周的日数,一周中的第几日

1-7

Week

Wk

ww

周,一年中的第几周

~

51

Hour

Hh

h

时0

~

23

Minute

Mi

n

分钟0

~

59

Second

Ss

s

~

59

Millisecond

Ms

-

毫秒

~

999

sqlserver用sql语句怎么返回一个月所有的天数。

可用如下sql语句:

select convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) day from

(select '2015-07'+'-01' day) t1,

(select number from MASTER..spt_values WHERE TYPE='P' AND number=0 and number=31) t2

where convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) like '2015-07%'

说明:要查询某年某月,只许将语句中的2015-07替换即可。

结果截图:


当前标题:sqlserver月几天,sqlserver当前时间
浏览路径:http://6mz.cn/article/dsihcph.html

其他资讯