Solution to a complex problem (not to let him die, answer)

I have two table with the structure below

MASTER_PARAM

Data type

PARAM_ID

NUMBER

PERIOD_FROM

DATE

PERIOD_TO

VARCHAR2 (10 byte)

PRIORITY

NUMBER

UPLOAD_VALUE

NUMBER

PARAM_DETAIL

Data type

PARAM_DTL_ID

NUMBER

PARAM_ID

NUMBER

MODEL_ID

VARCHAR2 (10 byte)

PROFIT_CENTER_ID

NUMBER

DEALER_ID

NUMBER

Now my goal is to insert records into the table Master_Param and Param_detail (for a table of the given source) under the following conditions

(1) for a Model_id - combination of dealer _ID (present in the source table) if the file is already there for the given date range and priority, then the Master_Param be, and the upload_value column will be updated accordingly.

(2) for a Dealer_id model_id if recording isn't here for a given date range and priority was a new record should be inserted to the table with a new param_id Master_Param and later param_detail table must be completed (for the same param_id as it is a foreign to this table key) and model_id-dealer_id information should be inserted in the table come.

Say the source table contains records in the format below

Source_table

Date_From

DATE_TO

MODEL_ID

Dealer_ID

Upload_value

PRIORITY

April 1, 13

30 April 13

1000

11

12

1

April 1, 13

30 April 13

1001

11

20

1

April 1, 13

30 April 13

1002

11

30

1

April 1, 13

30 April 13

1000

12

25

1

April 1, 13

30 April 13

1005

12

21

1

Before insertion

MASTER_PARAM

PARAM_ID

PERIOD_FROM

PERIOD_TO

PRIORITY

UPLOAD_VALUE

1

April 1, 13

30 April 13

1

10

2

April 1, 13

30 April 13

1

10

3

April 1, 13

30 April 13

1

20

4

February 1, 13

28 February 13

1

25

For every param_id there is a matching record in the secondary table

PARAM_DETAIL

PARAM_DTL_ID

PARAM_ID

MODEL_ID

DEALER_ID

101

1

1000

11

102

2

1003

14

103

3

1002

11

104

4

1001

11

After Insertion

MASTER_PARAM

PARAM_ID

PERIOD_FROM

PERIOD_TO

PRIORITY

UPLOAD_VALUE

1

April 1, 13

30 April 13

1

12

Update

2

April 1, 13

30 April 13

1

10

3

April 1, 13

30 April 13

1

30

Update

4

February 1, 13

28 February 13

1

25

5

April 1, 13

30 April 13

1

20

Inserted

6

April 1, 13

30 April 13

1

21

Inserted

PARAM_DETAIL

PARAM_DTL_ID

PARAM_ID

MODEL_ID

DEALER_ID

101

1

1000

11

102

2

1003

14

103

3

1002

11

104

4

1001

11

105

5

1001

11

Inserted

106

6

1005

12

Inserted

Insert multi table and simple update statement will satisfy your requirement.

Here is the illustration with your sample data.

SQL >
SQL > select * from v version $;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11 g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE Production 11.2.0.2.0
AMT for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production

SQL >
SQL > DROP SEQUENCE param_id_seq;

Sequence has fallen.

SQL > DROP SEQUENCE param_dtl_seq;

Sequence has fallen.

SQL > DROP TABLE source_table;

Deleted table.

SQL > DROP TABLE param_detail;

Deleted table.

SQL > DROP TABLE master_param;

Deleted table.

SQL > CREATE SEQUENCE param_id_seq
2. START BY 5
3 MAXVALUE 999999
MINVALUE 4 0;

Order of creation.

SQL > CREATE SEQUENCE param_dtl_seq
2. START WITH 105
3 MAXVALUE 999999
MINVALUE 4 0;

Order of creation.

SQL > CREATE TABLE master_param
() 2
3 param_id NUMBER PRIMARY KEY,
4 period_from DATE,
5 period_to VARCHAR2 (10 BYTE),
6 priority NUMBER,
7 upload_value NUMBER
8  );

Table created.

SQL > CREATE TABLE param_detail
() 2
3 param_dtl_id NUMBER PRIMARY KEY,
NUMBER of REFERENCES of param_id 4 master_param (param_id),
5 model_id VARCHAR2 (10 BYTE),
6 profit_center_id NUMBER,
7 dealer_id NUMBER
8  );

Table created.

SQL > CREATE TABLE source_table
() 2
3 date_from DATE,
4 date_to VARCHAR2 (10),
5 model_id VARCHAR2 (10 BYTE),
6 dealer_id NUMBER,
7 upload_value NUMBER,
8 priority
9  );

