
WITH A AS (
SELECT 
*, ROW_NUMBER() OVER (PARTITION BY KARTLAGGNR
     ORDER BY UPDDAT DESC, UTBILDNINGSNIVANR DESC) RADNR FROM 
LD0P0U28)
DELETE FROM A WHERE RADNR=2
;

CREATE TABLE LD0P0U28_97200
 (
    KARTLAGGNR         DECIMAL(9) NOT NULL,
    UTBILDNINGSNIVA    DECIMAL(9) NOT NULL,
    UPDDAT             VARCHAR(8)  NOT NULL,
    UPDSIGN            VARCHAR(10) NOT NULL
);
 
INSERT INTO LD0P0U28_97200 (KARTLAGGNR, UTBILDNINGSNIVA, UPDDAT, UPDSIGN ) SELECT KARTLAGGNR, UTBILDNINGSNIVA, UPDDAT, UPDSIGN FROM LD0P0U28
;

DROP TABLE LD0P0U28
;

SELECT * INTO LD0P0U28 FROM LD0P0U28_97200
;

CREATE UNIQUE INDEX LD0C0U28 ON LD0P0U28 
(
  KARTLAGGNR
)
;

DROP VIEW LD0V0U28
;

CREATE VIEW LD0V0U28 AS SELECT * FROM LD0P0U28
;

GRANT ALL ON LD0V0U28 TO LD0ROANV
;

DROP VIEW LD0VAA0SurveyEducationalLevel 
;

CREATE VIEW LD0VAA0SurveyEducationalLevel AS SELECT
U28.*, 
Q.KLARTEXT AS UTBILDNINGSNIVA_T 
FROM LD0P0U28 U28
LEFT OUTER JOIN LD0P0Q01 Q ON Q.KODTYP = 'LNUTBNIVA' AND Q.KOD = U28.UTBILDNINGSNIVA
;

GRANT ALL ON LD0VAA0SurveyEducationalLevel TO LD0ROANV
;

