剛有個項目,需要查詢水位數據表中的水位信息,及查詢降雨量表中統計時段降雨量的數據,以計算出日降雨量,而且時段是前一天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