|
| 1 | +create table t1 (a int, b varchar(32)); |
| 2 | +insert into t1 values |
| 3 | +(4,'aaaa' ), (7,'bb'), (1,'ccc'), (4,'dd'); |
| 4 | +insert into t1 values |
| 5 | +(3,'eee'), (7,'bb'), (1,'fff'), (4,'ggg'); |
| 6 | +with recursive |
| 7 | +a1(a,b) as |
| 8 | +(select * from t1 where t1.a>3 |
| 9 | +union |
| 10 | +select * from b1 where b1.a >3 |
| 11 | +union |
| 12 | +select * from c1 where c1.a>3), |
| 13 | +b1(a,b) as |
| 14 | +(select * from a1 where a1.b > 'ccc' |
| 15 | +union |
| 16 | +select * from c1 where c1.b > 'ddd'), |
| 17 | +c1(a,b) as |
| 18 | +(select * from a1 where a1.a<6 and a1.b< 'zz' |
| 19 | +union |
| 20 | +select * from b1 where b1.b > 'auu') |
| 21 | +select * from c1; |
| 22 | +ERROR HY000: No anchors for recursive WITH element 'b1' |
| 23 | +drop table t1; |
| 24 | +create table folks(id int, name char(32), dob date, father int, mother int); |
| 25 | +insert into folks values |
| 26 | +(100, 'Vasya', '2000-01-01', 20, 30), |
| 27 | +(20, 'Dad', '1970-02-02', 10, 9), |
| 28 | +(30, 'Mom', '1975-03-03', 8, 7), |
| 29 | +(10, 'Grandpa Bill', '1940-04-05', null, null), |
| 30 | +(9, 'Grandma Ann', '1941-10-15', null, null), |
| 31 | +(25, 'Uncle Jim', '1968-11-18', 8, 7), |
| 32 | +(98, 'Sister Amy', '2001-06-20', 20, 30), |
| 33 | +(8, 'Grandma Sally', '1943-08-23', 5, 6), |
| 34 | +(6, 'Grandgrandma Martha', '1923-05-17', null, null), |
| 35 | +(67, 'Cousin Eddie', '1992-02-28', 25, 27), |
| 36 | +(27, 'Auntie Melinda', '1971-03-29', null, null); |
| 37 | +with recursive |
| 38 | +ancestors |
| 39 | +as |
| 40 | +( |
| 41 | +select * |
| 42 | +from folks |
| 43 | +where name = 'Vasya' and dob = '2000-01-01' |
| 44 | + union |
| 45 | +select p.id, p.name, p.dob, p.father, p.mother |
| 46 | +from folks as p, ancestors AS a |
| 47 | +where p.id = a.father or p.id = a.mother |
| 48 | +) |
| 49 | +select * from ancestors; |
| 50 | +id name dob father mother |
| 51 | +100 Vasya 2000-01-01 20 30 |
| 52 | +20 Dad 1970-02-02 10 9 |
| 53 | +30 Mom 1975-03-03 8 7 |
| 54 | +10 Grandpa Bill 1940-04-05 NULL NULL |
| 55 | +9 Grandma Ann 1941-10-15 NULL NULL |
| 56 | +8 Grandma Sally 1943-08-23 5 6 |
| 57 | +6 Grandgrandma Martha 1923-05-17 NULL NULL |
| 58 | +with recursive |
| 59 | +ancestors |
| 60 | +as |
| 61 | +( |
| 62 | +select p.* |
| 63 | +from folks as p, ancestors AS a |
| 64 | +where p.id = a.father or p.id = a.mother |
| 65 | +union |
| 66 | +select * |
| 67 | +from folks |
| 68 | +where name = 'Vasya' and dob = '2000-01-01' |
| 69 | +) |
| 70 | +select * from ancestors; |
| 71 | +id name dob father mother |
| 72 | +100 Vasya 2000-01-01 20 30 |
| 73 | +20 Dad 1970-02-02 10 9 |
| 74 | +30 Mom 1975-03-03 8 7 |
| 75 | +10 Grandpa Bill 1940-04-05 NULL NULL |
| 76 | +9 Grandma Ann 1941-10-15 NULL NULL |
| 77 | +8 Grandma Sally 1943-08-23 5 6 |
| 78 | +6 Grandgrandma Martha 1923-05-17 NULL NULL |
| 79 | +with recursive |
| 80 | +ancestors |
| 81 | +as |
| 82 | +( |
| 83 | +select * |
| 84 | +from folks |
| 85 | +where name = 'Cousin Eddie' |
| 86 | + union |
| 87 | +select p.* |
| 88 | +from folks as p, ancestors as a |
| 89 | +where p.id = a.father or p.id = a.mother |
| 90 | +) |
| 91 | +select * from ancestors; |
| 92 | +id name dob father mother |
| 93 | +67 Cousin Eddie 1992-02-28 25 27 |
| 94 | +25 Uncle Jim 1968-11-18 8 7 |
| 95 | +27 Auntie Melinda 1971-03-29 NULL NULL |
| 96 | +8 Grandma Sally 1943-08-23 5 6 |
| 97 | +6 Grandgrandma Martha 1923-05-17 NULL NULL |
| 98 | +with recursive |
| 99 | +ancestors |
| 100 | +as |
| 101 | +( |
| 102 | +select * |
| 103 | +from folks |
| 104 | +where name = 'Vasya' or name='Sister Amy' |
| 105 | + union |
| 106 | +select p.* |
| 107 | +from folks as p, ancestors as a |
| 108 | +where p.id = a.father or p.id = a.mother |
| 109 | +) |
| 110 | +select * from ancestors; |
| 111 | +id name dob father mother |
| 112 | +100 Vasya 2000-01-01 20 30 |
| 113 | +98 Sister Amy 2001-06-20 20 30 |
| 114 | +20 Dad 1970-02-02 10 9 |
| 115 | +30 Mom 1975-03-03 8 7 |
| 116 | +10 Grandpa Bill 1940-04-05 NULL NULL |
| 117 | +9 Grandma Ann 1941-10-15 NULL NULL |
| 118 | +8 Grandma Sally 1943-08-23 5 6 |
| 119 | +6 Grandgrandma Martha 1923-05-17 NULL NULL |
| 120 | +with recursive |
| 121 | +prev_gen |
| 122 | +as |
| 123 | +( |
| 124 | +select folks.* |
| 125 | +from folks, prev_gen |
| 126 | +where folks.id=prev_gen.father or folks.id=prev_gen.mother |
| 127 | +union |
| 128 | +select * |
| 129 | +from folks |
| 130 | +where name='Vasya' |
| 131 | +), |
| 132 | +ancestors |
| 133 | +as |
| 134 | +( |
| 135 | +select * |
| 136 | +from folks |
| 137 | +where name='Vasya' |
| 138 | + union |
| 139 | +select * |
| 140 | +from ancestors |
| 141 | +union |
| 142 | +select * |
| 143 | +from prev_gen |
| 144 | +) |
| 145 | +select ancestors.name, ancestors.dob from ancestors; |
| 146 | +name dob |
| 147 | +Vasya 2000-01-01 |
| 148 | +Dad 1970-02-02 |
| 149 | +Mom 1975-03-03 |
| 150 | +Grandpa Bill 1940-04-05 |
| 151 | +Grandma Ann 1941-10-15 |
| 152 | +Grandma Sally 1943-08-23 |
| 153 | +Grandgrandma Martha 1923-05-17 |
| 154 | +with recursive |
| 155 | +descendants |
| 156 | +as |
| 157 | +( |
| 158 | +select * |
| 159 | +from folks |
| 160 | +where name = 'Grandpa Bill' |
| 161 | + union |
| 162 | +select folks.* |
| 163 | +from folks, descendants as d |
| 164 | +where d.id=folks.father or d.id=folks.mother |
| 165 | +) |
| 166 | +select * from descendants; |
| 167 | +id name dob father mother |
| 168 | +10 Grandpa Bill 1940-04-05 NULL NULL |
| 169 | +20 Dad 1970-02-02 10 9 |
| 170 | +100 Vasya 2000-01-01 20 30 |
| 171 | +98 Sister Amy 2001-06-20 20 30 |
| 172 | +with recursive |
| 173 | +descendants |
| 174 | +as |
| 175 | +( |
| 176 | +select * |
| 177 | +from folks |
| 178 | +where name = 'Grandma Sally' |
| 179 | + union |
| 180 | +select folks.* |
| 181 | +from folks, descendants as d |
| 182 | +where d.id=folks.father or d.id=folks.mother |
| 183 | +) |
| 184 | +select * from descendants; |
| 185 | +id name dob father mother |
| 186 | +8 Grandma Sally 1943-08-23 5 6 |
| 187 | +30 Mom 1975-03-03 8 7 |
| 188 | +25 Uncle Jim 1968-11-18 8 7 |
| 189 | +100 Vasya 2000-01-01 20 30 |
| 190 | +98 Sister Amy 2001-06-20 20 30 |
| 191 | +67 Cousin Eddie 1992-02-28 25 27 |
| 192 | +with recursive |
| 193 | +ancestors |
| 194 | +as |
| 195 | +( |
| 196 | +select * |
| 197 | +from folks |
| 198 | +where name = 'Vasya' and dob = '2000-01-01' |
| 199 | + union |
| 200 | +select p.* |
| 201 | +from folks as p, ancestors AS a |
| 202 | +where p.id = a.father OR p.id = a.mother |
| 203 | +) |
| 204 | +select * |
| 205 | +from ancestors t1, ancestors t2 |
| 206 | +where exists (select * from ancestors a |
| 207 | +where a.father=t1.id AND a.mother=t2.id); |
| 208 | +id name dob father mother id name dob father mother |
| 209 | +20 Dad 1970-02-02 10 9 30 Mom 1975-03-03 8 7 |
| 210 | +10 Grandpa Bill 1940-04-05 NULL NULL 9 Grandma Ann 1941-10-15 NULL NULL |
| 211 | +with |
| 212 | +ancestor_couples(husband, h_dob, wife, w_dob) |
| 213 | +as |
| 214 | +( |
| 215 | +with recursive |
| 216 | +ancestors |
| 217 | +as |
| 218 | +( |
| 219 | +select * |
| 220 | +from folks |
| 221 | +where name = 'Vasya' |
| 222 | + union |
| 223 | +select p.* |
| 224 | +from folks as p, ancestors AS a |
| 225 | +where p.id = a.father OR p.id = a.mother |
| 226 | +) |
| 227 | +select t1.name, t1.dob, t2.name, t2.dob |
| 228 | +from ancestors t1, ancestors t2 |
| 229 | +where exists (select * from ancestors a |
| 230 | +where a.father=t1.id AND a.mother=t2.id) |
| 231 | +) |
| 232 | +select * from ancestor_couples; |
| 233 | +husband h_dob wife w_dob |
| 234 | +Dad 1970-02-02 Mom 1975-03-03 |
| 235 | +Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15 |
| 236 | +with recursive |
| 237 | +ancestors |
| 238 | +as |
| 239 | +( |
| 240 | +select * |
| 241 | +from folks |
| 242 | +where name = 'Vasya' and dob = '2000-01-01' |
| 243 | + union |
| 244 | +select p.* |
| 245 | +from folks as p, ancestors AS a |
| 246 | +where p.id = a.father |
| 247 | +union |
| 248 | +select p.* |
| 249 | +from folks as p, ancestors AS a |
| 250 | +where p.id = a.mother |
| 251 | +) |
| 252 | +select * from ancestors; |
| 253 | +id name dob father mother |
| 254 | +100 Vasya 2000-01-01 20 30 |
| 255 | +20 Dad 1970-02-02 10 9 |
| 256 | +30 Mom 1975-03-03 8 7 |
| 257 | +9 Grandma Ann 1941-10-15 NULL NULL |
| 258 | +10 Grandpa Bill 1940-04-05 NULL NULL |
| 259 | +8 Grandma Sally 1943-08-23 5 6 |
| 260 | +6 Grandgrandma Martha 1923-05-17 NULL NULL |
| 261 | +with recursive |
| 262 | +ancestor_couples(h_id, h_name, h_dob, h_father, h_mother, |
| 263 | +w_id, w_name, w_dob, w_father, w_mother) |
| 264 | +as |
| 265 | +( |
| 266 | +select h.*, w.* |
| 267 | +from folks h, folks w, coupled_ancestors a |
| 268 | +where a.father = h.id AND a.mother = w.id |
| 269 | +union |
| 270 | +select h.*, w.* |
| 271 | +from folks v, folks h, folks w |
| 272 | +where v.name = 'Vasya' and |
| 273 | +(v.father = h.id AND v.mother= w.id) |
| 274 | +), |
| 275 | +coupled_ancestors (id, name, dob, father, mother) |
| 276 | +as |
| 277 | +( |
| 278 | +select h_id, h_name, h_dob, h_father, h_mother |
| 279 | +from ancestor_couples |
| 280 | +union |
| 281 | +select w_id, w_name, w_dob, w_father, w_mother |
| 282 | +from ancestor_couples |
| 283 | +) |
| 284 | +select h_name, h_dob, w_name, w_dob |
| 285 | +from ancestor_couples; |
| 286 | +h_name h_dob w_name w_dob |
| 287 | +Dad 1970-02-02 Mom 1975-03-03 |
| 288 | +Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15 |
| 289 | +drop table folks; |
0 commit comments