Need to Include a Name with Group By oracle sql - oracle

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

Related

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')

Missing Keyword Error in Oracle - Wrong Syntax WHERE Expression

I'm new to Oracle SQL, I'm being asked to do some scenarios to learn the different expressions and so on.
I'm currently working on this statement but I keep having trouble with syntax and trying to get my expressions in the correct place.
If you don't mind taking a look at what I'm doing wrong and helping me learn the correct syntax I'd appreciate it a lot.
I have to find everything in the Sale, SaleDetail, OrderStatus, Warehouse, User and StockDetail tables.
The fields I need to find are saleno, serialstart, serialend, the product description (label field), sale status (saleid (I think)), WarehouseName (WH.NAME)
Here below is the code I've written so far.
SELECT
S.SALENO,
SD.SERIALSTART,
SD.SERIALEND,
SDT.LABEL,
USR.USERNAME,
WH.NAME
FROM
ITR_SALE,
ITR_SALEDETAIL,
ITR_ORDER,
ITR_WAREHOUSE,
ITR_USER,
ITR_STOCKDETAIL
JOIN ITR_SALE S
JOIN ITR_SALEDETAIL SD ON S.ID = SD.SALENO
JOIN ITR_WAREHOUSE WH ON SD.ID = WH.NAME
JOIN ITR_ORDER ODR ON WH.ID = ODR.STATUSID
JOIN ITR_USER USR ON ODR.ID = USR.USERNAME
JOIN ITR_STOCKDETAIL ON USR.ID = SDT.LABEL
WHERE S.LASTSTATUSCHANGETIME
BETWEEN ('2016-01-01 00:00:00' AND '2016-12-31 23:59:59')
AND STATUSID = ('COMPLETED');
Below follows the error message
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Error at Line: 21 Column: 1
EDIT:
Finished code below, changed a few expressions and conditions.
SELECT
S.SALENO,
SD.SERIALSTART,
SD.SERIALEND,
SDA.LABEL,
USR.USERNAME,
WH.NAME
FROM
ITR_SALE S
INNER JOIN
ITR_SALEDETAIL SD ON S.ID = SD.SALEID
INNER JOIN
ITR_ORDERSTATUS ODS ON SD.ID = ODS.ID
INNER JOIN
ITR_WAREHOUSE WH ON ODS.ID = WH.NAME
INNER JOIN
ITR_USER USR ON WH.ID = USR.USERNAME
INNER JOIN
ITR_STOCKDETAIL SDA ON USR.ID = SDA.LABEL
WHERE 'DATE' BETWEEN '2016-01-01' AND '2016-12-31'
AND S.STATUSID = '4';`
Use proper join syntax. Edit. Need to remove parenthesis from last line or user IN clause.
SELECT
S.SALENO,
SD.SERIALSTART,
SD.SERIALEND,
SDT.LABEL,
USR.USERNAME,
WH.NAME
FROM
ITR_SALE S INNER JOIN ITR_SALEDETAIL SD ON S.ID = SD.SALENO
INNER JOIN ITR_SALEDETAIL SD ON S.ID = SD.SALENO
INNER JOIN ITR_WAREHOUSE WH ON SD.ID = WH.NAME
INNER JOIN ITR_ORDER ODR ON WH.ID = ODR.STATUSID
INNER JOIN ITR_USER USR ON ODR.ID = USR.USERNAME
INNER JOIN ITR_STOCKDETAIL STD ON USR.ID = SDT.LABEL
WHERE S.LASTSTATUSCHANGETIME
BETWEEN '2016-01-01 00:00:00' AND '2016-12-31 23:59:59'
AND STATUSID = 'COMPLETED';

Region wise percentage

i need to calculate percentage grosssales with regionwise breakup on a particular segment ie Domestic Corp .
My Query is
select REGION_NAME,
round(ratio_to_report(nvl(sum(gross_sales_amt_t),0)) over ()*100) as Gr_Sales_domcorp
from fact_mfdex_segment A
JOIN dim_location B
ON a.branch_code = b.branch_code
WHERE to_date(DATE_SK,'YYYYMMDD') between '01-feb-2016' and '01-mar-2016'
AND week_flag='Y'
AND a.segments = 'Domestic Corp.'
group by REGION_NAME;
I need to repeat the same query for HNI,RETAIL ETC. instead of Domestic Corp. rather than writing different query can i use my first query to add other segments. I have tried case statement but not working. Any help appreciated
Just include all the records for required segments by putting them inside "IN" clause and then group by segment also and here you go for expected results.
select REGION_NAME,
round(ratio_to_report(nvl(sum(gross_sales_amt_t),0)) over ()*100) as Gr_Sales_domcorp
from fact_mfdex_segment A
JOIN dim_location B
ON a.branch_code = b.branch_code
WHERE to_date(DATE_SK,'YYYYMMDD') between '01-feb-2016' and '01-mar-2016'
AND week_flag='Y'
AND a.segments IN ( 'Domestic Corp.','HNI','RETAIL')
group by REGION_NAME, a.segments;
Add a.segments to the selected columns, remove the "AND a.segments = ..." line, partition ratio_to_report by a.segments, and group by a.segments and REGION_NAME. You may want to change the output column name from Gr_Sales_domcorp to Gr_Sales. Something like this:
select REGION_NAME, a.segments,
round(ratio_to_report(nvl(sum(gross_sales_amt_t),0))
over (partition by a.segments)*100) as Gr_Sales
from fact_mfdex_segment A
JOIN dim_location B
ON a.branch_code = b.branch_code
WHERE to_date(DATE_SK,'YYYYMMDD') between '01-feb-2016' and '01-mar-2016'
AND week_flag='Y'
group by a.segments, REGION_NAME
order by a.segments, REGION_NAME

Left Outer Join Error

I get this error for the below query when I am trying to make a left outer join
ERROR at line 7:
ORA-00936: missing expression
select s.FINAL_BSAL,s.EMP_No,p.ERN_DDCT_CATNO,p.AMOUNT,n.NO_PAY_AMOUNT,
p.Pay_month,a.ARREARS_AMOUNT from salary_details s,pay_details p,Arrears a,No_Pay n
where s.emp_no=p.emp_no
and
s.SAL_NO IN (SELECT MAX(SAL_NO) FROM SALARY_DETAILS group by EMP_NO)
AND
to_char(P.PAY_MONTH,'MM-YYYY') =to_char(n.NO_PAY_MONTH,'MM-YYYY') (+)
AND
to_char(P.PAY_MONTH,'MM-YYYY')=to_char(a.ARREARS_MONTH,'MM-YYYY') ;
Please help.
The issue lies with the placement of (+):
Instead of:
to_char(P.PAY_MONTH,'MM-YYYY') =to_char(n.NO_PAY_MONTH,'MM-YYYY') (+)
you should do:
to_char(P.PAY_MONTH,'MM-YYYY') =to_char(n.NO_PAY_MONTH (+),'MM-YYYY')
However, if I were you, I'd go with #Walter_Ritzel's approach and use ANSI JOIN syntax instead. That and properly format the SQL so that it's readable...
Try this:
select s.FINAL_BSAL
,s.EMP_No
,p.ERN_DDCT_CATNO
,p.AMOUNT
,n.NO_PAY_AMOUNT
,p.Pay_month
,a.ARREARS_AMOUNT
from salary_details s inner join pay_details p on s.emp_no = p.emp_no
inner join Arrears a on to_char(P.PAY_MONTH,'MM-YYYY')= to_char(a.ARREARS_MONTH,'MM-YYYY')
left outer join No_Pay n on to_char(P.PAY_MONTH,'MM-YYYY') = to_char(n.NO_PAY_MONTH,'MM-YYYY')
where s.SAL_NO IN (SELECT MAX(SAL_NO) FROM SALARY_DETAILS group by EMP_NO);

Hibernate HQL GroupBy in Oracle

I created this query using HQL with Hibernate and Oracle
select c from Cat c
left join c.kittens k
where (c.location= 1 OR c.location = 2)
and (i.activo = 1)
group
by c.id,
c.name,
c.fulldescription,
c.kittens
order by count(e) desc
The problem comes with the fact that in HQL you need to specify all fields in Cat in order to perform a Group By, but fulldescription is a CLOB, and you cannot group by by a CLOB (I get a "Not a Group By Expression" error. I've seen a few solutions around for a pure SQL sentence but none for HQL.
A serious issue GROUP BY of HQL because if you specify your object in GROUP BY and in your SELECT field list behaviours are differents. In GROUP BY has considered only id field but in SELECT field list all fields are considered.
So you can use a subquery with GROUP BY to return only id from your object, so that result becomes an input for the main query, like the follow I write for you.
Pay attention there are some alias table (i and e) not defined, so this query doesn't work, but you know as fixed.
Try this:
select c2 from Cat c2
where c2.id in (
select c.id from Cat c
left join c.kittens k
where (c.location= 1 OR c.location = 2)
and (i.activo = 1) <-- who is i alias??
group by c.id)
order by count(e) desc <-- who is e alias???

Resources