Invalid identifier error, Oracle - sql

I've been trying to write this query for 1 hour, but the SQL Developer always throws an error.
SELECT d.driver_name, COUNT(*) AS cnt
FROM Drivers d
JOIN Fastest_laps fl ON d.ID_driver = fl.ID_driver
GROUP BY d.driver_name
HAVING cnt = MAX(cnt);
00000 - "%s: invalid identifier"
Error at last line, column 20.
So I've figured out another solution, but another error is thrown:
SELECT d.driver_name, COUNT(*) as cnt
FROM Drivers d
JOIN Fastest_laps fl ON d.ID_driver = fl.ID_driver
GROUP BY d.driver_name
HAVING COUNT(*) = MAX(COUNT(*));
00000 - "group function is nested too deeply"
Error at last line, column 25.
EDIT: thanks gyus, you are awsome, almost all the replies are working, but I have to choose one...

Use a window function:
SELECT driver_name, cnt
FROM (SELECT d.driver_name, COUNT(*) AS cnt,
MAX(COUNT(*)) OVER () as MAXcnt
FROM Drivers d JOIN
Fastest_laps fl
ON d.ID_driver = fl.ID_driver
GROUP BY d.driver_name
) d
WHERE cnt = MAXcnt;
You can also express this using RANK() or DENSE_RANK():
SELECT driver_name, cnt
FROM (SELECT d.driver_name, COUNT(*) AS cnt,
RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
FROM Drivers d JOIN
Fastest_laps fl
ON d.ID_driver = fl.ID_driver
GROUP BY d.driver_name
) d
WHERE seqnum = 1;
The advantage to this approach is that you can use ROW_NUMBER() instead and get exactly one row, even if multiple drivers have the same maximum.

I'm not sure if Oracle supports this, but please give it a try:
SELECT d.driver_name, COUNT(*) as cnt
FROM Drivers d
JOIN Fastest_laps fl ON d.ID_driver = fl.ID_driver
GROUP BY d.driver_name
ORDER BY cnt DESC
FETCH FIRST 1 ROW WITH TIES
Or use a common table expression:
with cte as
(
SELECT d.driver_name as driver_name, COUNT(*) AS cnt
FROM Drivers d
JOIN Fastest_laps fl ON d.ID_driver = fl.ID_driver
GROUP BY d.driver_name
)
select driver_name, cnt
from cte
where cnt = (select max(cnt) from cte)

Try this. I ordered by cnt in descending order. And then selected the top row from it. You can edit the query as rownum <=2 to get the top 2 rows and so on.
with tbl1 as
(SELECT d.driver_name as driver_name, COUNT(*) AS cnt
FROM Drivers d
JOIN Fastest_laps fl ON d.ID_driver = fl.ID_driver
GROUP BY d.driver_name
order by cnt desc
)
select driver_name,cnt from tbl1
where cnt = (select cnt from tbl1 rownum=1)

You have to wrap your query into a inline view to be able to query cnt:
select *
from (
SELECT d.driver_name, COUNT(*) AS cnt
FROM Drivers d
JOIN Fastest_laps fl ON d.ID_driver = fl.ID_driver
GROUP BY d.driver_name
) x
group
by driver_name, cnt
having cnt = MAX(cnt);

Related

Select both row number and count from oracle

I'm trying to select records with pagination, and I need the total number of records so that I can display the number of records and pages on the UI.
The query I'm using is as below but it always returning the totalcount as 1.
WITH cteEmp AS
(SELECT e.empid, e.empname, d.deptid, d.deptname
FROM hr.Emp e
INNER JOIN hr.dept d ON e.deptid = d.deptid)
Select * from (SELECT row_number() over (order by hr.empid desc) rn, Count(*) totalcount,
C.empName FROM CTEPO C
LEFT JOIN hr.emphistory ON C.empid=hr.empid
GROUP BY c.empid,hr.empid) where rn>0 and rn<= 100
You can try this maybe it'll work for you:
(SELECT e.empid, e.empname, d.deptid, d.deptname
FROM hr.Emp e
INNER JOIN hr.dept d ON e.deptid = d.deptid)
Select * from (SELECT row_number() over (order by hr.empid desc) rn,
count(*) OVER (ORDER BY hr.empid desc ) AS totalcount
C.empName FROM CTEPO C
LEFT JOIN hr.emphistory ON C.empid=hr.empid
GROUP BY c.empid,hr.empid) where rn>0 and rn<= 100

Getting ORA-00907: missing right parenthesis when no extra parenthesis on left

Here is my SQL query:
select hck.hacker_id, hck.name, cnt
from (
Hacker as hck
inner join (
Select hacker_id, count(challenge_id) as cnt
from Challenges
group by hacker_id
) chl_count on hck.hacker_id = chl_count.hacker_id
) having cnt = max(cnt) or
cnt in (select cnt
from chl_count
group by cnt
having count(hacker_id) = 1)
order by cnt desc, hck.hacker_id asc;
Here Hackers has schema:
Hackers(name, hacker_id)
And Challenges has schema
Challenges(hacker_id, ,challenge_id)
I don't see any missing parenthesis in the query. So, what is wrong? Also, other syntaxes such as commas are correct as well.
It seems you are new in Oracle SQL.
You can't do this: "..FROM (Hacker as hck inner join) .." but you can do it like this:
WITH chl_count
AS ( SELECT hacker_id, COUNT (challenge_id) AS cnt
FROM Challenges
GROUP BY hacker_id)
SELECT hck.hacker_id, hck.name, cnt
FROM Hacker hck INNER JOIN chl_count ON hck.hacker_id = chl_count.hacker_id
HAVING cnt = (select max(challenge_id) from Challenges)
OR cnt IN ( SELECT cnt
FROM chl_count
WHERE hacker_id= 1)
ORDER BY cnt DESC, hck.hacker_id ASC;
It should work now.