Table created.

SQL >-load sample data into master_param
SQL > INSERT INTO master_param
2. SELECT *.
3 FROM (SELECT 1 AS param_id,
4 TO_DATE ('1 April 13', 'DD-MON-YY') AS period_from,
5 30 April 13 ' AS period_to.
6 1 priority,
7 10 AS upload_value
8 DOUBLE
9 UNION ALL
10. SELECT 2 AS param_id,
11 TO_DATE ('1 April 13', 'DD-MON-YY') AS period_from,
12 30 April 13 ' AS period_to.
13 1 priority,
14 10 AS upload_value
15 DOUBLE
16 UNION ALL
17 SELECT 3 AS param_id,
18 TO_DATE ('1 April 13', 'DD-MON-YY') AS period_from,
19-30 April 13 ' AS period_to.
20 1 priority,
21-20 AS upload_value
22 DOUBLE
23 UNION ALL
24 SELECT 4 AS param_id,
25 TO_DATE ('1 February 13', 'DD-MON-YY') AS period_from,
26-28 February 13 ' AS period_to.
27 1 priority,
28 25 AS upload_value
29 FROM DUAL) temp;

4 lines were created.

SQL >-load sample data into Param_detail
SQL >
SQL > INSERT INTO param_detail
2. SELECT *.
3 FROM (SELECT 101 AS param_dtl_id,
4-1 AS param_id,
5 1000 AS model_id,
6 NULL AS profit_center_id,
7-11 AS dealer_id
8 DOUBLE
9 UNION ALL
10. SELECT 102 AS param_dtl_id,
11 2 AS param_id,
12 1003 AS model_id,
NULL AS profit_center_id 13,
14 14 AS dealer_id
15 DOUBLE
16 UNION ALL
17 SELECT 103 AS param_dtl_id,
18 3 AS param_id,
19 1002 AS model_id,
20 NULL AS profit_center_id,
21-11 AS dealer_id
22 DOUBLE
23 UNION ALL
24 SELECT 104 AS param_dtl_id,
25 4 AS param_id,
26 1001 AS model_id,
NULL AS profit_center_id 27,
28-11 AS dealer_id
29 FROM DUAL) temp;

4 lines were created.

SQL >
SQL >
SQL >-load sample data in table_source
SQL >
SQL > INSERT INTO source_table
2. SELECT *.
3 (SELECT TO_DATE ('1 April 13', 'DD-MON-YY') AS date_from,)
4 30 April 13 ' AS date_to,.
5 1000 AS model_id,
6-11 AS dealer_id,
7 12 AS upload_value,
8 1 priority
9 DOUBLE
ANY UNION 10
11 SELECT TO_DATE ('1 April 13', 'DD-MON-YY') AS date_from,.
12 30 April 13 ' AS date_to,.
13 1001 AS model_id,
14 11 AS dealer_id,
15-20 AS upload_value,
16 1 priority
17 OF THE DOUBLE
18 UNION ALL
19 SELECT TO_DATE ('1 April 13', 'DD-MON-YY') AS date_from,.
20-30 April 13 ' AS date_to,.
21 1002 AS model_id,
22 11 AS dealer_id,
23-30 AS upload_value,
24 1 priority
25 DOUBLE
26. ANY TRADE UNION
27 SELECT TO_DATE ('1 April 13', 'DD-MON-YY') AS date_from,.
28-30 April 13 ' AS date_to,.
29 1000 AS model_id,
30 12 AS dealer_id,
31 25 AS upload_value,
32 1 priority
33 OF THE DOUBLE
34. ANY TRADE UNION
35 SELECT TO_DATE ('1 April 13', 'DD-MON-YY') AS date_from,.
36 30 April 13 ' AS date_to,.
37 1005 AS model_id,
38 12 AS dealer_id,
39 21 AS upload_value,
40 1 priority
41 DOUBLE) temp;

5 rows created.

SQL >
SQL > COMMIT;

Validation complete.

SQL >
SQL >-table insert Multi
SQL >
SQL > INSERT ALL
2 IN master_param (param_id,
period_from 3,.
period_to 4,.
priority 5,
6 upload_value)
7 VALUES (param_id_seq. NEXTVAL,
date_from 8.
date_to 9,.
priority 10,
11 upload_value)
Param_detail (param_dtl_id 12,
param_id 13,
model_id 14,
15 dealer_id)
16 VALUES (param_dtl_seq. NEXTVAL,
17 param_id_seq. NEXTVAL,
model_id 18,
19 dealer_id)
Date_from SELECTION 20,
date_to 21,
model_id 22,
dealer_id 23,
upload_value 24,
25 priority
26 OF source_table st
27. WHERE DOES NOT EXIST
28 (SELECT 1
29. OF master_param a, param_detail b, c from source_table
30 WHERE a.param_id = b.param_id
31 AND b.model_id = c.model_id
32 AND b.dealer_id = c.dealer_id
33 AND a.period_from = c.date_from
34 AND a.period_to = c.date_to
35 AND a.priority = c.priority
36 AND st. ROWID = c.ROWID);

