Is there a way to get the average of a column which the query is not grouped by - sql

I dont rly know how to explain my Problem, but i have a Query where i need to group by a column but on the other i need to get an avg of a column which is not grouped by.
My Code is like this:
Select SID,PID,Cost, AVG(COST)
from catalog
group by SID,PID
ORDER by SID
All Columns are in the same table.
What can i do to get the AVG(Cost) of PID?
My Question is related to an exam question which is the following: Find the SID's who charge more for some PID than the average cost of that PID.
The table has the columns SID, PID, COST. I cant upload pictures of the table because my account is new, so im sorry.
So my Problem was that i couldnt get the AVG of the PID, my next Problem because i already tried it with Partition is, that i dont know how the having clause has to look like. Do i need a sub-query for that?

You can use window functions to add an average to a row:
select SID, PID, Cost,
avg(cost) over (partition by pid) as avg_cost_for_pid
from catalog
order by sid;

Related

How to get rolling unique count of employees per year based on key fields

I have the following table and I wanted to get the running unique count by dept, team, level. But the cumulative unique count will restart per year.
Note: sorry in my main table example, employee numbers may repeat up to four times. There is another column called leave type but wasn't able to illustrate it in the image
Main table
Expected output would be something like below.
Expected output
Is this possible? Apologies. Not too advanced when it comes to SQL. Thank you.
You can do:
select max(extract(year from date)), date, department, level, team, count(*)
from t
group by date, department, level, team

How to use max on sum in oracle sql?

I am working in a task and got stuck at particular question. I am new to SQL so I am reaching out to this platform for the support. Below are the 2 tables. 1st is Theatre_play_table and 2nd is Ticketsales table.
Question: List titles, directors and writers of all shows/plays with the highest total sale.
Theatre_play_table
Ticketsales table
I have pasted screenshot of some part of the table. ID column in both the table represents the same information. Last column in Ticketsales table is Totalamount.
I have tried with below query;
Select theatre_play.title, theatre_play.director, theatre_play.writer, sum(totalamount)
from theatre_play, totalsales
where theatre_play.id = totalsales.id
group by theatre_play.title, theatre_play.director, Theatre_play.writer
order by sum(totalamount) desc
fetch first 3 rows only;
The above approach is not useful when data is huge. I wanted to apply max(sum(totalamount)) but oracle threw an error.
Can anyone please help me solve this question?
If I understand you right, the issue is to get the three highest values?
Then try something like this:
select * from (
Select dpro.title, dpro.director, dpro.writer, sum(fth.totalamount)
from dpro
join fth on dpro.id = fth.id
group by dpro.title, dpro.director, dpro.writer
order by sum(totalamount) desc )
where rownum <=3

Total Count of each subtype and assign to every record again

I am novice to SQL. I am learning count(). So here is what I am trying to do. There is a table in which Products and Product Types are listed. I want an output which will have a separate column which gives total count of each product_type and assigns it to every record. Can anyone help me to write this query? I searched the forums, but couldn't find similar requirement. Please find attached image for Source table and target table example.
Thank you,
DP
Case for SQL Query
Use windowed COUNT:
SELECT Product,
Product_Type,
COUNT(*) OVER(PARTITION BY Product_Type) AS "Count"
FROM table

Count(), max(),min() fuctions definition with many selects

Lets say we have a view/table hotel(hotel_n,hotel_name, room_n, price). I want to find the cheapest room. I tried group by room_n, but I want the hotels name (hotel_name) to be shown to the board without grouping it.
So as an amateur with sql(oracle 11g) I began with
select hotel_n, room_n, min(price)
from hotel
group by room_n;
but it shows the error: ORA-00979: not a GROUP BY expression. I know I have to type group by room_n, hotel_n, but I want the hotel_n to be seen in the table that I make without grouping by it!
Any ideas? thank you very much!
Aggregate functions are useful to show, well, aggregate information per group of rows. If you want to get a specific row from a group of rows in relation to the other group members (e.g., the cheapest room per room_n), you'd probably need an analytic function, such as rank:
SELECT hotel_n, hotel_name, room_n, price
FROM (SELECT hotel_n, hotel_name, room_n, price
RANK() OVER (PARTITION BY room_n ORDER BY price ASC) rk
FROM hotel) t
WHERE rk = 1

SQL grouping results in a select

My SQL table "offers" contains offers users make for products (product_ID, customer_ID, offer).
In my admin page, I want to list the products for which at least one offer exists and show the total offers existing for it.
For example,
PRODUCT #324 Total offers: 42
PRODUCT #99 Total offers: 1
etc.
My guess would be to combine a
SELECT DISTINCT product_ID FROM offers...
And in a second query, to SELECT COUNT(*) FROM offers WHERE product_ID=...
Is it the most efficient way to achieve this, or is there a way to make it inside a single query?
You can do this in one query which will get the count by grouping by the product_id:
SELECT product_ID, COUNT(*)
FROM offers
GROUP BY product_ID
As bluefeet already answered, you achieve it in single query by using group by.
(group by demo)
Another thing to mention is the order by,
select
product_id as id,
count(*) as totals
from
t
group by product_id
order by totals;
If you want to sort with the totals of hits, or if you want to sort by product_id etc.
sqlfiddle