This question already has answers here:
how to select the most frequently appearing values? [duplicate]
(2 answers)
Closed 9 years ago.
I have a table with column 'Price' and would need to get the most frequent value. What would be the eeasiest way?
One option would be something like
SELECT price
FROM (SELECT price, rank() over (order by cnt desc) rnk
FROM (SELECT price, count(*) cnt
FROM your_table
GROUP BY price))
WHERE rnk = 1
If there are two (or more) prices that occur equally as often, both will be returned by this query. If you want to guarantee a single row, you'll need to tell us how you want to handle ties.
My algorithm is as follows:
Step one: make distinct selection as a collection;
Step two: foreach item in distinct collection count the items found in the original collection as diffcollection;
Step three: select max from diffcollection.
Related
This question already has answers here:
Oracle SELECT TOP 10 records [duplicate]
(6 answers)
Oracle SQL - How to Retrieve highest 5 values of a column [duplicate]
(5 answers)
Closed 2 years ago.
Hey guys I know the code to show the most expensive movie but what's the one that will show the most expensive and ones right below it. I think that's the question. This is the code I got for one movie.
SELECT *
FROM movie
WHERE purchase_price =
(SELECT MAX(purchase_price) FROM movie);
Well since your description is a little ambiguous, to find your prefer solution, you will have to try several of them.
For example, you can try by using an ORDER BY Condition. Using this condition, you will retrieve all the movies starting with the most expensive one at the top.
SELECT
*
FROM
movie
ORDER BY
purchase_price DESC;
FETCH FIRST 2 ROWS ONLY
But yet again, there are other solutions you can try as well. You can RANK them by price in a subquery and then fetch all the answers. Another example would be to use between max and min ( or any other value ). You can reach even some more technical and harder to implement solutions.
Rank them by price (in a subquery), and fetch the first two. Something like this:
select *
from (select m.*,
rank() over (order by m.purchase_price desc) rnk
from movie m
)
where rnk <= 2
Depending on data you have, you might also consider using ROW_NUMBER or DENSE_RANK analytic functions.
If you strictly want the two most expensive movies, you could order the query and use a fetch first clause:
SELECT *
FROM movie
ORDER BY purchase_price DESC
FETCH FIRST 2 ROWS ONLY
If multiple movies can have the same price, and you want to find all the movies with two most expensive prices, you could use the dense_rank window function:
SELECT *
FROM (SELECT *, DENSE_RANK() OVER (ORDER BY purchase_price DESC) AS rk
FROM movie) m
WHERE rk <= 2
I would rather use the FETCH FIRST 2 ROWS WITH TIES option which will give you the first two most expensive movies and also takes care of multiple movies with the same purchase price
SELECT *
FROM movie
ORDER BY purchase_price DESC
FETCH FIRST 2 ROWS ONLY TIES;
This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 4 years ago.
I need to create a different column of total sales which is the total of the sales units till that row. As our sales unit will increase, the total sales will increase row by row. I have attached the image to get the clear idea.
Screenshot
One method is use to use of subquery with correlation approach
select *,
(select sum(sales) from table where product = t.product and ? <= t.?) TotalSales
from table t
However, you would required ? (i.e. rowid or id) column that could specify your column ordering.
You can use the following query, which adds up all the sales upto that rowId and for the specific product.
select (select sum(ids)
from TBL_NAME T1
where T1.rowid <= TBL_NAME.rowid and PRODUCT=TBL_NAME.PRODUCT
) as TotalSales
from TBL_NAME;
Following answer is for Oracle DB, you can understand the query and easily convert it to the product you want.
This question already has answers here:
Why no windowed functions in where clauses?
(8 answers)
Closed 5 years ago.
I have a table that consists of an order, items on that order, and then the quantity of the item ordered.
What I would like to do is create an additional column for 'Order quantity' which is the sum of item quantities grouped by order (see graphic of table below... order B has 30 total quantity split across three lines)
I can easily accomplish this using sum and partition:
SUM(quantity) OVER (PARTITION BY order_id) order_qty
However, what I need to is then filter to only those orders having quantity > 20. When I try to add that criteria to the WHERE or HAVING clauses, I get this error:
ORA-30483: window functions are not allowed here
One solution appears to be to wrap the whole SQL block inside of another SELECT FROM statement, and then add a WHERE clause to filter by order_qty. Overall that seems sloppy and non-intuitive... Is there a better solution to filter based on an aggregated value that is partitioned at a higher level?
Replace with
SUM(quantity) OVER (PARTITION BY order_id order by 1) order_qty
This question already has answers here:
How can I remove duplicate rows?
(43 answers)
Closed 8 years ago.
suppose you have a data set having 3 columns: ID, user, date.
is it possible to filter the data based on the minimum date even if some of the rows have identical IDs?
sorry if the question is a bit unclear. hopefully the image below will help clear things.
there are two records having ID=1, with different users as well as dates. what i want to retrieve is the record having an ID=1, USER=A, DATE=2013-01-20 because its date is earlier than that of the second record (ID=1, USER=A, DATE=2013-01-21)
i want to achieve the same effect for the three records having an ID=2. the desired record is ID=2,USER=C,DATE=2013-10-20
basically i want to group these records by their IDs and then from that grouping, get the one with the lowest date
SELECT id, user, date
FROM OriginalData od
WHERE date = (SELECT MIN(date)
FROM OriginalDate od1
WHERE od.id = od1.id)
Select * from table_name where date in
( select MIN(date) from table_name)
You have tyo use Group By clause on Id attribute.
Use the following syntax
select * from tab1 where (id, date) in (select id, min(date) from tab1 group by(id))
This question already has answers here:
Get most common value for each value of another column in SQL
(9 answers)
Closed 8 years ago.
I've seen examples where the query orders by count and takes the top row, but in this case there can be multiple "most frequent" values, so I might want to return more than just a single result.
In this case I want to find the most frequently appearing last names in a users table, here's what I have so far:
select last_name from users group by last_name having max(count(*));
Unfortunately with this query I get an error that my max function is nested too deeply.
select
x.last_name,
x.name_count
from
(select
u.last_name,
count(*) as name_count,
rank() over (order by count(*) desc) as rank
from
users u
group by
u.last_name) x
where
x.rank = 1
Use the analytical function rank. It will assign a numbering based on the order of count(*) desc. If two names got the same count, they get the same rank, and the next number is skipped (so you might get rows having ranks 1, 1 and 3). dense_rank is an alternative which doesn't skip the next number if two rows got the same rank, (so you'd get 1, 1, 2), but if you want only the rows with rank 1, there is not much of a difference.
If you want only one row, you'd want each row to have a different number. In that case, use row_number. Apart from this small-but-important difference, these functions are similar and can be used in the same way.
select name
from
(select name, count(1)
from table
group by name
order by count(1) desc) a
where rownum = 1