ORA-00918: column ambiguously defined with [group by] - oracle

in SQL I have these table
create table employee(
emp_ssn number(10),
first_name varchar2(10) not null ,
second_name varchar2(10),
last_name varchar2(10) not null ,
address varchar2(20) ,
birthdate date not null ,
super_ssn number(10),
job_no number (2),
constraint employee_pk primary key (emp_ssn));
create table job (
job_no number (2),
job_name varchar2(11) ,
constraint job_pk primary key (job_no ));
and I write a query that display how many employee in each
job using group by
SELECT job_no, job_name,COUNT(emp_ssn)
FROM job j JOIN employee e
ON (j.job_no = e.job_no)
GROUP BY j.job_no, j.job_name;
and the output is
SELECT job_no, job_name,COUNT(emp_ssn)
FROM job j JOIN employee e
ON (j.job_no = e.job_no)
GROUP BY j.job_no, j.job_name;
SELECT job_no, job_name,COUNT(emp_ssn)
ERROR at line 1: ORA-00918: column ambiguously defined
can you help me please !

SELECT j.job_no --<-- you are missing alias here
, j.job_name --<-- here
,COUNT(e.emp_ssn) --<-- and here
FROM job j JOIN employee e
ON (j.job_no = e.job_no)
GROUP BY j.job_no, j.job_name;

Related

How to multi-update the table in Oracle?

I have these tables called transaction_line_fact and channel_dim:
SQL> desc transaction_line_fact
Name Null? Type
----------------------------------------------------------------- -------- --------------------------------------------
TRANSACTION_ID NUMBER(20)
TRANSACTION_LINE_ID NUMBER(20)
TRANID VARCHAR2(30)
TRANSACTION_TYPE VARCHAR2(50)
TRANDATE DATE
KPI_CHANNEL_SKEY NUMBER(20)
KPI_LOCATION_SKEY NUMBER(20)
KPI_DEPARTMENT_SKEY NUMBER(20)
KPI_ITEM_SKEY NUMBER(20)
AMOUNT NUMBER(8,2)
COST NUMBER(8,2)
UNITS NUMBER(5)
KPI_DW_SKEY NOT NULL NUMBER(20)
SQL> desc channel_dim
Name Null? Type
----------------------------------------------------------------- -------- --------------------------------------------
DATE_CREATED DATE
IS_RECORD_INACTIVE CHAR(1)
LAST_MODIFIED_DATE DATE
LIST_ID NUMBER(20)
LIST_ITEM_NAME VARCHAR2(20)
KPI_DW_SKEY NOT NULL NUMBER(20)
KPI_DW_INSERT_DATE DATE
KPI_DW_UPDATE_DATE DATE
Currently the in transaction_line_fact table KPI_CHANNEL_SKEY contains null values but I want to populate it with the help of KPI_DW_SKEY column in channel_dim.
Also I've tried doing this way:
SQL> update transaction_line_fact set kpi_channel_skey = (select c.kpi_dw_skey from channel_dim c join transaction_line_fact t on c.kpi_dw_skey=t.kpi_dw_skey);
update transaction_line_fact set kpi_channel_skey = (select c.kpi_dw_skey from channel_dim c join transaction_line_fact t on c.kpi_dw_skey=t.kpi_dw_skey)
*
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row
SQL> update transaction_line_fact set kpi_channel_skey = (select kpi_dw_skey from channel_dim) where kpi_channel_skey is null;
update transaction_line_fact set kpi_channel_skey = (select kpi_dw_skey from channel_dim) where kpi_channel_skey is null
*
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row
SQL> update transaction_line_fact set kpi_channel_skey in (select kpi_dw_skey from channel_dim) where kpi_channel_skey is null;
update transaction_line_fact set kpi_channel_skey in (select kpi_dw_skey from channel_dim) where kpi_channel_skey is null
*
ERROR at line 1:
ORA-00927: missing equal sign
But again it shows error like ORA-01427: single-row subquery returns more than one row
Any idea on How to populate it?
Thank You so much!
Correct syntax would be
UPDATE transaction_line_fact f
SET f.kpi_channel_skey =
(SELECT c.kpi_dw_skey
FROM channel_dim c
WHERE c.kpi_dw_skey = f.kpi_dw_skey);
In other words, you have to join channel_dim with transaction_line_fact, but not specifying that table once again in a subquery, but using a where clause to join appropriate columns.
Although this is now "OK" as far as the principle is concerned, I can't tell whether it will (or will not) raise an error; too_many_rows could happen again, but - in that case - you have to decide what to do if for the same kpi_dw_skey channel_dim contains more than a single row - will you take only one (which one?), or ... Really, can't tell.

