刚有个项目,需要查询水位数据表中的水位信息,及查询降雨量表中统计时段降雨量的数据,以计算出日降雨量,而且时段是前一天8时到后一天8时总共24个小时。
两个子查询:
1、根据当前时间判断统计前天8时到今天8时还是大前天8时到前天8时的时段雨量;
select STCD,SUM(DRP) as drp2 from Jialiang.dbo.ST_PPTN_R where STCD=\'1\'and TM>case when Datename(hour,GetDate())>=8 then CONVERT(varchar(100), GETDATE()-1, 23)+\' 8:00\' else CONVERT(varchar(100), GETDATE()-2, 23)+\' 8:00\' end and TM<case when Datename(hour,GetDate())>=8 then CONVERT(varchar(100), GETDATE(), 23)+\' 8:00\' else CONVERT(varchar(100), GETDATE()-1, 23)+\' 8:00\'end GROUP BY STCD
2、查询最新的水位数据
select STCD,Z from Jialiang.dbo.ST_RIVER_R where STCD=\'1\' and TM=(select MAX(TM) from Jialiang.dbo.ST_RIVER_R where STCD=\'1\')
最后把这两个查询结果根据设备编码联合起来
select a.Z,b.drp2 from(select STCD,SUM(DRP) as drp2 from Jialiang.dbo.ST_PPTN_R where STCD=\'1\'and TM>case when Datename(hour,GetDate())>=8 then CONVERT(varchar(100), GETDATE()-1, 23)+\' 8:00\' else CONVERT(varchar(100), GETDATE()-2, 23)+\' 8:00\' end and TM<case when Datename(hour,GetDate())>=8 then CONVERT(varchar(100), GETDATE(), 23)+\' 8:00\' else CONVERT(varchar(100), GETDATE()-1, 23)+\' 8:00\'end GROUP BY STCD) as b, (select STCD,Z from Jialiang.dbo.ST_RIVER_R where STCD=\'1\' and TM=(select MAX(TM) from Jialiang.dbo.ST_RIVER_R where STCD=\'1\')) as a where a.STCD=b.STCD
本文参考了
SQL获取当前时间的方法:http://www.cnblogs.com/weiqt/articles/2040800.html
DateName()还可以获得到小时、时间、秒、星期几、第几周,分别如下:
Select Datename(hour,GetDate()) Select Datename(minute,GetDate()) Select Datename(second,GetDate()) Select Datename(weekDay,GetDate()) Select Datename(week,GetDate())
使用Convert()函数:
select convert(char(10),GetDate(),120) as Date
* 第3个参数就是用来设置日期类型数据的显示样式的,下面介绍几种样式的参数:
100 mm dd yyyy
101 mm/dd/yyyy
102 yyyy.mm.dd
103 dd/mm/yyyy
106 dd mm yyyy
108 hh:mi:ss(时间)
111 yyyy/mm/dd
112 yyyymmdd
120 yyyy-mm-dd hh:mm:ss
23 yyyy-mm-dd
1 SELECT CONVERT(varchar(100), GETDATE(), 0) 05 9 2011 9:12AM 2 SELECT CONVERT(varchar(100), GETDATE(), 1) 05/09/11 3 SELECT CONVERT(varchar(100), GETDATE(), 2) 11.05.09 4 SELECT CONVERT(varchar(100), GETDATE(), 3) 09/05/11 5 SELECT CONVERT(varchar(100), GETDATE(), 4) 09.05.11 6 SELECT CONVERT(varchar(100), GETDATE(), 5) 09-05-11 7 SELECT CONVERT(varchar(100), GETDATE(), 6) 09 05 11 8 SELECT CONVERT(varchar(100), GETDATE(), 7) 05 09, 11 9 SELECT CONVERT(varchar(100), GETDATE(), 8) 09:13:14 10 SELECT CONVERT(varchar(100), GETDATE(), 9) 05 9 2011 9:13:14:670AM 11 SELECT CONVERT(varchar(100), GETDATE(), 10) 05-09-11 12 SELECT CONVERT(varchar(100), GETDATE(), 11) 11/05/09 13 SELECT CONVERT(varchar(100), GETDATE(), 12) 110509 14 SELECT CONVERT(varchar(100), GETDATE(), 13) 09 05 2011 09:13:14:670 15 SELECT CONVERT(varchar(100), GETDATE(), 14) 09:13:14:670 16 SELECT CONVERT(varchar(100), GETDATE(), 20) 2011-05-09 09:13:14 17 SELECT CONVERT(varchar(100), GETDATE(), 21) 2011-05-09 09:13:14.670 18 SELECT CONVERT(varchar(100), GETDATE(), 22) 05/09/11 9:15:33 AM 19 SELECT CONVERT(varchar(100), GETDATE(), 23) 2011-05-09 20 SELECT CONVERT(varchar(100), GETDATE(), 24) 09:15:33 21 SELECT CONVERT(varchar(100), GETDATE(), 25) 2011-05-09 09:15:33.140 22 SELECT CONVERT(varchar(100), GETDATE(), 100) 05 9 2011 9:15AM 23 SELECT CONVERT(varchar(100), GETDATE(), 101) 05/09/2011 24 SELECT CONVERT(varchar(100), GETDATE(), 102) 2011.05.09 25 SELECT CONVERT(varchar(100), GETDATE(), 103) 09/05/2011 26 SELECT CONVERT(varchar(100), GETDATE(), 104) 09.05.2011 27 SELECT CONVERT(varchar(100), GETDATE(), 105) 09-05-2011 28 SELECT CONVERT(varchar(100), GETDATE(), 106) 09 05 2011 29 SELECT CONVERT(varchar(100), GETDATE(), 107) 05 09, 2011 30 SELECT CONVERT(varchar(100), GETDATE(), 108) 09:16:38 31 SELECT CONVERT(varchar(100), GETDATE(), 109) 05 9 2011 9:16:38:543AM 32 SELECT CONVERT(varchar(100), GETDATE(), 110) 05-09-2011 33 SELECT CONVERT(varchar(100), GETDATE(), 111) 2011/05/09 34 SELECT CONVERT(varchar(100), GETDATE(), 112) 20110509 35 SELECT CONVERT(varchar(100), GETDATE(), 113) 09 05 2011 09:17:19:857 36 SELECT CONVERT(varchar(100), GETDATE(), 114) 09:17:19:857 37 SELECT CONVERT(varchar(100), GETDATE(), 120) 2011-05-09 09:17:19 38 SELECT CONVERT(varchar(100), GETDATE(), 121) 2011-05-09 09:17:19.857 39 SELECT CONVERT(varchar(100), GETDATE(), 126) 2011-05-09T09:17:19.857 40 SELECT CONVERT(varchar(100), GETDATE(), 130) 6 ????? ??????? 1432 9:17:19:857AM 41 SELECT CONVERT(varchar(100), GETDATE(), 131) 6/06/1432 9:17:19:857AM
SQL case when.. else..end 用法:
WHERE WATRDJ>= case when P05_TRDJ_FM is not null then V_TRDJ_FM else WATRDJ end and WATRDJ<= case when P05_TRDJ_TO is not null then V_TRDJ_TO else WATRDJ end
SQL联合查询两个表方法:
同一实例下的数据库表是可以直接引用的,只要加上数据库名就可以了
如
select a.* from A.dbo.AA as a,B.dbo.AA as b where a.col1 = b.col1 and a.col2 <> b.col2