select * from alg_kenmerk


DROP TABLE CSUN_IMP_PROJECT CASCADE CONSTRAINTS;

CREATE TABLE CSUN_IMP_PROJECT
(
  ACCOUNTCODE     VARCHAR2(14),
  ACCOUNTNAAM     VARCHAR2(60),
  PROJECT         VARCHAR2(14),
  VERVALLEN       VARCHAR2(1),
  BU              VARCHAR2(14),
  DM              NUMBER(3),
  VOORNAAM        VARCHAR2(30),
  VOORLETTERS     VARCHAR2(10),
  TUSSENVOEGSELS  VARCHAR2(15),
  ACHTERNAAM      VARCHAR2(60),
  CODE            VARCHAR2(10),
  OMSCHRIJVING    VARCHAR2(60),
  ADRES           VARCHAR2(35),
  POSTCODE        VARCHAR2(9),
  WOONPLAATS      VARCHAR2(35),
  KENMERK1        VARCHAR2(255),
  KENMERK2        VARCHAR2(255),
  KENMERK3        VARCHAR2(255),
  KENMERK4        VARCHAR2(255),
  KENMERK5        VARCHAR2(255)
)

create table csun_imp_as400 (
acccode VARCHAR2(200),
accoms VARCHAR2(200),
selcode VARCHAR2(200),
geocode VARCHAR2(200),
bu VARCHAR2(200),
kpl VARCHAR2(200),
debnr VARCHAR2(200),
debnaam VARCHAR2(200),
debtav VARCHAR2(200),
debadres VARCHAR2(200),
debplaats VARCHAR2(200),
debplaats2 VARCHAR2(200),
debpc VARCHAR2(200),
prjnr VARCHAR2(200),
contactnaam VARCHAR2(200),
contav VARCHAR2(200),
conadres VARCHAR2(200),
conpc VARCHAR2(200),
conplaats VARCHAR2(200),
contel VARCHAR2(200),
werknaam VARCHAR2(200),
werkadres VARCHAR2(200),
werkpc VARCHAR2(200),
werkplaats VARCHAR2(200),
werktav VARCHAR2(200),
soortklant VARCHAR2(200)
)


/* Formatted on 3/15/2013 1:06:37 PM (QP5 v5.136.908.31019) */
INSERT INTO alg_onrgoedkenmerk (alg_kenmerk_key, alg_onrgoed_key, alg_onrgoedkenmerk_waarde)
     SELECT 1040, alg_locatie_key, MIN (prjnr)
       FROM (SELECT l.alg_locatie_key, i.prjnr
               FROM csun_imp_as400 i, alg_locatie l
              WHERE 1 = 1
                AND UPPER (REPLACE (alg_locatie_adres, ' ', '')) =
                       UPPER (REPLACE (werkadres, ' ', ''))
                AND UPPER (alg_locatie_plaats) = UPPER (werkplaats))
   GROUP BY alg_locatie_key
     HAVING COUNT (alg_locatie_key) = 1

create or replace view csun_v_locatie 
as
select (select alg_onrgoedkenmerk_waarde from alg_onrgoedkenmerk k where alg_onrgoed_key = alg_locatie_key and alg_kenmerk_key = 1040) prjnr, 
(select fac_usrdata_omschr from alg_onrgoedkenmerk k, fac_usrdata ud where alg_onrgoed_key = alg_locatie_key and fac.safe_to_number( alg_onrgoedkenmerk_waarde)= fac_usrdata_key and alg_kenmerk_key = 1000) account, l.* from alg_locatie l

select account, count(*) from csun_v_locatie where prjnr is null 
group by account

select * from csun_imp_as400 where 1=1 
--and upper(accoms) like '%ABN%'
and acccode = '005'

select * from csun_imp_as400 where 1=1 
and upper(accoms) like '%USG%'
--and acccode = '480'

select * from csun_v_locatie where account = 'USG' and prjnr is null

INSERT INTO alg_onrgoedkenmerk (alg_kenmerk_key, alg_onrgoed_key, alg_onrgoedkenmerk_waarde)
     SELECT 1040, il.alg_locatie_key, MIN (il.prjnr)
       FROM (SELECT l.alg_locatie_key, i.prjnr
               FROM csun_imp_as400 i, alg_locatie l
              WHERE 1 = 1
              --  AND UPPER (REPLACE (alg_locatie_omschrijving, ' ', '')) like  'USG%' || UPPER (REPLACE(werkplaats,' ', '')) || '%' 
                AND UPPER (REPLACE (alg_locatie_postcode, ' ', '')) = UPPER (REPLACE(werkpc,' ', '')) 
                --||  UPPER (REPLACE (werkadres, ' ', ''))
                AND acccode = '480') il,  -- USG
         csun_v_locatie cl
       where cl.alg_locatie_key = il.alg_locatie_key
       and cl.prjnr is null
   GROUP BY il.alg_locatie_key
     HAVING COUNT (il.alg_locatie_key) = 1
    
select * from csun_imp_as400 where 1=1 
--and upper(accoms) like '%HUMA%'
and acccode = '475'

select * from csun_v_locatie where account = 'Humanitas'

INSERT INTO alg_onrgoedkenmerk (alg_kenmerk_key, alg_onrgoed_key, alg_onrgoedkenmerk_waarde)
    --
 SELECT 1040, il.alg_locatie_key, MIN (il.prjnr)
       FROM (SELECT l.alg_locatie_key, i.prjnr
               FROM csun_imp_as400 i, alg_locatie l
              WHERE 1 = 1
                AND alg_locatie_upper like 'HUMANITAS%'
                AND UPPER (REPLACE (alg_locatie_post_postcode, ' ', '')) = UPPER (REPLACE (werkpc, ' ', ''))
                AND acccode = '475') il,  -- HUMANITAS
         csun_v_locatie cl
       where cl.alg_locatie_key = il.alg_locatie_key
       and cl.prjnr is null
   GROUP BY il.alg_locatie_key
     HAVING COUNT (il.alg_locatie_key) = 1
   

select * from csun_imp_as400 where 1=1 
and upper(accoms) like '%MANPOW%'
--and acccode = '547'
--and werkadres like '%eukenpad%'

select * from csun_v_locatie where account = 'Manpower'

INSERT INTO alg_onrgoedkenmerk (alg_kenmerk_key, alg_onrgoed_key, alg_onrgoedkenmerk_waarde)
     SELECT 1040, il.alg_locatie_key, MIN (il.prjnr)
       FROM (SELECT l.alg_locatie_key, i.prjnr
               FROM csun_imp_as400 i, alg_locatie l
              WHERE 1 = 1
                AND alg_locatie_upper like 'MANPOWER%'
                AND UPPER (REPLACE (alg_locatie_omschrijving, ' ', '')) like '%' || UPPER (REPLACE (werkplaats, ' ', '') || '%')
                AND acccode = '547') il,  --  MANPOWER
         csun_v_locatie cl
       where cl.alg_locatie_key = il.alg_locatie_key
       and cl.prjnr is null
   GROUP BY il.alg_locatie_key
     HAVING COUNT (il.alg_locatie_key) = 1
     
     
     select * from csun_v_locatie where prjnr is not null
     
     select * from alg_kenmerk
     
     
     select cl.prjnr, account, alg_locatie_omschrijving , werkadres, werkplaats from csun_v_locatie cl, csun_imp_as400 i  where cl.prjnr = i.prjnr
     
     select cl.prjnr, account, alg_locatie_code, alg_locatie_omschrijving  from csun_v_locatie cl  where cl.prjnr is null order by 1,2