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

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
);

Related

How to Group by and find max value in sql [ Oracle] [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)
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>

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;

SQL- Do I need some kind of Count function? [duplicate]

This question already has answers here:
Selecting COUNT(*) with DISTINCT
(7 answers)
Closed 4 years ago.
I need to query a database that has about 10-11 columns, including a column of id's and a column of role codes. Those are the 2 column that i'm interested in.
ID ROLE
1 a
2 a
2 b
2 c
3 a
4 a
4 b
I need to count how many role codes exist for each ID. (Basically like counting the number of times each id exists in the database)
Output should be something like this:
ID Count
1 1
2 3
3 1
4 2
Use count distinct:
SELECT ID, COUNT(DISTINCT ROLE)
FROM YOURTABLE
GROUP BY ID

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;