sql 查询问题

xlong224 2013-06-05 09:10:05
配置表
ID beginTime endTime excutedate week1 week2 week3 week4 week5 week6 week7
1 8:00 18:00 2013-1-1 1 1 1 1 1 0 0
2 8:30 17:30 2013-5-4 1 1 1 1 1 1 0
3 8:30 18:00 2013-5-20 1 1 1 1 1 0 1
说明:配置可以配置多条 不交叉就行 比如一个月可以配置多条,
week1-7 表示周几上班[1 上班 0 不上班]

我想查出某月配置的所有天的上班情况(如果月未配置则查询离当月最近的一个配置)
比如查4月(配置则为1月1号配置的 周1-5上班) 结果就为
日期 星期几 是否上班 上班时间 下班时间
2013-4-1 1 上班 18:00 2013-1-1
。。。
2013-4-30 2 上班 18:00 2013-1-1

查5月(配置有多条,5月1号到3号是执行1月的,4号到19号是执行第二条配置,20到31号执行第三条配置)
日期 星期几 是否上班 上班时间 下班时间
2013-05-01 3 上班 18:00 2013-1-1
。。
2013-05-03 5 上班 18:00 2013-1-1
2013-05-04 6 上班 8:30 17:30
。。
2013-05-19 7 不上班 8:30 17:30
2013-05-20 1 上班 8:30 18:00
。。
2013-05-31 5 上班 8:30 18:00

问:
该怎么写这样根据动态配置查询出结果呢
只有一个配置的我会写。多个就不会了。
各位大神 大师 帮帮忙吧
...全文
161 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlong224 2013-06-06
  • 打赏
  • 举报
回复
引用 10 楼 sc273607742 的回复:
[quote=引用 9 楼 xlong224 的回复:] [quote=引用 8 楼 sc273607742 的回复:]

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select * from td a 
你看看不做任何处理的数据,就知道是啥意思了
过滤不对的 能不能把exsits 改成 join 形式啊 exsits 看不懂。[/quote]

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select *
 from td a 
 where id=(select max(id) from td where date=a.date)
[/quote] 这个看懂了 大神 怎么联系你 留个联系方式啊
哥眼神纯洁不 2013-06-06
  • 打赏
  • 举报
回复
引用 9 楼 xlong224 的回复:
[quote=引用 8 楼 sc273607742 的回复:]

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select * from td a 
你看看不做任何处理的数据,就知道是啥意思了
过滤不对的 能不能把exsits 改成 join 形式啊 exsits 看不懂。[/quote]

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select *
 from td a 
 where id=(select max(id) from td where date=a.date)
xlong224 2013-06-06
  • 打赏
  • 举报
回复
引用 8 楼 sc273607742 的回复:

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select * from td a 
你看看不做任何处理的数据,就知道是啥意思了
过滤不对的 能不能把exsits 改成 join 形式啊 exsits 看不懂。
哥眼神纯洁不 2013-06-05
  • 打赏
  • 举报
回复

if exists(select 1 from sys.tables where name='tb')
drop table tb

go

with tb(ID,beginTime,endTime,excutedate,week1,week2,week3,week4,week5,week6,week7)as( 
select 1,'8:00','18:00','2013-1-1',1,1,1,1,1,0,0 union all
select 2,'8:30','17:30','2013-5-4',1,1,1,1,1,1,0 union all
select 3,'8:30','18:00','2013-5-20',1,1,1,1,1,0,1)
select * into tb from tb

go

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select convert(date,date,120) 日期,DATEPART(WEEKDAY,date-1) 星期几,
 case when CHARINDEX(convert(varchar,datepart(weekday,date)-1),
 (case when week1=1 then '1' else '' end+
  case when week2=1 then '2' else '' end+
  case when week3=1 then '3' else '' end+
  case when week4=1 then '4' else '' end+
  case when week5=1 then '5' else '' end+
  case when week6=1 then '6' else '' end+
  case when week7=1 then '0' else '' end))>0 then '上班'
  else '不上班'end 是否上班,begintime 上班时间,endtime 下班时间
 from td a 
 where not exists 
 (select 1 from td where a.date=date and ID>a.ID)

/*
日期       星期几      是否上班  上班时间 下班时间
---------- ----------- ------    ----     -----
2013-05-01 3           上班      8:00     18:00
2013-05-02 4           上班      8:00     18:00
2013-05-03 5           上班      8:00     18:00
2013-05-04 6           上班      8:30     17:30
2013-05-05 7           不上班    8:30     17:30
2013-05-06 1           上班      8:30     17:30
2013-05-07 2           上班      8:30     17:30
2013-05-08 3           上班      8:30     17:30
2013-05-09 4           上班      8:30     17:30
2013-05-10 5           上班      8:30     17:30
2013-05-11 6           上班      8:30     17:30
2013-05-12 7           不上班    8:30     17:30
2013-05-13 1           上班      8:30     17:30
2013-05-14 2           上班      8:30     17:30
2013-05-15 3           上班      8:30     17:30
2013-05-16 4           上班      8:30     17:30
2013-05-17 5           上班      8:30     17:30
2013-05-18 6           上班      8:30     17:30
2013-05-19 7           不上班    8:30     17:30
2013-05-20 1           上班      8:30     18:00
2013-05-21 2           上班      8:30     18:00
2013-05-22 3           上班      8:30     18:00
2013-05-23 4           上班      8:30     18:00
2013-05-24 5           上班      8:30     18:00
2013-05-25 6           不上班    8:30     18:00
2013-05-26 7           上班      8:30     18:00
2013-05-27 1           上班      8:30     18:00
2013-05-28 2           上班      8:30     18:00
2013-05-29 3           上班      8:30     18:00
2013-05-30 4           上班      8:30     18:00
2013-05-31 5           上班      8:30     18:00

(31 行受影响)
*/

好费时啊...
xlong224 2013-06-05
  • 打赏
  • 举报
回复
引用 2 楼 u010434451 的回复:
是说查询都数据都一样,只是月份的不一样吗?这样的话可以用存储过程.
不会写啊 。 会写就不问了。
u010434451 2013-06-05
  • 打赏
  • 举报
回复
是说查询都数据都一样,只是月份的不一样吗?这样的话可以用存储过程.
xlong224 2013-06-05
  • 打赏
  • 举报
回复
来人帮帮忙啊。
哥眼神纯洁不 2013-06-05
  • 打赏
  • 举报
回复

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select * from td a 
你看看不做任何处理的数据,就知道是啥意思了
xlong224 2013-06-05
  • 打赏
  • 举报
回复
引用 6 楼 sc273607742 的回复:
[quote=引用 5 楼 xlong224 的回复:] [quote=引用 4 楼 sc273607742 的回复:]

if exists(select 1 from sys.tables where name='tb')
drop table tb

go

with tb(ID,beginTime,endTime,excutedate,week1,week2,week3,week4,week5,week6,week7)as( 
select 1,'8:00','18:00','2013-1-1',1,1,1,1,1,0,0 union all
select 2,'8:30','17:30','2013-5-4',1,1,1,1,1,1,0 union all
select 3,'8:30','18:00','2013-5-20',1,1,1,1,1,0,1)
select * into tb from tb

go

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select convert(date,date,120) 日期,DATEPART(WEEKDAY,date-1) 星期几,
 case when CHARINDEX(convert(varchar,datepart(weekday,date)-1),
 (case when week1=1 then '1' else '' end+
  case when week2=1 then '2' else '' end+
  case when week3=1 then '3' else '' end+
  case when week4=1 then '4' else '' end+
  case when week5=1 then '5' else '' end+
  case when week6=1 then '6' else '' end+
  case when week7=1 then '0' else '' end))>0 then '上班'
  else '不上班'end 是否上班,begintime 上班时间,endtime 下班时间
 from td a 
 where not exists 
 (select 1 from td where a.date=date and ID>a.ID)