6 rows created.

SQL >
SQL > COMMIT;

Validation complete.

SQL >
SQL > SELECT * FROM master_param;

PARAM_ID PERIOD_FR PERIOD_TO PRIORITY UPLOAD_VALUE
---------- --------- ---------- ---------- ------------
April 1 1 13 30 April 13 1 10
April 2 1 13 30 April 13 1 10
April 3 1 13 30 April 13 1 20
February 4 1 13 February 28 13 1 25
April 5 1 13 30 April 13 1 20
April 6 1 13 30 April 13 1 25
April 7 1 13 30 April 13 1 21

7 selected lines.

SQL >
SQL >
SQL >
SQL > UPDATE master_param mp
2 SET upload_value =
3 (SELECT c.upload_value
4. FROM master_param a, param_detail b, c from source_table
5 WHERE a.param_id = b.param_id
6 AND b.model_id = c.model_id
7 AND b.dealer_id = c.dealer_id
8 AND a.period_from = c.date_from
9 AND a.period_to = c.date_to
10 AND a.priority = c.priority
11 AND a.param_id = mp.param_id)
12. WHERE THERE IS
13 (SELECT 1
14. FROM master_param a, param_detail b, c from source_table
15 WHERE a.param_id = b.param_id
16 AND b.model_id = c.model_id
17 AND b.dealer_id = c.dealer_id
18 AND a.period_from = c.date_from
19 AND a.period_to = c.date_to
20 AND a.priority = c.priority
21 AND a.param_id = mp.param_id);

5 lines to date.

SQL >
SQL > COMMIT;

Validation complete.

SQL >
SQL > SELECT * FROM master_param;

PARAM_ID PERIOD_FR PERIOD_TO PRIORITY UPLOAD_VALUE
---------- --------- ---------- ---------- ------------
April 1 1 13 30 April 13 1 12
April 2 1 13 30 April 13 1 10
April 3 1 13 30 April 13 1 30
February 4 1 13 February 28 13 1 25
April 5 1 13 30 April 13 1 20
April 6 1 13 30 April 13 1 25
April 7 1 13 30 April 13 1 21

7 selected lines.

SQL >
SQL > SELECT * FROM param_detail;

PARAM_DTL_ID PARAM_ID PROFIT_CENTER_ID DEALER_ID MODEL_ID
------------ ---------- ---------- ---------------- ----------
101          1 1000                                11
102          2 1003                                14
103          3 1002                                11
104          4 1001                                11
105          5 1001                                11
106          6 1000                                12
107          7 1005                                12

7 selected lines.

SQL >
SQL > spool off;

Thank you

GPU

Tags: Database

Similar Questions

Maybe you are looking for

  • Comment on the time of sunset.

    When I put to bed for the alarms of the week, AOS alarms work correctly. However, I noticed that even if I had a scheduled Monday alarm AM, I don't have an alert Sunday night. Sunset alerts have to be compensated forward, not back.

  • improve the readability of the pictures taken on i-phone 6

    People, I use an iPhone 6 to take pictures.  My quality is 11.7 GB and my version is 9.3.2 (13F69).  Can I take a photo and send it to another mac, but I have problems, showing a readable picture.  I liked to retouching photo, but need help.  I'm a n

  • Hard drive Compitabilty for Qosmio G20

    Hi I have a Qosmio G20-139 with a drive of 80 GB H., can I use a standard 2.5 "in this laptop or what model should I use? I noticed that the connector of the standard 2.5 "is different from the one installed in the G20-139

  • "Make sure that all dependencies of VI (including screws vi.lib) are located in the same LLB as the VI you specified.

    I use VBAI 2011 and LV2010. I design a VI using LV and use it for my VBAI app: VBAI will transmit the data to the VI by updating the user interface of inspection. Throughout it works fine until I have add a 'linear curve fit' in the VI - and the erro

  • Unable to start cold the Windows XP computer

    Original title: computer starts not cold.Cold, after that turn off for a while, the computer doen't start when turned on, have to light several times so he can start the boot process. Fan comes on, but it doesn't progress thruogh the start-up. Once i