I'm trying to get the PlayerID from the result I get in following image
The query I use to display that is:
SELECT PlayerIDFK, sum(TwoPointMade) as TwoPointMade, sum(ThreePointMade) as ThreePointMAde
FROM PlayerPerformance GROUP BY PlayerIDFK;
I also have another query which returns the maximum value of sums of both columns in this case 47, which is the correct answer, but I also want to get the PlayerIDFK.
SELECT MAX(SUM(TwoPointMade) + SUM(ThreePointMade)) AS "Points"
FROM PlayerPerformance GROUP BY PlayerIDFK;
When I try to get the player ID using the query I get not a single-group group function. This is the query Im trying to use:
SELECT PlayerIDFK, MAX(SUM(TwoPointMade) + SUM(ThreePointMade)) AS "Points"
FROM PlayerPerformance GROUP BY PlayerIDFK;
Use order by and rownum (or fetch first 1 row only in 12c+):
SELECT *
FROM (SELECT PlayerIDFK, sum(TwoPointMade) as TwoPointMade, sum(ThreePointMade) as ThreePointMAde
FROM PlayerPerformance
GROUP BY PlayerIDFK
ORDER BY ( sum(TwoPointMade) + sum(ThreePointMade) ) desc
) pp
WHERE rownum = 1;
Related
select max(created_t) from inv_database ;
o/p = 78578596
select in_date from inv_database where created_t = 78578596;
I want to merge these two queries and want the result.
There are 4 records which have maximum created_t and all of them have same in_date.I want to get that date. what query it will need?
Use a subquery:
select in_date
from inv_database
where created_t = (select max(created_t) from inv_database);
You could also use an analytic function like row_number:
select t.in_date
from
(
select in_date,
row_number() over (order by created_t desc) rn
from inv_database
) t
where t.rn = 1;
SubQuery will help you...
select in_date as Date
from inv_database
where created_t
= (select max(created_t) from inv_database);
you can try it based on your table column structure.
I have a match table with winningteamid and stadiumid as attributes,
I need to retrieve the winningteamid which has won all its games in the same stadium.
I tried this, and I'm getting additional unwanted rows:
select winningteamid
from match
group by winningteamid
having count(winningteamid) in (
select count(*) from match group by (winningteamid,stadiumid)
Try this please (please write which RDBMS you are using):
with cte as (
select winningteamid, stadiumid, count(stadiumid) over (partition by winningteamid) as count
from match
group by winningteamid, stadiumid
)
select * from cte where count = 1;
You should use this:
SELECT MAX(winningteamid)
FROM (
SELECT DISTINCT winningteamid, stadiumid
FROM match
)
GROUP BY stadiumid
HAVING COUNT(*) = 1;
I suppose it's as easy as using HAVING to verify only one distinct stadiumid:
select winningteamid
from match
group by winningteamid
having count(distinct stadiumid) = 1
I'm a using distinct select in the Oracle SQL, what I want to do is sum up all the data at specific ID. Example in image:
So for example where PlayerIDFK is 1 I want to sum up TwoPointsMade in one column and ThrePointsMade in another, so for that the result would be
PlayerIDFK TwoPointsMade ThreePointsMade
--------------------------------------------------
1 5 2
The query Im using now is:
SELECT PlayerIDFK, TwoPointMade, ThreePointMade
FROM PlayerPerformance
WHERE PlayerIDFK IN (SELECT DISTINCT PlayerIDFK
FROM PlayerPerformance);
I think you could use the groupby-clause where you could group by playerIDFK and sum over twopointsmade and threepointsmade
Something like this might work:
SELECT PlayerIDFK, sum(TwoPointMade), sum(ThreePointMade) FROM PlayerPerformance GROUP BY PlayerIDFK
if you want to get the max you can do a nested query, something like this:
SELECT MAX(inside_query.m1, inside_query.m2) FROM (SELECT PlayerIDFK, sum(TwoPointMade) as m1, sum(ThreePointMade)as m2 FROM PlayerPerformance GROUP BY PlayerIDFK) as inside_query
there might be a more elegant way to do it but thats what I got :)
Maybe this might help:
SELECT * FROM (
SELECT PlayerIDFK, sum(TwoPointMade) as TwoPointMade
FROM PlayerPerformance
GROUP BY PlayerIDFK
ORDER BY sum(TwoPointMade) DESC
) WHERE ROWNUM = 1
UNION ALL
SELECT * FROM (
SELECT PlayerIDFK, sum(ThreePointMade) as ThreePointMade
FROM PlayerPerformance
GROUP BY PlayerIDFK
ORDER BY sum(ThreePointMade) DESC
) WHERE ROWNUM = 1
I have this query currently, which selects the top "number of pickups" in descending order. I need to filter only the top 10 rows/highest numbers though. How can I do this?
I have tried adding 'WHERE ROWNUM <= 10' at the bottom, to no avail.
SELECT customer.company_name, COUNT (item.pickup_reference) as "Number of Pickups"
FROM customer
JOIN item ON (customer.reference_no=item.pickup_reference)
GROUP BY customer.company_name, item.pickup_reference
ORDER BY COUNT (customer.company_name) DESC;
Thanks for any help!
You need to subquery it for the rownum to work.
SELECT *
FROM
(
SELECT customer.company_name, COUNT (item.pickup_reference) as "Number of Pickups"
FROM customer
JOIN item ON (customer.reference_no=item.pickup_reference)
GROUP BY customer.company_name, item.pickup_reference
ORDER BY COUNT (customer.company_name) DESC
)
WHERE rownum <= 10
You could alternatively use ranking functions, but given the relative simplicity of this, I'm not sure whether I would.
The solution by using the rank is something like this :
select customer.company_name, COUNT (item.pickup_reference) from (
select distinct customer.company_name, COUNT (item.pickup_reference) ,
rank() over ( order by count(item.pickup_reference) desc) rnk
from customer
JOIN item ON (customer.reference_no=item.pickup_reference)
group by customer.company_name, item.pickup_reference
order by COUNT (customer.company_name) )
where rnk < 10
Using the 'rownum' to get the top result doesn't give the expected result, because it get the 10 first rows which are not ordred, and then order them (Please notify this on a comment on Andrew's response, I don't have the right to add the comment) .
how do i improved this code.
select code,max(total) from
(select code,count(*) from table_1 group by code)
The above code,not working because I try to do MAX function onto the result set from query,but failed.
If you only want the number, then you can use this:
select max(total)
from (
select code,
count(*) as total -- you forgot the column alias here
from table_1
group by code
)
If you want the code and the number, use the following:
with count_result as (
select code,
count(*) as total
from table_1
group by code
)
select code,
total
from count_result
where total = (select max(total) from count_result);