Advertisement
xxdf344

Untitled

Nov 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.89 KB | None | 0 0
  1. CREATE VIEW ana_view_accerta AS
  2. SELECT distinct
  3. (
  4. CASE
  5. WHEN
  6. (
  7. select
  8. param_value
  9. from
  10. aut_global_params
  11. where
  12. app_prefix = 'ana'
  13. and param_key like '%gestione_numero_individuale%'
  14. )
  15. = '0'
  16. then
  17. per.pkid
  18. else
  19. per.numero_individuo
  20. end
  21. ) AS COD_PERSONA, an1.codice_fiscale as COD_FISCALE, an1.flg_codice_fiscale AS validaz_codfis, an1.cognome as cognome, an1.nome as nome, to_char(an1.data_nascita, 'dd/MM/yyyy') AS data_nascita,
  22. case
  23. when
  24. an1.sesso = 1
  25. then
  26. 'M'
  27. when
  28. an1.sesso = 2
  29. then
  30. 'F'
  31. else
  32. null
  33. end
  34. as sesso, coalesce(accres.cap, areres.cap, comres.cap) as cap, comres.des as COMUNE_RESID , stares.des as NAZIONE_RESID , '' as luogo_residenza, cast(
  35. case
  36. when
  37. comnas.st_provincia is null
  38. then
  39. null
  40. else
  41. TO_CHAR(comres.cod, 'fm000000')
  42. end
  43. as varchar(6)) as COD_COMUNE_RESID , cast( TO_CHAR(pronas.cod, 'fm000') as varchar(3)) as COD_PROV_RESID, cast( TO_CHAR(stares.cod, 'fm000') as varchar(3)) as COD_NAZIONE_RESID , cast(substr (
  44. CASE
  45. WHEN
  46. areres.pkid IS NOT NULL
  47. THEN
  48. (
  49. CASE
  50. WHEN
  51. areres.specie IS NULL
  52. THEN
  53. areres.des
  54. ELSE
  55. rtrim(ltrim(rtrim(ltrim(areres.specie)) || ' ' || rtrim(ltrim(areres.des))))
  56. END
  57. )
  58. ELSE
  59. accres.des_gen
  60. END
  61. , 1, 40) as varchar(100)) AS via, accres.civico as NUM_CIVICO, accres.lettera as BIS, accres.piano as piano, accres.scala as scala, accres.interno as interno, cast(rtrim(ltrim(an1con.cognome || ' ' || an1con.nome)) as varchar(255)) as cognomeNomeConiuge,
  62. (
  63. select
  64. CASE
  65. WHEN
  66. (
  67. select
  68. param_value
  69. from
  70. aut_global_params
  71. where
  72. app_prefix = 'ana'
  73. and param_key like '%gestione_numero_individuale%'
  74. )
  75. = '0'
  76. then
  77. perpd.pkid
  78. else
  79. perpd.numero_individuo
  80. end
  81. from
  82. ANA_PERSONE perpd
  83. inner join
  84. AN1_ANAGRAFE_UNICA an2
  85. on perpd.id_anagrafe_unica = an2.pkid
  86. where
  87. an2.pkid = an1con.pkid
  88. )
  89. AS COD_CONIUGE,
  90. case
  91. when
  92. an1.sesso = 1
  93. then
  94. stc.des_m
  95. else
  96. stc.des_f
  97. end
  98. as STATO_CIVILE, stc.cod_istat as COD_STATO_CIVILE_INTERNO, stc.cod_anpr as COD_STATO_CIVILE,
  99. case
  100. when
  101. fam.flg_con = 0
  102. then
  103. cast( rel.cod as varchar(5))
  104. else
  105. rel.cod_anpr_c
  106. end
  107. as COD_RELPAR,
  108. case
  109. when
  110. an1.sesso = 1
  111. then
  112. rel.des_m
  113. else
  114. rel.des_f
  115. end
  116. as RAPP_PARENTELA,
  117. case
  118. when
  119. rel.cod = 1
  120. then
  121. 'S'
  122. else
  123. 'N'
  124. end
  125. as CAPOFAMIGLIA_SI_NO, cast(
  126. case
  127. when
  128. stacit.cod = 1
  129. then
  130. 1
  131. else
  132. stacit.cod
  133. end
  134. as smallint) as COD_CITTADINANZA , stacit.des_cittadinanza_generica as CITTADINANZA , stacit.flg_ue as COMUNITARIO_SI_NO , percit.data_ini as data_cittadinanza, cast(
  135. case
  136. when
  137. comnas.st_provincia is null
  138. then
  139. null
  140. else
  141. TO_CHAR(comnas.cod, 'fm000000')
  142. end
  143. as varchar(6)) as cod_comune_nascita, cast( TO_CHAR(pronas.cod, 'fm000') as varchar(3)) as cod_provincia_nascita, cast( TO_CHAR(stanas.cod, 'fm000') as varchar(3)) as cod_stato_estero_nascita,
  144. case
  145. when
  146. comnas.st_provincia is null
  147. then
  148. comnas.des
  149. else
  150. null
  151. end
  152. as des_comune_estero_nascita,
  153. case
  154. when
  155. comnas.st_provincia is null
  156. then
  157. stanas.des
  158. else
  159. null
  160. end
  161. as stato_estero_nascita, pronas.des as provincia_nascita, comattnas.des as comune_atto_nascita,
  162. case
  163. when
  164. pernas.st_comune_atto = cominst.static_id
  165. then
  166. pernas.numero
  167. else
  168. pernas.numero_t
  169. end
  170. as numero_atto_nascita, cast(
  171. case
  172. when
  173. pernas.st_comune_atto = cominst.static_id
  174. then
  175. pernas.parte
  176. else
  177. pernas.parte_t
  178. end
  179. as varchar(255)) as parte_nascita,
  180. case
  181. when
  182. pernas.st_comune_atto = cominst.static_id
  183. then
  184. pernas.serie
  185. else
  186. pernas.serie_t
  187. end
  188. as serie_nascita, cast(
  189. case
  190. when
  191. pernas.st_comune_atto = cominst.static_id
  192. then
  193. pernas.anno
  194. else
  195. pernas.anno_t
  196. end
  197. as smallint) as anno_atto_nascita,
  198. case
  199. when
  200. pernas.st_comune_atto = cominst.static_id
  201. then
  202. pernas.ufficio
  203. else
  204. pernas.ufficio_t
  205. end
  206. as ufficio_nascita, cast(an1pd.nominativo as varchar(250)) as PADRE, an1pd.codice_fiscale as COD_FISCALE_PADRE ,
  207. (
  208. select
  209. CASE
  210. WHEN
  211. (
  212. select
  213. param_value
  214. from
  215. aut_global_params
  216. where
  217. app_prefix = 'ana'
  218. and param_key like '%gestione_numero_individuale%'
  219. )
  220. = '0'
  221. then
  222. perpd.pkid
  223. else
  224. perpd.numero_individuo
  225. end
  226. from
  227. ANA_PERSONE perpd
  228. inner join
  229. AN1_ANAGRAFE_UNICA an2
  230. on perpd.id_anagrafe_unica = an2.pkid
  231. where
  232. an2.pkid = an1pd.pkid
  233. )
  234. AS COD_PADRE, an1md.codice_fiscale as COD_FISCALE_MADRE , cast(an1md.nominativo as varchar(250)) as MADRE,
  235. (
  236. select
  237. CASE
  238. WHEN
  239. (
  240. select
  241. param_value
  242. from
  243. aut_global_params
  244. where
  245. app_prefix = 'ana'
  246. and param_key like '%gestione_numero_individuale%'
  247. )
  248. = '0'
  249. then
  250. perpd.pkid
  251. else
  252. perpd.numero_individuo
  253. end
  254. from
  255. ANA_PERSONE perpd
  256. inner join
  257. AN1_ANAGRAFE_UNICA an2
  258. on perpd.id_anagrafe_unica = an2.pkid
  259. where
  260. an2.pkid = an1md.pkid
  261. )
  262. AS COD_MADRE, cast(
  263. CASE
  264. WHEN
  265. per.flg_cessato = 0
  266. and fam.flg_aire = 0
  267. THEN
  268. 'RESIDENTE'
  269. WHEN
  270. per.flg_cessato = 0
  271. and fam.flg_aire = 1
  272. THEN
  273. 'AIRE'
  274. WHEN
  275. per.flg_cessato = 1
  276. AND
  277. (
  278. ((mot_apr_fin.gruppo * 100 + mot_apr_fin.tipo * 10 + mot_apr_fin.cod) IN
  279. (
  280. 123, 123
  281. )
  282. )
  283. OR
  284. (
  285. (mot_aire_fin.gruppo * 100 + mot_aire_fin.tipo * 10 + mot_aire_fin.cod) IN
  286. (
  287. 123, 123
  288. )
  289. )
  290. )
  291. THEN
  292. 'EMIGRATO'
  293. WHEN
  294. per.flg_cessato = 1
  295. AND
  296. (
  297. ((mot_apr_fin.gruppo * 100 + mot_apr_fin.tipo * 10 + mot_apr_fin.cod) IN
  298. (
  299. 125, 127, 144
  300. )
  301. )
  302. OR
  303. (
  304. (mot_aire_fin.gruppo * 100 + mot_aire_fin.tipo * 10 + mot_aire_fin.cod) IN
  305. (
  306. 125, 127, 144
  307. )
  308. )
  309. )
  310. THEN
  311. 'IRREPERIBILE'
  312. WHEN
  313. per.flg_cessato = 1
  314. AND
  315. (
  316. ((mot_apr_fin.gruppo * 100 + mot_apr_fin.tipo * 10 + mot_apr_fin.cod) IN
  317. (
  318. 121, 142
  319. )
  320. )
  321. OR
  322. (
  323. (mot_aire_fin.gruppo * 100 + mot_aire_fin.tipo * 10 + mot_aire_fin.cod) IN
  324. (
  325. 121, 142
  326. )
  327. )
  328. )
  329. THEN
  330. 'DECEDUTO'
  331. ELSE
  332. 'NON ISCRITTO'
  333. END
  334. as varchar(215)) AS DESC_POSIZIONE_ANAGá ,
  335. case
  336. when
  337. permat.st_comune_atto = cominst.static_id
  338. then
  339. permat.numero
  340. else
  341. permat.numero_t
  342. end
  343. as numero_atto_matrimonio, cast(
  344. case
  345. when
  346. permat.st_comune_atto = cominst.static_id
  347. then
  348. permat.parte
  349. else
  350. permat.parte_t
  351. end
  352. as varchar(255)) as parte_matrimonio,
  353. case
  354. when
  355. permat.st_comune_atto = cominst.static_id
  356. then
  357. permat.serie
  358. else
  359. permat.serie_t
  360. end
  361. as serie_matrimonio, cast(
  362. case
  363. when
  364. permat.st_comune_atto = cominst.static_id
  365. then
  366. permat.anno
  367. else
  368. permat.anno_t
  369. end
  370. as smallint) as anno_matrimonio,
  371. case
  372. when
  373. permat.st_comune_atto = cominst.static_id
  374. then
  375. permat.ufficio
  376. else
  377. permat.ufficio_t
  378. end
  379. as ufficio_matrimonio, cast(
  380. case
  381. when
  382. commat.st_provincia is not null
  383. then
  384. TO_CHAR(commat.cod, 'fm000000')
  385. else
  386. null
  387. end
  388. as varchar(6)) as cod_comune_matrimonio, commat.des as luogo_matrimonio, cast(permat.data_eve as date) as data_matrimonio, cast(perdiv.data_eve as date) as data_decorrenza_divorzio, cast(perdiv.data_sentenza as date) as data_sentenza_divorzio, perdiv.num_sentenza as numero_sentenza_divorzio, cast(TO_CHAR(comtrbdiv.cod, 'fm000000') as varchar(6)) as cod_com_tribunale_divor, cast(null as varchar(255)) as tipo_divorzio, cast(null as date) as data_atto_divorzio,
  389. case
  390. when
  391. perdiv.st_comune_atto = cominst.static_id
  392. then
  393. perdiv.numero
  394. else
  395. null
  396. end
  397. as numero_atto_divorzio, cast(
  398. case
  399. when
  400. perdiv.st_comune_atto = cominst.static_id
  401. then
  402. perdiv.parte
  403. else
  404. null
  405. end
  406. as varchar(255)) as parte_divorzio,
  407. case
  408. when
  409. perdiv.st_comune_atto = cominst.static_id
  410. then
  411. perdiv.serie
  412. else
  413. null
  414. end
  415. as serie_divorzio,
  416. case
  417. when
  418. perdiv.st_comune_atto = cominst.static_id
  419. then
  420. perdiv.volume
  421. else
  422. null
  423. end
  424. as volume_divorzio,
  425. case
  426. when
  427. perdiv.st_comune_atto = cominst.static_id
  428. then
  429. perdiv.ufficio
  430. else
  431. null
  432. end
  433. as ufficio_divorzio, cast(null as date) as data_atto_vedovanza,
  434. case
  435. when
  436. perved.st_comune_atto = cominst.static_id
  437. then
  438. perved.numero
  439. else
  440. null
  441. end
  442. as numero_atto_vedovanza, cast(
  443. case
  444. when
  445. perved.st_comune_atto = cominst.static_id
  446. then
  447. perved.parte
  448. else
  449. null
  450. end
  451. as varchar(255)) as parte_vedovanza,
  452. case
  453. when
  454. perved.st_comune_atto = cominst.static_id
  455. then
  456. perved.serie
  457. else
  458. null
  459. end
  460. as serie_vedovanza,
  461. case
  462. when
  463. perved.st_comune_atto = cominst.static_id
  464. then
  465. perved.volume
  466. else
  467. null
  468. end
  469. as volume_vedovanza,
  470. case
  471. when
  472. perved.st_comune_atto = cominst.static_id
  473. then
  474. perved.ufficio
  475. else
  476. null
  477. end
  478. as ufficio_vedovanza, cast(
  479. case
  480. when
  481. commor.st_provincia is not null
  482. then
  483. TO_CHAR(commor.cod, 'fm000000')
  484. else
  485. null
  486. end
  487. as varchar(6)) as codice_istat_comune_morte, cast(permor.data_eve as date) as data_morte, cast(permor.numero as integer) as numero_atto_morte, cast(permor.parte as varchar(255)) as parte_morte, cast(permor.serie as varchar(255)) as serie_morte, cast(permor.ufficio as varchar(255)) as ufficio_morte, cast(permor.anno as smallint) as anno_morte, prof.des as professione, studio.cod_anpr as cod_studio, cast(
  488. case
  489. when
  490. perci.numero_cie is null
  491. then
  492. perci.sig_ci || cast(perci.num_ci as varchar(11))
  493. else
  494. perci.numero_cie
  495. end
  496. as varchar(21)) as numero_ci,
  497. case
  498. when
  499. perci.numero_cie is null
  500. then
  501. 'CI'
  502. else
  503. 'CIE'
  504. end
  505. as tipo_ci, cast(perci.data_eve as date) as data_ci, cast(perci.data_scadenza as date) as data_scadenza_ci, perci.flg_espatrio as ci_valida_espatrio, fam.numero_famiglia as cod_famiglia, TO_CHAR(comaprini.cod, 'fm000000') as COD_COMUNE_IMMIGR_APR, cast(comaprini.des as varchar(50)) as COMUNE_IMMIGR_APR, aprini.data_ini as DATA_IMMIGR,
  506. CASE
  507. WHEN
  508. mot_apr_fin.cod > 1
  509. THEN
  510. TO_CHAR(comaprfin.cod, 'fm000000')
  511. ELSE
  512. cast ( null as varchar(6))
  513. END
  514. AS COD_COMUNE_EMIGR_APR , cast(
  515. CASE
  516. WHEN
  517. mot_apr_fin.cod > 1
  518. THEN
  519. comaprfin.des
  520. ELSE
  521. null
  522. END
  523. as varchar(50)) AS COMUNE_EMIGR_APR ,
  524. CASE
  525. WHEN
  526. mot_apr_fin.cod > 1
  527. THEN
  528. aprfin.data_ini
  529. ELSE
  530. cast ( null as date)
  531. END
  532. AS data_EMIGR_APR ,
  533. CASE
  534. WHEN
  535. mot_apr_fin.cod != 2
  536. then
  537. TO_CHAR(comairefin.cod, 'fm000000')
  538. ELSE
  539. cast ( null as varchar(6))
  540. END
  541. AS COD_COMUNE_EMIGR_AIRE , cast(
  542. CASE
  543. WHEN
  544. mot_apr_fin.cod != 2
  545. then
  546. comairefin.des
  547. ELSE
  548. null
  549. END
  550. as varchar(50)) AS comune_EMIGR_AIRE,
  551. CASE
  552. WHEN
  553. mot_apr_fin.cod != 2
  554. then
  555. airefin.data_ini
  556. ELSE
  557. cast ( null as date)
  558. END
  559. AS data_EMIGR_aire , TO_CHAR(comaireini.cod, 'fm000000') AS COD_COMUNE_IMMIGR_AIRE , cast( comaireini.des as varchar(50)) AS comune_IMMIGR_AIRE , aireini.data_ini AS data_IMMIGR_AIRE ,
  560. CASE
  561. WHEN
  562. fam.flg_aire = 0
  563. THEN
  564. aprini.data_ini
  565. ELSE
  566. aireini.data_ini
  567. END
  568. AS DTA_ISCRIZ_ANAG, sogg.numero as numero_soggiorno, sogg.data_rilascio as data_rilascio_soggiorno ,
  569. case
  570. when
  571. sogg.tipo = 1
  572. then
  573. 'Permesso di soggiorno'
  574. when
  575. sogg.tipo = 2
  576. then
  577. 'Carta di soggiorno'
  578. else
  579. cast(null as varchar(20))
  580. end
  581. as tipo_soggiorno, TO_CHAR(comsog.cod, 'fm000000') AS COD_COMUNE_soggiorno , comsog.des AS COMUNE_soggiorno
  582. FROM
  583. AN1_RECAPITI rec
  584. left outer join
  585. TER_ACCESSI accres
  586. on rec.id_accesso = accres.pkid
  587. left outer join
  588. TER_AREE areres
  589. on accres.st_area = areres.static_id
  590. left outer join
  591. TER_COMUNI comres
  592. on accres.st_comune = comres.static_id
  593. left outer join
  594. TER_STATI stares
  595. on comres.st_stato = stares.static_id, ANA_FAMIGLIE_PERSONE fp
  596. inner join
  597. ANA_PERSONE per
  598. on fp.id_persona = per.pkid
  599. inner join
  600. AN1_ANAGRAFE_UNICA an1
  601. on per.id_anagrafe_unica = an1.pkid
  602. left outer join
  603. TER_COMUNI comnas
  604. on an1.st_comune_nascita = comnas.static_id
  605. left outer join
  606. TER_PROVINCE pronas
  607. on comnas.st_provincia = pronas.static_id
  608. left outer join
  609. TER_STATI stanas
  610. on comnas.st_stato = stanas.static_id
  611. inner join
  612. ANA_FAMIGLIE fam
  613. on fp.id_famiglia = fam.pkid
  614. left outer join
  615. ANA_PERSONE_STATO_CIVILE perstc
  616. on perstc.id_persona = per.pkid
  617. and perstc.flg_cessato = 0
  618. and perstc.data_fin is null
  619. left outer join
  620. ANA_STATO_CIVILE stc
  621. on stc.pkid = perstc.id_stato_civile
  622. left outer join
  623. ANA_RELAZIONI rel
  624. on rel.pkid = fp.id_relazione
  625. left outer join
  626. ANA_PERSONE_CITTADINANZA percit
  627. on percit.id_persona = per.pkid
  628. and percit.flg_cessato = 0
  629. and percit.data_fin is null
  630. left outer join
  631. TER_STATI stacit
  632. on stacit.static_id = percit.st_stato_cittadinanza
  633. left outer join
  634. ANA_PERSONE_NASCITA pernas
  635. on pernas.id_persona = per.pkid
  636. and pernas.flg_Cessato = 0
  637. and pernas.data_fin is null
  638. left outer join
  639. TER_COMUNI comattnas
  640. on pernas.st_comune_atto = comattnas.static_id
  641. left join
  642. an1_anagrafe_unica an1md
  643. on an1md.pkid = pernas.id_an1_madre
  644. left join
  645. an1_anagrafe_unica an1pd
  646. on an1pd.pkid = pernas.id_an1_padre
  647. left outer join
  648. ANA_PERSONE_MATRIMONIO permat
  649. on permat.id_persona = per.pkid
  650. and permat.flg_Cessato = 0
  651. and permat.data_fin is null
  652. and permat.tipo = 0
  653. left outer join
  654. AN1_ANAGRAFE_UNICA an1con
  655. on an1con.pkid = permat.id_an1_coniuge
  656. left outer join
  657. TER_COMUNI comattmat
  658. on permat.st_comune_atto = comattmat.static_id
  659. left outer join
  660. TER_COMUNI commat
  661. on permat.st_comune = commat.static_id
  662. left outer join
  663. ANA_PERSONE_DIVORZIO perdiv
  664. on perdiv.id_persona = per.pkid
  665. and perdiv.flg_cessato = 0
  666. and perdiv.data_fin is null
  667. and perdiv.tipo = 0
  668. left outer join
  669. TER_COMUNI comattdiv
  670. on perdiv.st_comune_atto = comattdiv.static_id
  671. left outer join
  672. TER_TRIBUNALI trbdiv
  673. on perdiv.id_tribunale = trbdiv.pkid
  674. left outer join
  675. TER_COMUNI comtrbdiv
  676. on comtrbdiv.pkid = trbdiv.id_comune
  677. left outer join
  678. ANA_PERSONE_VEDOVANZA perved
  679. on perved.id_persona = per.pkid
  680. and perved.flg_cessato = 0
  681. and perved.data_fin is null
  682. and perved.tipo = 0
  683. left outer join
  684. ANA_PERSONE_PROFESSIONI prof
  685. on prof.id_persona = per.pkid
  686. and prof.flg_cessato = 0
  687. and prof.data_fin is null
  688. left join
  689. ana_cond_prof cprof
  690. on cprof.pkid = prof.id_cond_prof
  691. left outer join
  692. ANA_PERSONE_STUDIO titst
  693. on titst.id_persona = per.pkid
  694. and titst.flg_cessato = 0
  695. and titst.data_fin is null
  696. left outer join
  697. ana_studio studio
  698. on titst.id_studio = studio.pkid
  699. left outer join
  700. ANA_PERSONE_CI perci
  701. on perci.id_persona = per.pkid
  702. and perci.flg_cessato = 0
  703. and perci.data_fin is null
  704. left outer join
  705. ANA_PERSONE_MORTE permor
  706. on permor.id_persona = per.pkid
  707. and permor.flg_cessato = 0
  708. and permor.data_fin is null
  709. left outer join
  710. TER_COMUNI commor
  711. on permor.st_comune = commor.static_id
  712. LEFT JOIN
  713. ana_persone_apr_ini aprini
  714. ON aprini.id_persona = per.pkid
  715. AND aprini.data_reg is not null
  716. AND aprini.data_ini <= sysdate
  717. AND
  718. (
  719. aprini.data_fin > sysdate
  720. OR aprini.data_fin IS NULL
  721. )
  722. left join
  723. ter_Comuni comaprini
  724. on comaprini.static_id = aprini.st_comune_prov
  725. left join
  726. ter_Stati staprini
  727. on staprini.static_id = aprini.st_stato_prov
  728. LEFT JOIN
  729. ana_persone_apr_fin aprfin
  730. ON aprfin.id_persona = per.pkid
  731. AND aprfin.data_ini <= sysdate
  732. AND
  733. (
  734. aprfin.data_fin > sysdate
  735. OR aprfin.data_fin IS NULL
  736. )
  737. left join
  738. ter_Comuni comaprfin
  739. on comaprfin.static_id = aprfin.st_comune_dest
  740. left join
  741. ter_Stati staaprfin
  742. on staaprfin.static_id = comaprfin.st_stato
  743. left outer join
  744. ana_motivi mot_apr_fin
  745. on mot_apr_fin.pkid = aprfin.id_motivo
  746. LEFT JOIN
  747. ana_persone_aire_ini aireini
  748. ON aireini.id_persona = per.pkid
  749. AND aireini.data_reg is not null
  750. AND aireini.data_ini <= sysdate
  751. AND
  752. (
  753. aireini.data_fin > sysdate
  754. OR aireini.data_fin IS NULL
  755. )
  756. left join
  757. ter_Comuni comaireini
  758. on comaireini.static_id = aireini.st_comune_prov
  759. left outer join
  760. ana_motivi mot_aire_ini
  761. on mot_aire_ini.pkid = aireini.id_motivo
  762. LEFT JOIN
  763. ana_persone_aire_fin airefin
  764. ON airefin.id_persona = per.pkid
  765. AND airefin.data_ini <= sysdate
  766. AND
  767. (
  768. airefin.data_fin > sysdate
  769. OR airefin.data_fin IS NULL
  770. )
  771. left join
  772. Ter_Comuni comairefin
  773. on comairefin.static_id = airefin.st_comune_dest
  774. left join
  775. Ter_Stati staairefin
  776. on staairefin.static_id = comairefin.st_stato
  777. left outer join
  778. ana_motivi mot_aire_fin
  779. on mot_aire_fin.pkid = airefin.id_motivo
  780. left outer join
  781. ANA_PERSONE_SOGGIORNO persog
  782. on persog.id_persona = per.pkid
  783. and persog.flg_cessato = 0
  784. and persog.data_fin is null
  785. left outer join
  786. ANA_SOGGIORNO sogg
  787. on persog.st_soggiorno = sogg.static_id
  788. left outer join
  789. TER_COMUNI comsog
  790. on sogg.st_comune_questura = comsog.static_id , aut_global_params parm
  791. inner join
  792. ter_comuni cominst
  793. on cominst.pkid = cast(param_value as integer)
  794. where
  795. per.flg_con = 0
  796. and per.id_anagrafe_unica = rec.id_anag_unica
  797. and rec.flg_residenza = 1
  798. and fp.id_persona = fp.id_persona
  799. and rec.flg_residenza = 1
  800. and
  801. (
  802. fp.flg_cessato = 0
  803. or
  804. (
  805. fp.flg_cessato = 1
  806. and not exists
  807. (
  808. select
  809. *
  810. from
  811. Ana_Famiglie_Persone fp1
  812. where
  813. fp1.id_persona = per.pkid
  814. and fp1.data_ini >= fp.data_ini
  815. and fp1.flg_cessato = 0
  816. )
  817. )
  818. )
  819. and
  820. (
  821. (fp.flg_cessato = 0
  822. and rec.data_fin is null)
  823. or
  824. (
  825. fp.flg_cessato = 1
  826. and rec.data_ini < fp.data_ini
  827. and
  828. (
  829. rec.data_fin >= fp.data_ini
  830. or rec.data_fin is null
  831. )
  832. )
  833. )
  834. and an1.pkid not in
  835. (
  836. select
  837. n.id_anagrafe_unica
  838. from
  839. Ana_Non_Iscrivere n
  840. where
  841. n.flg_annullato = 0
  842. )
  843. and fp.data_fin is null
  844. and stacit.cod =
  845. (
  846. (
  847. SELECT
  848. min(stacit2.cod) AS min
  849. FROM
  850. ana_persone_cittadinanza cit2
  851. LEFT JOIN
  852. ter_stati stacit2
  853. ON stacit2.static_id = cit2.st_stato_cittadinanza
  854. WHERE
  855. cit2.id_persona = percit.id_persona
  856. AND
  857. (
  858. cit2.pkid IS NULL
  859. OR cit2.data_fin IS NULL
  860. AND cit2.flg_cessato = 0
  861. )
  862. AND
  863. (
  864. stacit2.pkid IS NULL
  865. OR stacit2.data_fin IS NULL
  866. AND stacit2.flg_cessato = 0
  867. )
  868. )
  869. )
  870. and parm.param_key = 'id_comune_installazione' -- and ROWNUM <= 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement