I am working on a trigger which needs INSERT INTO with WHERE logic.
I have three tables.
Absence_table:
-----------------------------
| user_id | absence_reason |
-----------------------------
| 1234567 | 40 |
| 1234567 | 50 |
| 1213 | 40 |
| 1314 | 20 |
| 1111 | 20 |
-----------------------------
company_table:
-----------------------------
| user_id | company_id |
-----------------------------
| 1234567 | 10201 |
| 1213 | 10200 |
| 1314 | 10202 |
| 1111 | 10200 |
-----------------------------
employment_table:
--------------------------------------
| user_id | emp_type | emp_no |
--------------------------------------
| 1234567 | Int | 1 |
| 1213 | Int | 2 |
| 1314 | Int | 3 |
| 1111 | Ext | 4 |
--------------------------------------
and finally I have the table out where data should be going only who have emp_type = Int in employment_table and have company_id = 10200
out:
--------------------------------
| employee_id | absence_reason |
--------------------------------
| 1 | 40 |
| 1 | 50 |
| 2 | 40 |
| 3 | 20 |
--------------------------------
Here is my trigger:
CREATE OR REPLACE TRIGGER "INOUT"."ABSENCE_TRIGGER"
AFTER INSERT ON absence_table
FOR EACH ROW
DECLARE
BEGIN
CASE
WHEN INSERTING THEN
INSERT INTO out (absence_reason, employee_id)
VALUES (:NEW.absence_reason, (SELECT employee_id FROM employment_table WHERE user_id = :NEW.user_id)
WHERE user_id IN
(SELECT user_id FROM employment_table WHERE employment_type = 'INT')
AND user_id IN
(SELECT user_id FROM company_table WHERE company_id = '10200');
END CASE;
END absence_trigger;
It is obviously not working and I can't figure out what should I do to make it work. Any suggestions?
change the insert to this:
insert into out (absence_reason, employee_id)
select :NEW.absence_reason, e.emp_no
from employment_table e
inner join company_table c
on c.user_id = e.user_id
where e.user_id = :NEW.user_id
and e.emp_type = 'INT'
and c.company_id = '10200';
which should work. note you had emp_no in your sample structure yet employee_id in the trigger insert too. i've assumed emp_no is right. also emp_type vs employment_type.
Finally in your trigger you have company_id in quotes. Is it really a varchar2? if so OK, if not, don't use quotes.
The parentheses are not balanced. The one for values is not closed. This is the cause of your specific error, but #DazzaL's answer looks like the correct solution.
Related
I want to find out the previous date value in my target table based on 3 columns. The example and scenario is explained in the attached screenshot.
Please help.
This can be done with lag.
select t.*,lag(date) over(partition by id,function_id,key order by date) as prev_date
from tbl t
Check if the below works for you.
> create table vimarsh (id int, function_id int, key string, dt date);
> insert into vimarsh
select 123,342,'test1','2018-10-15'
union all
select 1234,35434,'test2','2018-10-16'
union all
select 2131,8907,'test3','2018-10-17'
union all
select 123,342,'test1','2018-10-18';
> select * from vimarsh;
+-------------+----------------------+--------------+-------------+--+
| vimarsh.id | vimarsh.function_id | vimarsh.key | vimarsh.dt |
+-------------+----------------------+--------------+-------------+--+
| 123 | 342 | test1 | 2018-10-15 |
| 1234 | 35434 | test2 | 2018-10-16 |
| 2131 | 8907 | test3 | 2018-10-17 |
| 123 | 342 | test1 | 2018-10-18 |
+-------------+----------------------+--------------+-------------+--+
> select id, function_id,key, dt, lag(dt) over(partition by id,function_id,key order by dt) as prev_date from vimarsh;
INFO : OK
+-------+--------------+--------+-------------+-------------+--+
| id | function_id | key | dt | prev_date |
+-------+--------------+--------+-------------+-------------+--+
| 123 | 342 | test1 | 2018-10-15 | NULL |
| 123 | 342 | test1 | 2018-10-18 | 2018-10-15 |
| 1234 | 35434 | test2 | 2018-10-16 | NULL |
| 2131 | 8907 | test3 | 2018-10-17 | NULL |
+-------+--------------+--------+-------------+-------------+--+
4 rows selected (35.585 seconds)
I have two tables:
Table 1:
+-----------+-----------+------------------+
| ID | Value | other |
+-----------+-----------+------------------+
| 123456 | 5 | 12 |
| 987654 | 7 | 15 |
| 456789 | 6 | 22 |
+-----------+-----------+------------------+
Table 2:
+-----------+-----------+------------------+
| ID | Type | other |
+-----------+-----------+------------------+
| 123456 | 00 | 2 |
| 123456 | 01 | 6 |
| 123456 | 02 | 4 |
| 987654 | 00 | 7 |
| 987654 | 01 | 8 |
| 456789 | 00 | 6 |
| 456789 | 01 | 16 |
+-----------+-----------+------------------+
Now I perform the inner join:
SELECT
table1.ID, table2.TYPE, table1.value, table2.other
FROM
table1 INNER JOIN table2 ON table1.ID = table2.ID
Here the SQLfiddle
Result Table:
+-----------+-----------+---------+------------------+
| ID | Type | Value | other |
+-----------+-----------+---------+------------------+
| 123456 | 00 | 5 | 2 |
| 123456 | 01 | 5 | 6 |
| 123456 | 02 | 5 | 4 |
| 987654 | 00 | 7 | 7 |
| 987654 | 01 | 7 | 8 |
| 456789 | 00 | 6 | 6 |
| 456789 | 01 | 6 | 16 |
+-----------+-----------+---------+------------------+
This is totally what I expected but not what I need.
Because if I now want to get the Value per ID the Value gets doubled or tripled for the first cause.
Desired Table:
+-----------+-----------+---------+------------------+
| ID | Type | Value | other |
+-----------+-----------+---------+------------------+
| 123456 | 00 | 5 | 2 |
| 123456 | 01 | - | 6 |
| 123456 | 02 | - | 4 |
| 987654 | 00 | 7 | 7 |
| 987654 | 01 | - | 8 |
| 456789 | 00 | 6 | 6 |
| 456789 | 01 | - | 16 |
+-----------+-----------+---------+------------------+
I tried to achieve a similar output by counting the rows per id and dividing the sum of Value by that count but it did not seem to work and is not the desired output.
Also, I tried grouping but this did not seem to achieve the desired output.
One thing to mention is that the DB I am working with is an ORACLE SQL DB.
How about this:
select table1.id
, table2.type
, case
when row_number() over (partition by table1.id order by table2.type) = 1
then table1.value
end as "VALUE"
, table2.other
from table1
join table2 on table1.id = table2.id
order by 1, 2;
(This is Oracle SQL syntax. Your SQL Fiddle (thanks!) was set to MySQL, which as far as I know doesn't have analytic functions like row_number().)
A way to get the result.
select t1.ID,
t2.type,
t1.value,
t2.other
from table1 t1 inner join table2 t2
ON t1.ID = t2.ID
inner join (select ID, min(type) mv
from table2
group by id) m
on t2.id = m.id
and t2.type = m.mv
union all
select t1.ID,
t2type,
null,
t2.other
from table1 t1 inner join table2 t2
ON t1.ID = t2.ID
and not exists (
select 1 from (
select ID, min(type) mv
from table2
group by id) m
where t2.id = m.id
and t2.type = m.mv
)
order by id,type
You can use a CASE block to display NULL for which it is NOT equal to MIN value of type
SELECT table1.ID,
table2.TYPE,
CASE
WHEN table2.TYPE =
MIN (table2.TYPE)
OVER (PARTITION BY table1.id ORDER BY table2.TYPE)
THEN
Table1.VALUE
END
VALUE,
table2.other
FROM table1 INNER JOIN table2 ON table1.ID = table2.ID;
I have a select statement that generate set value thereafter I want insert that set of values into another table, MY concern is I'm using select statement in select I'm using one one more select clause((select max(org_id)+1 from org)) where I'm trying to get max value and increment by one but I'm not able get incremented value instead I'm getting same value you can see column name id_limit
select abc,abc1,abc3,abc4,(select max(org_id)+1 from org) as id_limit from xyz
current output
-----------------------------------------------------------------
| abc | abc1 | abc3 | abc4 | id_limit |
----------------------------------------------------------------|
| BUSINESS_UNIT | 0 | 100 | London | 6 |
| BUSINESS_UNIT | 0 | 200 | Sydney | 6 |
| BUSINESS_UNIT | 0 | 300 | Kiev | 6 |
-----------------------------------------------------------------
I'm trying to get expected out output
-----------------------------------------------------------------
| abc | abc1 | abc3 | abc4 | id_limit |
----------------------------------------------------------------|
| BUSINESS_UNIT | 0 | 100 | London | 6 |
| BUSINESS_UNIT | 0 | 200 | Sydney | 7 |
| BUSINESS_UNIT | 0 | 300 | Kiev | 8 |
-----------------------------------------------------------------
Yes, in Oracle 12.
create table foo (
id number generated by default on null as identity
);
https://oracle-base.com/articles/12c/identity-columns-in-oracle-12cr1
In previous versions you use sequence/trigger as explained here:
How to create id with AUTO_INCREMENT on Oracle?
I am having trouble formulating this question, so I would give an example to demonstrate. Consider my table as,
CREATE TABLE ABC
(
PID NUMBER(10,0) NOT NULL,
DISP_COL VARCHAR2(100 BYTE),
VAL_COL VARCHAR2(20 BYTE),
ORD_COL1 NUMBER(5,2),
ORD_COL2 NUMBER(5,2),
CONSTRAINT PK_PID PRIMARY KEY
(
PID
)
);
And the data I have is,
PID | DISP_COL | VAL_COL | ORD_COL1 | ORD_COL2
----------------------------------------------
1 | DISP1 | VAL1 | 1 | 14
2 | DISP2 | VAL26 | 2 | 22
3 | DISP1 | VAL8 | 1 | 17
4 | DISP1 | VAL56 | 1 | 9
5 | DISP2 | VAL9 | 2 | -10
6 | DISP3 | VAL12 | 2 | 20
7 | AISP1 | VAL7 | 2 | -3
Now based on the descending ordering of ORD_COL1, ORD_COL2, I want to get the unique DISP_COL values and then all rows of that DISP_COL value to follow. So my data should like,
PID | DISP_COL | VAL_COL | ORD_COL1 | ORD_COL2
----------------------------------------------
2 | DISP2 | VAL26 | 2 | 22
5 | DISP2 | VAL9 | 2 | -10
6 | DISP3 | VAL12 | 2 | 20
7 | AISP1 | VAL7 | 2 | -3
3 | DISP1 | VAL8 | 1 | 17
1 | DISP1 | VAL1 | 1 | 14
4 | DISP1 | VAL56 | 1 | 9
A simple ORDER BY ORD_COL1 DESC, ORD_COL2 DESC does get me the order I want DISP_COL to occur but then I want the same valued rows to follow that.
I am kind of new to oracle and pl/sql, so all help is appreciated. Thanks in advance.
SELECT * FROM ABC ORDER BY ORD_COL1 DESC, DISP_COL ASC, ORD_COL2 DESC;
http://sqlfiddle.com/#!4/40401/18
You will need to order by the DISP_COL in ASC in order to get the result you want. See the updated fiddle and the code above. This will give you what you want from you question.
The following query worked (http://sqlfiddle.com/#!4/c340b/2/0)
SELECT A1.* FROM (SELECT * FROM ABC) A1 INNER JOIN
(SELECT DISP_COL, MAX(ORD_COL1) COL1, MAX(ORD_COL2) COL2 FROM ABC
GROUP BY DISP_COL
ORDER BY COL1 DESC, COL2 DESC) A2 ON (A1.DISP_COL = A2.DISP_COL)
ORDER BY A2.COL1 DESC, A2.COL2 DESC, A2.DISP_COL, A1.ORD_COL1 DESC,
A1.ORD_COL2 DESC;
I am trying to insert the data from other table using INSERT/SELECT combo. I also need to insert increment with specific calculation. However, I can't figure out why it is not working.
I have the table (temp_business_area) like this:
----------
| bname |
----------
| London |
| Sydney |
| Kiev |
----------
I would like to have this in enum table:
-----------------------------------------------------------------
| identifier | language_id | code | data | company_limit |
----------------------------------------------------------------|
| BUSINESS_UNIT | 0 | 100 | London | 126 |
| BUSINESS_UNIT | 0 | 200 | Sydney | 126 |
| BUSINESS_UNIT | 0 | 300 | Kiev | 126 |
-----------------------------------------------------------------
But what I get is this:
-----------------------------------------------------------------
| identifier | language_id | code | data | company_limit |
----------------------------------------------------------------|
| BUSINESS_UNIT | 0 | 100 | London | 126 |
| BUSINESS_UNIT | 0 | 100 | Sydney | 126 |
| BUSINESS_UNIT | 0 | 100 | Kiev | 126 |
| BUSINESS_UNIT | 0 | 200 | London | 126 |
| BUSINESS_UNIT | 0 | 200 | Sydney | 126 |
| BUSINESS_UNIT | 0 | 200 | Kiev | 126 |
| BUSINESS_UNIT | 0 | 300 | London | 126 |
| BUSINESS_UNIT | 0 | 300 | Sydney | 126 |
| BUSINESS_UNIT | 0 | 300 | Kiev | 126 |
-----------------------------------------------------------------
And here is my loop.
BEGIN
FOR x IN 1 .. 3 LOOP
INSERT INTO enum (identifier, language_id, code, data, company_limit)
SELECT 'BUSINESS_UNIT', 0, x*100, bname, 126 FROM temp_business_area;
END LOOP;
END;
I can't figure out where am I making mistake. Help?
You are doing three inserts for each row in temp_business_area, that's why you wind up with 9 rows.
From your description of what you want to achieve you don't need the loop at all.
Just use a single insert:
INSERT INTO enum (identifier, language_id, code, data, company_limit)
SELECT 'BUSINESS_UNIT',
0,
row_number() over (order by null) * 100,
bname, 126
FROM temp_business_area;
The SELECT statement will return 3 rows, and each row will be inserted into the enum table. The row_number() function will return an incrementing value for each row (1,2,3) which multplied by 100 will yield the code that you want.
Edit
(after David's comments):
The use of the windowing function does add a bit of an overhead to the statement. If the additional control over the numbering is not needed, using ROWNUM instead will be a bit more efficient (although it won't matter for only three rows).
INSERT INTO enum (identifier, language_id, code, data, company_limit)
SELECT 'BUSINESS_UNIT',
0,
rownum * 100,
bname, 126
FROM temp_business_area;
you may to use two variants else:
declare
i integer := 1;
BEGIN
FOR x IN (select distinct bname from temp_business_area) LOOP
INSERT INTO enum (identifier, language_id, code, data, company_limit)
SELECT 'BUSINESS_UNIT', 0, i*100, x.bname, 126 FROM temp_business_area;
i := i + 1;
END LOOP;
END;
variant 2
BEGIN
FOR x IN 1..3 LOOP
INSERT INTO enum (identifier, language_id, code, data, company_limit)
SELECT distinct 'BUSINESS_UNIT', 0, x*100, bname, 126 FROM temp_business_area WHERE rownum = x;
END LOOP;
END;