SQL Query using Joins: ORA-00979 error [duplicate] - oracle

This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 5 years ago.
I'm trying to execute one sql query using joins, but I'm getting the below error:
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
The query is to count the number of normalized_event_ids along with the error message and error_ids using two tables.:
select count(nee.normalised_event_id),
em.error_message,
em.error_message_id
from normalised_event_error nee, error_message em
where nee.charge_start_date >= to_date('01-07-2017','DD-MM-YYYY')
and nee.error_message_id = em.error_message_id
group by em.error_message;

SELECT COUNT (nee.normalised_event_id), em.error_message, em.error_message_id
FROM normalised_event_error nee
INNER JOIN error_message em
ON nee.error_message_id = em.error_message_id
WHERE nee.charge_start_date >= TO_DATE ('01-07-2017', 'DD-MM-YYYY')
GROUP BY em.error_message, em.error_message_id;
The select columns and group by columns must be same for grouping.

Example
You need to add error_message_id column to the group by clause:
SELECT COUNT(nee.normalised_event_id) as messages,
em.error_message,
em.error_message_id
FROM normalised_event_error nee
JOIN error_message em ON em.error_message_id = nee.error_message_id
WHERE nee.charge_start_date >= to_date('01-07-2017','DD-MM-YYYY')
GROUP BY em.error_message, em.error_message_id;

Try this:
SELECT
em.error_message,
em.error_message_id,
count(nee.normalised_event_id)
from normalised_event_error nee,
error_message em
where nee.charge_start_date >= to_date('01-07-2017','DD-MM-YYYY')
and nee.error_message_id = em.error_message_id
group by em.error_message, em.error_message_id;

Related

How to get rows in the correct order when using RowNum in where clause [duplicate]

This question already has answers here:
How do I do top 1 in Oracle? [duplicate]
(9 answers)
Closed 1 year ago.
The below query returns me large set of records. I only want to get 100000 as fast as possible in exactly that specific order. When i use where RowNum < 100000, the order gets messed up for obvious reasons.
How do I change the below query to return in the right orderby and only limit to get 100000 records. Please advise. Thanks
SELECT S.LOGIN_ID as LoginId, S.ProductId as ProductId, P.CREATE_DT as CreateDate,
FROM SITE_USER S
INNER JOIN Production P on P.LOGIN_ID = S.LOGIN_ID
where P.PROCESS_CD = 'REGISTRATION'
and P.CREATE_DT >= '20-JAN-21'
order by P.Request_id asc
Since you're running on v11, you can't use FETCH but you can limit the sub-query.
select * from (
SELECT S.LOGIN_ID as LoginId, S.ProductId as ProductId, P.CREATE_DT as CreateDate,
FROM SITE_USER S
INNER JOIN Production P on P.LOGIN_ID = S.LOGIN_ID
where P.PROCESS_CD = 'REGISTRATION'
and P.CREATE_DT >= '20-JAN-21'
order by P.Request_id asc)
where rownum <= 10000;
UPD. And check please if this condition will work
and P.CREATE_DT >= '20-JAN-21'
if p.create_dt of date type, you need to update it to something similar to this
and P.CREATE_DT >= to_date('20-JAN-21', 'dd-MON-yy')

Teradata to Oracle Query eaxdata Conversion

I need help with converting the below SQL query in Taradata to Oracle exadata. Not sure how to convert the date, I changed the CAST date function To_Date but get a few errors.
SELECT DISTINCT
pat.pat_id,
pat.pat_mrn_id AS patientmrn,
pat.pat_name AS patientname,
adt_pat_class_c,
pat.death_date AS deathdate,
cast(patenc.hosp_admsn_time AS DATE format 'mm/dd/yyyy') AS
admitdate,
cast(patenc.hosp_disch_time AS DATE format 'mm/dd/yyyy') AS
dischargedate,
extract(year FROM acct.adm_date_time)-extract(year FROM
pat.birth_date) - CASE WHEN acct.adm_date_time (format
'MMDD')CHAR(4))
<pat.birth_date (format 'MMDD') (CHAR(4)) THEN 1 ELSE 0
END AS patage,
adt_billing_type_c,
adt_patient_stat_c,
hosp_admsn_type_c,
acct_basecls_ha_c,
ordproc.order_proc_id AS order_id,
ordproc.ordering_date
FROM patient pat
inner join pat_enc_hsp patenc ON pat.pat_id = patenc.pat_id
inner join hsp_account acct ON acct.prim_enc_csn_id = patenc.pat_enc_csn_id
inner join order_proc ordproc ON acct.prim_enc_csn_id ON
ordproc.pat_enc_csn_id
inner join clarity_ser ser ON ser.prov_id = ordproc.authrzing_prov_id
inner join identity_ser_id idser ON ser.prov_id=idser.prov_id
inner join clarity_loc loc ON loc.loc_id = acct.loc_id
inner join zc_loc_rpt_grp_7 grp7 ON loc.rpt_grp_seven = grp7.rpt_grp_seven
WHERE grp7.name = 'AB'
AND cast(patenc.hosp_disch_time AS DATE format 'mm/dd/yyyy') >= '01/01/2019'
AND admit_conf_stat_c IN (1,4)
AND description LIKE '%CULTURE%'
AND ordproc.lab_status_c = 3
AND adt_pat_class_c IN ('1204','12113')
AND result_time > patenc.hosp_disch_time;
I changed the top part of the query(one with dates) -
select distinct
pat.pat_id,
pat.PAT_MRN_ID as PatientMRN,
pat.PAT_NAME as PatientName,
ADT_PAT_CLASS_C,
pat.DEATH_DATE as DeathDate,
TO_DATE(patenc.HOSP_ADMSN_TIME, 'mm/dd/yyyy') as AdmitDate,
TO_DATE(patenc.HOSP_DISCH_TIME, 'mm/dd/yyyy') as DischargeDate----
I get the below errors -
ORA-12801: error signaled in parallel query server P02G, instance nzcladb01xm.nndc.kp.org:CDB001N41 (1)
ORA-01843: not a valid month
12801. 00000 - "error signaled in parallel query server %s"
*Cause: A parallel query server reached an exception condition.
*Action: Check the following error message for the cause, and consult
your error manual for the appropriate action.
*Comment: This error can be turned off with event 10397, in which
case the server's actual error is signaled instead.