I cannot display the values of the 2 table Ceremony and Referee

create table STADIUM
( stad_location varchar2(20) primary key
, stad_name varchar2(10)
, stad_capacity number(5)
, match_ID char(8)
, stall_ID char(4)
, foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
, foreign key (stall_ID) references STALL (stall_ID) ON DELETE SET NULL);
create table TRANSPORTATION
(registra_no char(6) primary key
, transp_type varchar2(10)
, capacity number(2)
);
create table CEREMONY
(cerem_type varchar2(10) primary key
, cerem_name varchar2(15)
, FIFA_theme_song varchar2(20)
, p_ID char(8)
, stad_location varchar2(20)
, foreign key (p_ID) references PERFORMER(p_ID) ON DELETE SET NULL
, foreign key (stad_location) references STADIUM(stad_location)ON DELETE SET NULL
);
create table REFEREE( ref_ID char(8) primary key
, ref_name varchar2(20)
, yo_exp number(2)
, match_ID char(8)
, registra_no char(6)
, foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
, foreign Key (registra_no) references TRANSPORTATION(registra_no) ON DELETE SET NULL);
insert into CEREMONY values('Opening', 'Speech', 'Colors', 'PP561475', 'Al-Waab Street');
insert into REFEREE values('RF503624','Mike Dean', 25, 'MM129456', 'QLM729'); select * from CEREMONY;
select * from REFEREE;
Bunch of tables are missing, referenced by foreign keys so I'm creating them first:
SQL> -- Missing tables, referenced by foreign keys
SQL> create table match (match_id char(8) primary key);
Table created.
SQL> create table stall (stall_id char(4) primary key);
Table created.
SQL> create table performer (p_id char(8) primary key);
Table created.
SQL>
Your tables:
SQL> create table STADIUM
2 ( stad_location varchar2(20) primary key
3 , stad_name varchar2(10)
4 , stad_capacity number(5)
5 , match_ID char(8)
6 , stall_ID char(4)
7 , foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
8 , foreign key (stall_ID) references STALL (stall_ID) ON DELETE SET NULL);
Table created.
SQL> create table TRANSPORTATION
2 (registra_no char(6) primary key
3 , transp_type varchar2(10)
4 , capacity number(2)
5 );
Table created.
SQL> create table CEREMONY
2 (cerem_type varchar2(10) primary key
3 , cerem_name varchar2(15)
4 , FIFA_theme_song varchar2(20)
5 , p_ID char(8)
6 , stad_location varchar2(20)
7 , foreign key (p_ID) references PERFORMER(p_ID) ON DELETE SET NULL
8 , foreign key (stad_location) references STADIUM(stad_location)ON DELETE SET NULL
9 );
Table created.
SQL> create table REFEREE( ref_ID char(8) primary key
2 , ref_name varchar2(20)
3 , yo_exp number(2)
4 , match_ID char(8)
5 , registra_no char(6)
6 , foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
7 , foreign Key (registra_no) references TRANSPORTATION(registra_no) ON DELETE SET NULL);
Table created.
SQL>
Inserts into tables referenced by ceremony and referee:
SQL> insert into performer values ('PP561475');
1 row created.
SQL> insert into stadium (stad_location) values ('Al-Waab Street');
1 row created.
SQL> insert into match values ('MM129456');
1 row created.
SQL> insert into transportation (registra_no) values ('QLM729');
1 row created.
SQL>
Your inserts & result of your select statements:
SQL> insert into CEREMONY values('Opening', 'Speech', 'Colors', 'PP561475', 'Al-Waab Street');
1 row created.
SQL> insert into REFEREE values('RF503624','Mike Dean', 25, 'MM129456', 'QLM729');
1 row created.
SQL> select * from CEREMONY;
CEREM_TYPE CEREM_NAME FIFA_THEME_SONG P_ID STAD_LOCATION
---------- --------------- -------------------- -------- --------------------
Opening Speech Colors PP561475 Al-Waab Street
SQL> select * from REFEREE;
REF_ID REF_NAME YO_EXP MATCH_ID REGIST
-------- -------------------- ---------- -------- ------
RF503624 Mike Dean 25 MM129456 QLM729
SQL>
Therefore, if you do everything right, everything is right.