Extract state with maximum count from subquery

Select a.states,MAX(a.cnt) from
(Select ci_location.state as states,Count(CI_Location.State) Cnt from
Fact_Transactions ft
Inner join CI_Location on ft.ID_Location = CI_Location.ID_Location
Where id_model like 'smsg%'
Group by State)a
Group by a.states,a.Cnt
Output:-
State Count
Alabama 7
Arizona 15
Arkansas 4
California 100
Colorado 7
Required output:-
state output
California 100
This code isn't extracting STATE with maximum count value from my table...Is something wrong??
Try the following
SELECT TOP 1 -- or TOP 1 WITH TIES
ci_location.state as states,
Count(CI_Location.State) Cnt
FROM Fact_Transactions ft
INNER JOIN CI_Location ON ft.ID_Location = CI_Location.ID_Location
WHERE id_model like 'smsg%'
GROUP BY ci_location.state
ORDER BY Cnt DESC
just remove the a.cnt from group by
select a.states,MAX(a.cnt) from
(Select ci_location.state as states,Count(CI_Location.State) Cnt from
Fact_Transactions ft
Inner join CI_Location on ft.ID_Location = CI_Location.ID_Location
Where id_model like 'smsg%'
Group by State)a
Group by a.states
You should use row_number() function approach to get the maximum count state
SELECT * FROM
(
SELECT
l.state as states,
Count(l.State) as Cnt,
row_number() over (order by Count(l.State) desc) Sq
FROM Fact_Transactions ft
INNER JOJN CI_Location l on ft.ID_Location = l.ID_Location
WHERE l.id_model like 'smsg%'
GROUP BY l.State
) a
WHERE sq = 1
Note : For your current query you are further grouping the derived table result. Try, to remove GROUP BY clause & just use max() to get the highest count state only.

Display the city name which has most number of branches

I have tried to get city name which has most number of branches .
select C.City_name ,count(B.B_Name)
from tblcity C
inner join
tblBranch B
on c.city_id=B.City_id
group by C.City_name
order by count(B.B_Name) desc
Above code will give me the count of branches for particular city .
Please help me solve to get city name which has most number of branches
you can add TOP 1 to your query
select TOP 1 C.City_name ,count(B.B_Name)
from tblcity C
inner join
tblBranch B
on c.city_id=B.City_id
group by C.City_name
order by count(B.B_Name) desc
Use DENSE_RANK():
SELECT
City_Name, cnt
FROM
(
SELECT
c.City_name,
COUNT(b.B_Name) cnt,
DENSE_RANK() OVER (ORDER BY COUNT(b.B_Name) DESC) dr
FROM tblcity c
INNER JOIN tblBranch b
ON b.city_id = c.City_id
GROUP BY c.City_name
) t
WHERE dr = 1;
Using TOP 1 WITH TIES would be another option here, but that is specific to SQL Server.

Highest Count with a group

I'm having an absolute brain fade
SELECT p.ProductCategory, f.ProductSubCategory, COUNT(*) AS Cnt
FROM Sales f
JOIN Products p ON f.ProductSubCategory = p.ProductSubCategory
GROUP BY p.ProductCategory, f.ProductSubCategory
ORDER BY 1,3 DESC
This shows me the count for each ProductSubCategory, I would like to see only the highest ProductSubCategory per ProductCategory.
I wish to see (I don't care about the Count value)
There are a couple of different ways to do this. One involves joining the results back to themselves and using the max aggregate. But since you are using SQL Server, you can use ROW_NUMBER to achieve the same result:
with cte as (
select p.productcategory, p.ProductSubCategory, COUNT(*) cnt,
ROW_NUMBER() over (partition by p.productcategory order by count(*) desc) rn
from products p
join sales s on p.ProductSubCategory = s.ProductSubCategory
group by p.productcategory, p.ProductSubCategory
)
select *
from cte
where rn = 1
You already got the answer, Please see the following code to. It may help you.
SELECT p.ProductCategory,
f.ProductSubCategory,
COUNT(*) AS Cnt
FROM Sales f
JOIN Products p ON f.ProductSubCategory = p.ProductSubCategory
JOIN (
SELECT p.ProductCategory,
f.ProductSubCategory,
ROW_NUMBER() OVER ( PARTITION BY p.ProductCategory,
f.ProductSubCategory
ORDER BY COUNT(*) DESC) [Row]
FROM Sales f
JOIN Products p ON f.ProductSubCategory = p.ProductSubCategory) Lu
ON P.ProductCategory = Lu.ProductCategory
AND f.ProductSubCategory = Lu.ProductSubCategory
WHERE Lu.Row = 1
GROUP By p.ProductCategory,
f.ProductSubCategory