using subquery factoring result in where clause

Why can't I use a subquery factoring clause result in the where clause of as depicted in the following sql:
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = rpt.id
and
tg.gene not in('TMB','MS')
The subquery is named rptand used in the select statement's where clause. When executed encountering the following error: ORA-00904: "RPT"."ID": invalid identifier
UPDATE:
In fact nested query for the same thing is also giving me the same issue. The nested subquery is only returning a single column value from a single row:
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = (select id from reports where caseid = :case_id and
rownum=1 order by created desc)
and
tg.gene not in('TMB','MS')
You missed to add the table rpt in your query, thus that error.
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
join
rpt on rt.reportid = rpt.id
where
tg.gene not in('TMB','MS')

Hadoop - error message when declaring variable within query

I have tried the following query within HUE's Beeswax Query Editor:
SET MAXDATE=(SELECT MAX(DATA_DAY) FROM DB1.DESTINATION_TABLE);
SELECT COUNT(*) FROM DB2.SOURCE_TABLE
WHERE YEAR(DATA_DAY) >= '2015'
AND DATA_DAY > ${HIVECONF:MAXDATE};
This query will not run and produces the following error message:
FAILED: ParseException line 1:4 missing KW_ROLE at 'MAXDATE' near 'MAXDATE' line 1:11 missing EOF at '=' near 'MAXDATE'
Any advice on what the problem is? I don't understand what the KW_ROLE message means.
I come from a SQL Server background and would just run the following within SQL Server, but am trying to find a functional Hadoop/Hive equivalent.
SELECT COUNT(*) FROM DB2.SOURCE_TABLE
WHERE YEAR(DATA_DAY) >= '2015'
AND DATA_DAY > (SELECT MAX(DATA_DAY) FROM DB1.DESTINATION_TABLE)
Query which you have tried contains syntax issue. HiveConf should surrounded by single quotes.
SET MAXDATE=(SELECT MAX(DATA_DAY) FROM DB1.DESTINATION_TABLE);
SELECT COUNT(*) FROM DB2.SOURCE_TABLE
WHERE YEAR(DATA_DAY) >= '2015'
AND DATA_DAY > '${HIVECONF:MAXDATE}';
As far as I know, hive support the following syntax too.
SELECT COUNT(*) FROM DB2.SOURCE_TABLE a
JOIN
(SELECT MAX(DATA_DAY) AS max_date FROM DB1.DESTINATION_TABLE) b
WHERE YEAR(a.DATA_DAY) >= '2015'
AND a.DATA_DAY > b.max_date;
But it's not a good implementation if there are bunches of data on DB1.DESTINATION_TABLE.
In such case each query would task lots of sub-querys in SELECT MAX(DATA_DAY) FROM DB1.DESTINATION_TABLE.
If possible, you could store your SELECT MAX(DATA_DAY) FROM DB1.DESTINATION_TABLE result in another table, maybe Max_table.
Then the sql would be like this:
SELECT COUNT(*) FROM DB2.SOURCE_TABLE
JOIN Max_table
WHERE YEAR(DB2.SOURCE_TABLE.DATA_DAY) >= '2015' and
DB2.SOURCE_TABLE.DATA_DAY > (Max_table.DATA_DAY)

Need to Include a Name with Group By oracle sql

I have some Code here,
`SELECT a.Officer_ID, Count(Crime_ID)
FROM Officers a inner Join Crime_Officers b on a.Officer_ID = b.Officer_ID
Group by a.Officer_ID
Having (Count(Crime_ID) > (select avg(distinct(Count(Crime_ID)))
From Crime_Officers
Group by officer_Id));`
it outputs this,
`OFFICER_ID COUNT(CRIME_ID)
---------- ---------------
111115 9 `
Its cool and all, but my assignment only needs the officers last name which is in table A. I tried it with the Officer_ID and Count just to make sure the code was working. When I try it like this,
`SELECT Last
FROM Officers a inner Join Crime_Officers b on a.Officer_ID = b.Officer_ID
Group by a.Officer_ID
Having (Count(Crime_ID) > (select avg(distinct(Count(Crime_ID)))
From Crime_Officers
Group by officer_Id));`
I get this error,
`Error starting at line 1 in command:
SELECT Last
FROM Officers a inner Join Crime_Officers b on a.Officer_ID = b.Officer_ID
Group by a.Officer_ID
Having (Count(Crime_ID) > (select avg(distinct(Count(Crime_ID)))
From Crime_Officers
Group by officer_Id))
Error at Command Line:1 Column:8
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:`
Can anyone help or explain what I'm missing/Doing wrong?
You can only select a column if a) it's in the GROUP BY, b) it's derived from a GROUP BY column or c) it's the result of an aggregate function like MAX/MIN.
In your case, each Officer_ID should only have one Last value.
Simply change your GROUP BY to read:
GROUP BY a.Officer_ID, a.Last

Resources