Oracle : It give a error in a dynamic view

I have two table one is employee and one is department. I am creating the dynamic view that will rank all departments by salary. The view should pull information from Department and Employee, sum the salary by department, and rank the department by salary.
CREATE TABLE DEPARTMENT
(DEPARTMENT_ID NUMBER PRIMARY KEY,
DEPARTMENT_NAME VARCHAR(30) NOT NULL
);
CREATE TABLE JOBS
(JOB_ID NUMBER PRIMARY KEY,
JOB_TITLE VARCHAR(35) NOT NULL,
MIN_SALARY DECIMAL NOT NULL,
MAX_SALARY DECIMAL NOT NULL
);
CREATE TABLE EMPLOYEES
(EMPLOYEE_ID NUMBER PRIMARY KEY,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(25) NOT NULL,
EMAIL VARCHAR(25) NOT NULL,
PHONE_NUMBER VARCHAR(20) NOT NULL,
HIRE_DATE DATE NOT NULL,
JOB_ID NUMBER NOT NULL,
SALARY DECIMAL NOT NULL,
DEPARTMENT_ID NUMBER NOT NULL,
CONSTRAINT emp_job_fk FOREIGN KEY(JOB_ID) REFERENCES JOBS(JOB_ID),
CONSTRAINT emp_department_fk FOREIGN KEY(DEPARTMENT_ID) REFERENCES DEPARTMENT(DEPARTMENT_ID)
);
INSERT INTO DEPARTMENT (DEPARTMENT_ID,DEPARTMENT_NAME)
VALUES(1,'IT');
INSERT INTO DEPARTMENT (DEPARTMENT_ID,DEPARTMENT_NAME)
VALUES(2,'Sales');
INSERT INTO JOBS (JOB_ID,JOB_TITLE,MIN_SALARY,MAX_SALARY)
VALUES (1,'IT Administrator',250000.00,50000.00);
INSERT INTO JOBS (JOB_ID,JOB_TITLE,MIN_SALARY,MAX_SALARY)
VALUES (2,'Salesman',200000.00,40000.00);
Here is I create so far but it give me a error
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:
Error at Line: 4 Column: 9
Here is my code
select department_id,department_name,total_salary
from(
select department_id,department_name, SALARY, count(*) as total_salary from(
select dep.department_id , dep.department_name ,emp.SALARY,
DENSE_RANK() OVER (PARTITION BY department_name ORDER BY salary)
from departments dep
inner join employees emp on dep.DEPARTMENT_ID = emp.DEPARTMENT_ID
)
GROUP BY SALARY)
Your query needs to join EMPLOYEES (to get the salaries) to DEPARTMENT (to get the DEPARTMENT_NAME). Calculate the total salary for each department by summing the employee salaries, not counting them. The GROUP BY needs to include the non-aggregated columns.
Then you need to rank the departments by the total salary per department. This query ranks the departments with highest salary = 1. It uses a left join to cater for departments with no employees.
select department_id
, department_name
, total_salary
, rank() over (order by total_salary desc) as dept_rank
from (
select d.department_id
, d.department_name
, sum(e.SALARY) as total_salary
from department d
left join employees e
on e.department_id = d.department_id
group by d.department_id
, d.department_name
)
/

ORA-01722 invalid number though types should match