/*
日期       星期几      是否上班  上班时间 下班时间
---------- ----------- ------    ----     -----
2013-05-01 3           上班      8:00     18:00
2013-05-02 4           上班      8:00     18:00
2013-05-03 5           上班      8:00     18:00
2013-05-04 6           上班      8:30     17:30
2013-05-05 7           不上班    8:30     17:30
2013-05-06 1           上班      8:30     17:30
2013-05-07 2           上班      8:30     17:30
2013-05-08 3           上班      8:30     17:30
2013-05-09 4           上班      8:30     17:30
2013-05-10 5           上班      8:30     17:30
2013-05-11 6           上班      8:30     17:30
2013-05-12 7           不上班    8:30     17:30
2013-05-13 1           上班      8:30     17:30
2013-05-14 2           上班      8:30     17:30
2013-05-15 3           上班      8:30     17:30
2013-05-16 4           上班      8:30     17:30
2013-05-17 5           上班      8:30     17:30
2013-05-18 6           上班      8:30     17:30
2013-05-19 7           不上班    8:30     17:30
2013-05-20 1           上班      8:30     18:00
2013-05-21 2           上班      8:30     18:00
2013-05-22 3           上班      8:30     18:00
2013-05-23 4           上班      8:30     18:00
2013-05-24 5           上班      8:30     18:00
2013-05-25 6           不上班    8:30     18:00
2013-05-26 7           上班      8:30     18:00
2013-05-27 1           上班      8:30     18:00
2013-05-28 2           上班      8:30     18:00
2013-05-29 3           上班      8:30     18:00
2013-05-30 4           上班      8:30     18:00
2013-05-31 5           上班      8:30     18:00

(31 行受影响)
*/

好费时啊...
大神啊 看了半天没看懂 能和我说下 思路吗 第一个with tc 是去选择月 每一天的日期 第二个with td 拿tc去关联 查出所有的天的配置信息(交叉所有) 查询的 显示要查询的信息 最后一句 where not exists 是什么意思。 [/quote] 每一个with的都能单独执行,你分别执行下吧...[/quote] with 的看懂就最后一个where条件 的 exsits 没看懂。 。
哥眼神纯洁不 2013-06-05
  • 打赏
  • 举报
回复
引用 5 楼 xlong224 的回复:
[quote=引用 4 楼 sc273607742 的回复:]

if exists(select 1 from sys.tables where name='tb')
drop table tb

go

with tb(ID,beginTime,endTime,excutedate,week1,week2,week3,week4,week5,week6,week7)as( 
select 1,'8:00','18:00','2013-1-1',1,1,1,1,1,0,0 union all
select 2,'8:30','17:30','2013-5-4',1,1,1,1,1,1,0 union all
select 3,'8:30','18:00','2013-5-20',1,1,1,1,1,0,1)
select * into tb from tb

go

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select convert(date,date,120) 日期,DATEPART(WEEKDAY,date-1) 星期几,
 case when CHARINDEX(convert(varchar,datepart(weekday,date)-1),
 (case when week1=1 then '1' else '' end+
  case when week2=1 then '2' else '' end+
  case when week3=1 then '3' else '' end+
  case when week4=1 then '4' else '' end+
  case when week5=1 then '5' else '' end+
  case when week6=1 then '6' else '' end+
  case when week7=1 then '0' else '' end))>0 then '上班'
  else '不上班'end 是否上班,begintime 上班时间,endtime 下班时间
 from td a 
 where not exists 
 (select 1 from td where a.date=date and ID>a.ID)

/*
日期       星期几      是否上班  上班时间 下班时间
---------- ----------- ------    ----     -----
2013-05-01 3           上班      8:00     18:00
2013-05-02 4           上班      8:00     18:00
2013-05-03 5           上班      8:00     18:00
2013-05-04 6           上班      8:30     17:30
2013-05-05 7           不上班    8:30     17:30
2013-05-06 1           上班      8:30     17:30
2013-05-07 2           上班      8:30     17:30
2013-05-08 3           上班      8:30     17:30
2013-05-09 4           上班      8:30     17:30
2013-05-10 5           上班      8:30     17:30
2013-05-11 6           上班      8:30     17:30
2013-05-12 7           不上班    8:30     17:30
2013-05-13 1           上班      8:30     17:30
2013-05-14 2           上班      8:30     17:30
2013-05-15 3           上班      8:30     17:30
2013-05-16 4           上班      8:30     17:30
2013-05-17 5           上班      8:30     17:30
2013-05-18 6           上班      8:30     17:30
2013-05-19 7           不上班    8:30     17:30
2013-05-20 1           上班      8:30     18:00
2013-05-21 2           上班      8:30     18:00
2013-05-22 3           上班      8:30     18:00
2013-05-23 4           上班      8:30     18:00
2013-05-24 5           上班      8:30     18:00
2013-05-25 6           不上班    8:30     18:00
2013-05-26 7           上班      8:30     18:00
2013-05-27 1           上班      8:30     18:00
2013-05-28 2           上班      8:30     18:00
2013-05-29 3           上班      8:30     18:00
2013-05-30 4           上班      8:30     18:00
2013-05-31 5           上班      8:30     18:00

(31 行受影响)
*/

