How to Group by and find max value in sql [ Oracle] [duplicate] - sql

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Get value based on max of a different column grouped by another column [duplicate]
(1 answer)
SQL: getting the max value of one column and the corresponding other columns [duplicate]
(2 answers)
Closed 1 year ago.
I have data in my database that I need to group by and find the max value based on frequency. For example I want to select both the ID and the Country with the maximum NumberOfVisits per ID.
sample data:
ID
Country
NumberOfVisits
1
US
5
2
UK
7
1
UAE
10
The Output I am expecting is :
ID
Country
1
UAE
2
UK

With a little help of analytic functions (sample data in lines #1 - 8; query begins at line #9):
SQL> WITH
2 test (id, country, num)
3 AS
4 (SELECT 1, 'US', 5 FROM DUAL
5 UNION ALL
6 SELECT 2, 'UK', 7 FROM DUAL
7 UNION ALL
8 SELECT 1, 'UAE', 10 FROM DUAL),
9 temp
10 AS
11 (SELECT id,
12 country,
13 num,
14 RANK () OVER (PARTITION BY id ORDER BY num DESC) rnk
15 FROM test)
16 SELECT id, country, num
17 FROM temp
18 WHERE rnk = 1;
ID COU NUM
---------- --- ----------
1 UAE 10
2 UK 7
SQL>

Related

Oracle - Display an incremental ID by a simple query [duplicate]

This question already has an answer here:
How to add a row number within a group in my query
(1 answer)
Closed 2 years ago.
I have a very simple query like this:
SELECT Line, ID, Enum
FROM tab1
the result is something like this:
Line id enum
A 10 1
A 10 2
A 10 3
A 10 4
...
B 20 4
B 20 5
B 20 6
B 20 7
...
I want, for each Line/id, the anum start by 1, just like this:
Line id enum
A 10 1
A 10 2
A 10 3
A 10 4
...
B 20 1
B 20 2
B 20 3
B 20 4
...
Any Idea?
You need an Analytic function such as ROW_NUMBER() :
SELECT Line, ID,
ROW_NUMBER() OVER (PARTITION BY Line ORDER BY Enum ) AS Enum
FROM tab1
where that function enumerates each Enum value again per each Line group starting from the integer 1, and sorted by currently existing Enum value
Perhaps you need grouping by Line and ID, then use PARTITION BY Line, ID instead of PARTITION BY Line.
Both will produce the same result in this case.
Demo

Increment row number based on value 1 and value 2 [duplicate]

This question already has answers here:
How to use RANK() in SQL Server
(8 answers)
Closed 3 years ago.
How can I create a sequential value based on two rows within a table, for example, let's say I have a table containing an employee's ID and work state. I would expect the following values:
ID State Expected Value
-----------------------------
1 NY 1
1 PA 2
1 NY 1
2 NC 1
2 FL 2
2 MN 3
You can use dense_rank():
select t.*,
dense_rank() over (partition by id order by state) as expected
from t;

how get first row from duplicata values Oracle SQL query [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Get top results for each group (in Oracle)
(5 answers)
Select latest row for each group from oracle
(3 answers)
How to get only one record for each duplicate rows of the id in oracle?
(3 answers)
Closed 3 years ago.
Below you can find my table with values (there are no constraints on my table):
SELECT DISTINCT * FROM khalil;
outputs:
ID VALUE
-- -------
1 yassine
1 khalil
2 amine
I need to get the first row when I have duplicate values.
I have two rows with id = 1 so, in this case, I need that the first one,
which is id = 1 and value = 'yassine'
SELECT * FROM khalil
WHERE ROWID IN (SELECT MIN(ROWID) FROM khalil GROUP BY id)
ORDER BY id
This will return the first row for each id.
If you don't really care which value you'll get (unless there's something you can use to distinguish values), aggregates - such as min or max - can help:
SQL> select id,
2 max(value) value
3 from khalil
4 group by id
5 order by id;
ID VALUE
---------- --------------------
1 yassine
2 amine
SQL>
Alternatively, using analytic functions (such as row_number, which lets you sort values), you'd do it as follows:
SQL> with temp as
2 (select id,
3 value,
4 row_number() over (partition by id order by value desc) rn
5 from khalil
6 )
7 select id,
8 value
9 from temp
10 where rn = 1
11 order by id;
ID VALUE
---------- --------------------
1 yassine
2 amine
SQL>

SQL Group By min value usable as a variable? [duplicate]

This question already has answers here:
MySQL - Fetching lowest value
(7 answers)
Closed 3 years ago.
I have the following table ComponentsB:
CID Name PPP ProcessingCharge LName Typ
1 RadXL 10 5 EisenAG Rad
2 RadXL 15 0 LederGmbH Rad
3 RadL 10 2 LederGmbH Rad
4 RadXL 2 1 RadAG Rad
5 RadXL 4 10 sdfkj Rad
6 SchraubeM 1 2 EisenAG Schraube
7 ZangeS 3 11 EisenAG Zange
8 ZangeM 12 12 sdfkj Zange
I want to get for every "Name" the lowest "ProcessingCharge" with the corresponding LName
select Name, min(ProcessingCharge)
from ComponentsB
group by Name
works perfectly fine but I can't get the corresponding LName because I get the error that it is not part of the aggregate function.
Don't think of this query as an aggregation query. Think of it as a filtering query. You want the row for each Name that has the minimum charge:
select c.*
from ComponentsB c
where c.ProcessingCharge = (select min(c2.ProcessingCharge)
from ComponentsB c2
where c2.Name = c.Name
);

SQL involving MAX of two colums and Group BY [duplicate]

This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Closed 7 years ago.
So... i got a table like this:
id group number year
1 1 1 2000
2 1 2 2000
3 1 1 2001
4 2 1 2000
5 2 2 2000
6 2 1 2001
7 2 2 2001
8 2 3 2001
And i need to select the bigger number of the bigger year for each group. So i expect the result of the exemple to be:
3 1 1 2001
8 2 3 2001
any ideias?
OBS: using Postgres
SELECT *
FROM (
SELECT *,
row_number() over (partition by "group" order by "year" desc, "number" desc ) x
FROM table1
) x
WHERE x = 1;
demo: http://sqlfiddle.com/#!15/cd78e/2
If it's just certain rows you want to get you can use DISTINCT. If you want different maximums on the same rows you could use GROUP BY
SELECT DISTINCT ON ("group") * FROM tbl
ORDER BY "group", year DESC, id DESC;