table1 dwh.fct_nc_crm_dims#etl4
Name Null Type
--------------------------- -------- -------------
BAN_KEY NOT NULL NUMBER(9)
CLIENT_NAME VARCHAR2(300)
CLIENT_INN VARCHAR2(40)
EFFECTIVE_DATE NOT NULL DATE
EXPIRATION_DATE DATE
table2 etl.stg_acrm_ban_attr#etl2
Name Null Type
---------------- ---- -------------
SEGMENT_CRM VARCHAR2(150)
BAN_KEY VARCHAR2(32)
table3 evkuzmin_b2b_churn_ban_segment
Name Null Type
----------- ---- -------------
BAN_KEY NUMBER(9)
CLIENT_NAME VARCHAR2(300)
CLIENT_INN VARCHAR2(40)
SEGMENT_CRM VARCHAR2(150)
My query. Here I join first 2 tables on ban_key and insert the result into the third table. The types match, but I still get the error. Why?
INSERT INTO evkuzmin_b2b_churn_ban_segment
SELECT a.ban_key, a.client_name, a.client_inn, b.segment_crm FROM(
SELECT ban_key, client_name, client_inn
FROM(
SELECT ban_key,client_name, client_inn,
ROW_NUMBER() OVER (PARTITION BY ban_key, client_inn ORDER BY effective_date DESC) AS rn
FROM dwh.fct_nc_crm_dims#etl4
WHERE expiration_date >= TO_DATE('01.04.2016','DD.MM.YYYY') OR expiration_date IS NULL --1ST DAY OF REPORTING PERIOD
)
WHERE rn = 1
) a, etl.stg_acrm_ban_attr#etl2 b
WHERE a.ban_key = b.ban_key;
Your ban_key in evkuzmin_b2b_churn_ban_segment(table 3) is a Number as compare to the other two tables(varchar).
your a.ban_key is number and b.ban_key is varchar

handling exception for insert into select from

SCHEMA 1
I have table transaction table
create table TXN_HEADER
(
txn_id NUMBER(10) not null,
txn_Date date
product_id NUMBER(10),
company_id NUMBER(10),
dealer_id NUMBER(10),
tran_amt number(10,2)
)
The above table having foreign key references to product.product_id and company.company_id.
This table having 5m rows
SCHEMA 2
create table TXN_HEADER_REPORTS
(
txn_id NUMBER(10) not null,
txn_Date date
product_id NUMBER(10),
company_id NUMBER(10),
dealer_id NUMBER(10),
tran_amt number(10,2)
)
here also we have the same constraints , having foreign key references to product.product_id and company.company_id.
in schema 2 we are trying to insert all the rows from schemea 1 to schema 2 in one shot, like this
begin
insert into TXN_HEADER_REPORTS (
txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt)
select
txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt
from schema1.TXN_HEADER;
commit;
exception
when others then
< ... procedure to log the errors >
end;
now we are trying to execute the above procedure , and it failed due to foreign key constraint of one rows. But entire my transaction rollback. Actually i dont want to use cursor to process the rows one by one , at it takes long time. So i used to "insert into .. SElect from " but due to constraints of 1 row all my transaction not moved to schema2.txn_Extract_hdr.
Is there any way to trap only that failed and to process the other rows without terminating
Please advice ..
You can create an error log table, and then use a single insert:
exec dbms_errlog.create_error_log(dml_table_name => 'TXN_HEADER_REPORTS');
insert into TXN_HEADER_REPORTS ( txn_id, txn_Date ,product_id,company_id ,
dealer_id , tran_amt)
select txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt
from schema1.TXN_HEADER
log errors into ERR$_TXN_HEADER_REPORTS reject limit unlimited;
Any rows that can't inserted will be recorded in the ERR table. Note that this is plain SQL, it doesn't need to be in a PL/SQL block.
Read more in the documentation.
I don't understand your constraint.
Does your insert fail because the product_id and company_id don't exists in schema2?
In that case, it may be better to insert the missing company and product records before you insert records into TXN_HEADER_REPORTS of schema2.
insert into company com_sch2
(col1, col2, col2,...)
select col1, col2, col3, ...
from schema1.company com_sch1
where not exists (select 'x'
from company com2
where com2.company_id = com_sch1.company_id);
And the seem for the product table.

Resources