好费时啊...
大神啊 看了半天没看懂 能和我说下 思路吗 第一个with tc 是去选择月 每一天的日期 第二个with td 拿tc去关联 查出所有的天的配置信息(交叉所有) 查询的 显示要查询的信息 最后一句 where not exists 是什么意思。 [/quote] 每一个with的都能单独执行,你分别执行下吧...
xlong224 2013-06-05
  • 打赏
  • 举报
回复
引用 4 楼 sc273607742 的回复:

if exists(select 1 from sys.tables where name='tb')
drop table tb

go

with tb(ID,beginTime,endTime,excutedate,week1,week2,week3,week4,week5,week6,week7)as( 
select 1,'8:00','18:00','2013-1-1',1,1,1,1,1,0,0 union all
select 2,'8:30','17:30','2013-5-4',1,1,1,1,1,1,0 union all
select 3,'8:30','18:00','2013-5-20',1,1,1,1,1,0,1)
select * into tb from tb

go

 declare @date datetime='2013-05-01'     --判断月份
 declare @date1 datetime=dateadd(month,1,@date);
 with tc as
 (select date=DATEADD(day,number,@date) 
 from master..spt_values 
 where type='p' and DATEADD(day,number,@date) <@date1)
 ,td as
 (select * from  tc left join tb a on tc.date>=a.excutedate)
 select convert(date,date,120) 日期,DATEPART(WEEKDAY,date-1) 星期几,
 case when CHARINDEX(convert(varchar,datepart(weekday,date)-1),
 (case when week1=1 then '1' else '' end+
  case when week2=1 then '2' else '' end+
  case when week3=1 then '3' else '' end+
  case when week4=1 then '4' else '' end+
  case when week5=1 then '5' else '' end+
  case when week6=1 then '6' else '' end+
  case when week7=1 then '0' else '' end))>0 then '上班'
  else '不上班'end 是否上班,begintime 上班时间,endtime 下班时间
 from td a 
 where not exists 
 (select 1 from td where a.date=date and ID>a.ID)

/*
日期       星期几      是否上班  上班时间 下班时间
---------- ----------- ------    ----     -----
2013-05-01 3           上班      8:00     18:00
2013-05-02 4           上班      8:00     18:00
2013-05-03 5           上班      8:00     18:00
2013-05-04 6           上班      8:30     17:30
2013-05-05 7           不上班    8:30     17:30
2013-05-06 1           上班      8:30     17:30
2013-05-07 2           上班      8:30     17:30
2013-05-08 3           上班      8:30     17:30
2013-05-09 4           上班      8:30     17:30
2013-05-10 5           上班      8:30     17:30
2013-05-11 6           上班      8:30     17:30
2013-05-12 7           不上班    8:30     17:30
2013-05-13 1           上班      8:30     17:30
2013-05-14 2           上班      8:30     17:30
2013-05-15 3           上班      8:30     17:30
2013-05-16 4           上班      8:30     17:30
2013-05-17 5           上班      8:30     17:30
2013-05-18 6           上班      8:30     17:30
2013-05-19 7           不上班    8:30     17:30
2013-05-20 1           上班      8:30     18:00
2013-05-21 2           上班      8:30     18:00
2013-05-22 3           上班      8:30     18:00
2013-05-23 4           上班      8:30     18:00
2013-05-24 5           上班      8:30     18:00
2013-05-25 6           不上班    8:30     18:00
2013-05-26 7           上班      8:30     18:00
2013-05-27 1           上班      8:30     18:00
2013-05-28 2           上班      8:30     18:00
2013-05-29 3           上班      8:30     18:00
2013-05-30 4           上班      8:30     18:00
2013-05-31 5           上班      8:30     18:00

(31 行受影响)
*/

好费时啊...
大神啊 看了半天没看懂 能和我说下 思路吗 第一个with tc 是去选择月 每一天的日期 第二个with td 拿tc去关联 查出所有的天的配置信息(交叉所有) 查询的 显示要查询的信息 最后一句 where not exists 是什么意思。

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