Maison > Article > base de données > 一个计算日期连续性的脚本_MySQL
1 USE [test] 2 GO 3 4 --一个计算日期连续性的脚本 5 --需求:查找指定天数的连续交易的记录,比如连续2天发生,连续3天发生等等。 6 /****** Object: Table [dbo].[liu_shui] Script Date: 11/11/2015 14:36:05 ******/ 7 SET ANSI_NULLS ON 8 GO 9 SET QUOTED_IDENTIFIER ON 10 GO 11 CREATE TABLE [dbo].[liu_shui]( 12 [d_date] [date] NOT NULL, 13 [i_id] [int] IDENTITY(1,1) NOT NULL, 14 CONSTRAINT [PK_liu_shui] PRIMARY KEY CLUSTERED 15 ( 16 [i_id] ASC 17 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 18 ) ON [PRIMARY] 19 GO 20 SET IDENTITY_INSERT [dbo].[liu_shui] ON 21 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA73A0B00 AS Date), 1) 22 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA83A0B00 AS Date), 2) 23 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA93A0B00 AS Date), 3) 24 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xAA3A0B00 AS Date), 4) 25 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xAB3A0B00 AS Date), 5) 26 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xAD3A0B00 AS Date), 6) 27 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xAF3A0B00 AS Date), 8) 28 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB03A0B00 AS Date), 9) 29 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB23A0B00 AS Date), 11) 30 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB33A0B00 AS Date), 12) 31 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB53A0B00 AS Date), 13) 32 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB83A0B00 AS Date), 14) 33 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB93A0B00 AS Date), 15) 34 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBA3A0B00 AS Date), 16) 35 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBB3A0B00 AS Date), 17) 36 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBC3A0B00 AS Date), 18) 37 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBD3A0B00 AS Date), 19) 38 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBE3A0B00 AS Date), 20) 39 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBF3A0B00 AS Date), 21) 40 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC03A0B00 AS Date), 22) 41 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC13A0B00 AS Date), 23) 42 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC23A0B00 AS Date), 24) 43 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC33A0B00 AS Date), 25) 44 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC43A0B00 AS Date), 26) 45 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC53A0B00 AS Date), 27) 46 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC63A0B00 AS Date), 28) 47 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC73A0B00 AS Date), 29) 48 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC83A0B00 AS Date), 30) 49 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC93A0B00 AS Date), 31) 50 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCB3A0B00 AS Date), 32) 51 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCC3A0B00 AS Date), 33) 52 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCD3A0B00 AS Date), 34) 53 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCE3A0B00 AS Date), 35) 54 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCF3A0B00 AS Date), 36) 55 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD03A0B00 AS Date), 37) 56 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD13A0B00 AS Date), 38) 57 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD33A0B00 AS Date), 39) 58 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD63A0B00 AS Date), 40) 59 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD73A0B00 AS Date), 41) 60 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD83A0B00 AS Date), 42) 61 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD93A0B00 AS Date), 43) 62 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDA3A0B00 AS Date), 44) 63 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDB3A0B00 AS Date), 45) 64 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDC3A0B00 AS Date), 46) 65 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDD3A0B00 AS Date), 47) 66 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDE3A0B00 AS Date), 48) 67 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDF3A0B00 AS Date), 49) 68 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE03A0B00 AS Date), 50) 69 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE13A0B00 AS Date), 51) 70 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE23A0B00 AS Date), 52) 71 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE33A0B00 AS Date), 53) 72 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE43A0B00 AS Date), 54) 73 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE53A0B00 AS Date), 55) 74 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE63A0B00 AS Date), 56) 75 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE73A0B00 AS Date), 57) 76 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE83A0B00 AS Date), 58) 77 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEA3A0B00 AS Date), 59) 78 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEB3A0B00 AS Date), 60) 79 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEC3A0B00 AS Date), 61) 80 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xED3A0B00 AS Date), 62) 81 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEE3A0B00 AS Date), 63) 82 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEF3A0B00 AS Date), 64) 83 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF03A0B00 AS Date), 65) 84 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF23A0B00 AS Date), 66) 85 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF53A0B00 AS Date), 67) 86 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF63A0B00 AS Date), 68) 87 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF73A0B00 AS Date), 69) 88 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF83A0B00 AS Date), 70) 89 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF93A0B00 AS Date), 71) 90 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFA3A0B00 AS Date), 72) 91 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFB3A0B00 AS Date), 73) 92 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFC3A0B00 AS Date), 74) 93 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFD3A0B00 AS Date), 75) 94 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFE3A0B00 AS Date), 76) 95 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFF3A0B00 AS Date), 77) 96 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x003B0B00 AS Date), 78) 97 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x013B0B00 AS Date), 79) 98 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x023B0B00 AS Date), 80) 99 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x033B0B00 AS Date), 81) 100 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x043B0B00 AS Date), 82) 101 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x053B0B00 AS Date), 83) 102 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x063B0B00 AS Date), 84) 103 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x073B0B00 AS Date), 85) 104 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x093B0B00 AS Date), 86) 105 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0A3B0B00 AS Date), 87) 106 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0B3B0B00 AS Date), 88) 107 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0C3B0B00 AS Date), 89) 108 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0D3B0B00 AS Date), 90) 109 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0E3B0B00 AS Date), 91) 110 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0F3B0B00 AS Date), 92) 111 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x113B0B00 AS Date), 93) 112 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x143B0B00 AS Date), 94) 113 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x153B0B00 AS Date), 95) 114 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x163B0B00 AS Date), 96) 115 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x173B0B00 AS Date), 97) 116 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x183B0B00 AS Date), 98) 117 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x193B0B00 AS Date), 99) 118 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x1A3B0B00 AS Date), 100) 119 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x1B3B0B00 AS Date), 101) 120 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x1C3B0B00 AS Date), 102) 121 GO 122 print 'Processed 100 total records' 123 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x1D3B0B00 AS Date), 103) 124 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x1E3B0B00 AS Date), 104) 125 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x1F3B0B00 AS Date), 105) 126 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x203B0B00 AS Date), 106) 127 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x213B0B00 AS Date), 107) 128 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x223B0B00 AS Date), 108) 129 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x233B0B00 AS Date), 109) 130 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x243B0B00 AS Date), 110) 131 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x263B0B00 AS Date), 111) 132 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x273B0B00 AS Date), 112) 133 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x283B0B00 AS Date), 113) 134 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x293B0B00 AS Date), 114) 135 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x2A3B0B00 AS Date), 115) 136 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x2B3B0B00 AS Date), 116) 137 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x2C3B0B00 AS Date), 117) 138 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x2E3B0B00 AS Date), 118) 139 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x313B0B00 AS Date), 119) 140 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x323B0B00 AS Date), 120) 141 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x333B0B00 AS Date), 121) 142 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x343B0B00 AS Date), 122) 143 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x353B0B00 AS Date), 123) 144 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x363B0B00 AS Date), 124) 145 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x373B0B00 AS Date), 125) 146 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x383B0B00 AS Date), 126) 147 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x393B0B00 AS Date), 127) 148 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x3A3B0B00 AS Date), 128) 149 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x3B3B0B00 AS Date), 129) 150 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x3C3B0B00 AS Date), 130) 151 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x3D3B0B00 AS Date), 131) 152 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x3E3B0B00 AS Date), 132) 153 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x3F3B0B00 AS Date), 133) 154 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x403B0B00 AS Date), 134) 155 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x413B0B00 AS Date), 135) 156 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x423B0B00 AS Date), 136) 157 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x433B0B00 AS Date), 137) 158 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x453B0B00 AS Date), 138) 159 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x463B0B00 AS Date), 139) 160 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x473B0B00 AS Date), 140) 161 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x483B0B00 AS Date), 141) 162 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x493B0B00 AS Date), 142) 163 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x4A3B0B00 AS Date), 143) 164 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x4B3B0B00 AS Date), 144) 165 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x4D3B0B00 AS Date), 145) 166 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x503B0B00 AS Date), 146) 167 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x513B0B00 AS Date), 147) 168 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x523B0B00 AS Date), 148) 169 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x533B0B00 AS Date), 149) 170 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x543B0B00 AS Date), 150) 171 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x553B0B00 AS Date), 151) 172 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x563B0B00 AS Date), 152) 173 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x573B0B00 AS Date), 153) 174 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x583B0B00 AS Date), 154) 175 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x593B0B00 AS Date), 155) 176 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x5A3B0B00 AS Date), 156) 177 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x5B3B0B00 AS Date), 157) 178 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x5C3B0B00 AS Date), 158) 179 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x5D3B0B00 AS Date), 159) 180 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x5E3B0B00 AS Date), 160) 181 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x5F3B0B00 AS Date), 161) 182 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x603B0B00 AS Date), 162) 183 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x613B0B00 AS Date), 163) 184 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x633B0B00 AS Date), 164) 185 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x643B0B00 AS Date), 165) 186 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x653B0B00 AS Date), 166) 187 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x663B0B00 AS Date), 167) 188 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x673B0B00 AS Date), 168) 189 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x683B0B00 AS Date), 169) 190 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x693B0B00 AS Date), 170) 191 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x6B3B0B00 AS Date), 171) 192 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x6E3B0B00 AS Date), 172) 193 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x6F3B0B00 AS Date), 173) 194 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x703B0B00 AS Date), 174) 195 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x713B0B00 AS Date), 175) 196 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x723B0B00 AS Date), 176) 197 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x733B0B00 AS Date), 177) 198 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x743B0B00 AS Date), 178) 199 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x753B0B00 AS Date), 179) 200 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x763B0B00 AS Date), 180) 201 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x773B0B00 AS Date), 181) 202 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x783B0B00 AS Date), 182) 203 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x793B0B00 AS Date), 183) 204 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x7A3B0B00 AS Date), 184) 205 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x7B3B0B00 AS Date), 185) 206 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x7C3B0B00 AS Date), 186) 207 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x7D3B0B00 AS Date), 187) 208 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x7E3B0B00 AS Date), 188) 209 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x7F3B0B00 AS Date), 189) 210 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x803B0B00 AS Date), 190) 211 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x823B0B00 AS Date), 191) 212 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x833B0B00 AS Date), 192) 213 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x843B0B00 AS Date), 193) 214 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x853B0B00 AS Date), 194) 215 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x863B0B00 AS Date), 195) 216 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x873B0B00 AS Date), 196) 217 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x883B0B00 AS Date), 197) 218 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x8A3B0B00 AS Date), 198) 219 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x8D3B0B00 AS Date), 199) 220 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x8E3B0B00 AS Date), 200) 221 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x8F3B0B00 AS Date), 201) 222 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x903B0B00 AS Date), 202) 223 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x913B0B00 AS Date), 203) 224 GO 225 print 'Processed 200 total records' 226 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x923B0B00 AS Date), 204) 227 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x933B0B00 AS Date), 205) 228 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x943B0B00 AS Date), 206) 229 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x953B0B00 AS Date), 207) 230 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x963B0B00 AS Date), 208) 231 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x973B0B00 AS Date), 209) 232 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x983B0B00 AS Date), 210) 233 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x993B0B00 AS Date), 211) 234 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x9A3B0B00 AS Date), 212) 235 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x9B3B0B00 AS Date), 213) 236 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x9C3B0B00 AS Date), 214) 237 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x9D3B0B00 AS Date), 215) 238 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x9E3B0B00 AS Date), 216) 239 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA03B0B00 AS Date), 217) 240 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA13B0B00 AS Date), 218) 241 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA23B0B00 AS Date), 219) 242 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA33B0B00 AS Date), 220) 243 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA43B0B00 AS Date), 221) 244 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA53B0B00 AS Date), 222) 245 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA63B0B00 AS Date), 223) 246 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xA83B0B00 AS Date), 224) 247 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xAB3B0B00 AS Date), 225) 248 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xAC3B0B00 AS Date), 226) 249 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xAD3B0B00 AS Date), 227) 250 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xAE3B0B00 AS Date), 228) 251 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xAF3B0B00 AS Date), 229) 252 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB03B0B00 AS Date), 230) 253 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB13B0B00 AS Date), 231) 254 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB23B0B00 AS Date), 232) 255 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB33B0B00 AS Date), 233) 256 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB43B0B00 AS Date), 234) 257 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB53B0B00 AS Date), 235) 258 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB63B0B00 AS Date), 236) 259 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB73B0B00 AS Date), 237) 260 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB83B0B00 AS Date), 238) 261 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xB93B0B00 AS Date), 239) 262 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBA3B0B00 AS Date), 240) 263 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBB3B0B00 AS Date), 241) 264 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBC3B0B00 AS Date), 242) 265 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBD3B0B00 AS Date), 243) 266 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xBF3B0B00 AS Date), 244) 267 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC03B0B00 AS Date), 245) 268 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC13B0B00 AS Date), 246) 269 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC23B0B00 AS Date), 247) 270 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC33B0B00 AS Date), 248) 271 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC43B0B00 AS Date), 249) 272 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC53B0B00 AS Date), 250) 273 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xC73B0B00 AS Date), 251) 274 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCA3B0B00 AS Date), 252) 275 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCB3B0B00 AS Date), 253) 276 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCC3B0B00 AS Date), 254) 277 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCD3B0B00 AS Date), 255) 278 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCE3B0B00 AS Date), 256) 279 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xCF3B0B00 AS Date), 257) 280 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD03B0B00 AS Date), 258) 281 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD13B0B00 AS Date), 259) 282 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD23B0B00 AS Date), 260) 283 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD33B0B00 AS Date), 261) 284 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD43B0B00 AS Date), 262) 285 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD53B0B00 AS Date), 263) 286 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD63B0B00 AS Date), 264) 287 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD73B0B00 AS Date), 265) 288 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD83B0B00 AS Date), 266) 289 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xD93B0B00 AS Date), 267) 290 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDA3B0B00 AS Date), 268) 291 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDB3B0B00 AS Date), 269) 292 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDC3B0B00 AS Date), 270) 293 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDE3B0B00 AS Date), 271) 294 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xDF3B0B00 AS Date), 272) 295 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE03B0B00 AS Date), 273) 296 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE13B0B00 AS Date), 274) 297 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE23B0B00 AS Date), 275) 298 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE33B0B00 AS Date), 276) 299 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE43B0B00 AS Date), 277) 300 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE63B0B00 AS Date), 278) 301 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xE93B0B00 AS Date), 279) 302 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEA3B0B00 AS Date), 280) 303 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEB3B0B00 AS Date), 281) 304 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEC3B0B00 AS Date), 282) 305 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xED3B0B00 AS Date), 283) 306 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEE3B0B00 AS Date), 284) 307 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xEF3B0B00 AS Date), 285) 308 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF03B0B00 AS Date), 286) 309 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF13B0B00 AS Date), 287) 310 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF23B0B00 AS Date), 288) 311 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF33B0B00 AS Date), 289) 312 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF43B0B00 AS Date), 290) 313 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF53B0B00 AS Date), 291) 314 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF63B0B00 AS Date), 292) 315 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF73B0B00 AS Date), 293) 316 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF83B0B00 AS Date), 294) 317 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xF93B0B00 AS Date), 295) 318 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFA3B0B00 AS Date), 296) 319 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFC3B0B00 AS Date), 297) 320 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFD3B0B00 AS Date), 298) 321 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFE3B0B00 AS Date), 299) 322 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0xFF3B0B00 AS Date), 300) 323 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x003C0B00 AS Date), 301) 324 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x013C0B00 AS Date), 302) 325 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x023C0B00 AS Date), 303) 326 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x043C0B00 AS Date), 304) 327 GO 328 print 'Processed 300 total records' 329 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x073C0B00 AS Date), 305) 330 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x083C0B00 AS Date), 306) 331 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x093C0B00 AS Date), 307) 332 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0A3C0B00 AS Date), 308) 333 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0B3C0B00 AS Date), 309) 334 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0C3C0B00 AS Date), 310) 335 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0D3C0B00 AS Date), 311) 336 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0E3C0B00 AS Date), 312) 337 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x0F3C0B00 AS Date), 313) 338 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x103C0B00 AS Date), 314) 339 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x113C0B00 AS Date), 315) 340 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x123C0B00 AS Date), 316) 341 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x133C0B00 AS Date), 317) 342 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x143C0B00 AS Date), 318) 343 INSERT [dbo].[liu_shui] ([d_date], [i_id]) VALUES (CAST(0x153C0B00 AS Date), 319) 344 SET IDENTITY_INSERT [dbo].[liu_shui] OFF 345 346 347 --输入任何一个日期,查找其之前和之后的连续的日期序列 348 create function Func_Continuity(@d_start_date date)--+1时点之后的日期序列,-1时点之前的日期序列 349 returns table 350 as 351 return 352 ( 353 with cte_end 354 as 355 ( 356 select i_id,d_date 357 from dbo.liu_shui 358 where d_date=@d_start_date 359 union all 360 select a.i_id,a.d_date 361 from dbo.liu_shui a 362 inner join cte_end as b 363 on DATEDIFF(day,b.d_date,a.d_date)=1--时点之后的日期序列 364 ) 365 ,cte_begin 366 as 367 ( 368 select i_id,d_date 369 from dbo.liu_shui 370 where d_date=@d_start_date 371 union all 372 select a.i_id,a.d_date 373 from dbo.liu_shui a 374 inner join cte_begin as b 375 on DATEDIFF(day,b.d_date,a.d_date)=-1--时点之前的日期序列 376 ) 377 select * from cte_begin 378 union 379 select * from cte_end 380 ) 381 go 382 383 384 select i_id,d_date from 385 ( 386 select a.i_id,a.d_date,b.d_date as b_d_date from dbo.liu_shui as a 387 cross apply Func_Continuity(a.d_date) as b 388 ) as tt 389 group by i_id,d_date 390 having COUNT(b_d_date)>=3--大于等于几就是连续多少天及以上发生 391 order by d_date