select max from a group by [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)
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)
Oracle SQL query: Retrieve latest values per group based on time [duplicate]
(2 answers)
Closed 2 years ago.
I've got a very simple question, I hope.
I have a table MYTABLE like this:
FIELD1 FIELD2 FREQ
A1 B 10
A1 C 20
A1 D 5
A2 X 7
A2 Y 12
...
and I want to get something like this:
enter code here
FIELD1 FIELD2
A1 C
A2 Y
that is I want to get for every distinct FIELD1 the row having the max(FREQ) but also with its FIELD2 value
Oracle 10g
Thanks in advance!
Mark

you can use analytical functions to get this done
select * from (
select x.*,row_number() over(partition by field1,field2 order by field1 asc) as rnk from (
select field1
,field2
,max(field2) over(partition by field1) as max_value
from mytable
)x
where max_value=field2
)y
where y.rnk=1

Use window functions.
select FIELD1, FIELD2
from (
select *, rank() over (partition by FIELD1 order by FREQ desc) rn
from mytable
) t
where t.rn = 1
If you use RANK() then you return all rows with MAX(FREQ) for each FIELD1.

use row_number()
select b.* from
(select a.* ,row_number()over (partition by FIELD1 orer by FREQ desc) rn
from table_name a
) b where b.rn=1

Related

Selecting a nth row from a table using SQL in Oracle, MYSQL and SQL Server assuming there is no ordered column in the table [duplicate]

This question already has answers here:
ROW_NUMBER() in MySQL
(26 answers)
SQL - 2nd highest record in table
(2 answers)
Closed 4 years ago.
Let us take a table t1
SQL > Select * from t1;
COL1
9
1
8
6
4
Q) Query to retrieve third row from the table.
A) Oracle :
SQL > select col1 from (select col1, row_number() over(order by rowid) rn from t1) where rn=3;
As rowid psuedo column doesn't exist in other databases, how can retrieve a nth record from a table in databases like MYSQL, SQL Server etc.
SQL Server also, you have the ROW_NUMBER Function
You Can Go like
;WITH CTE
AS
(
SELECT
RN = ROW_NUMBER() OVER(ORDER BY EmployeeID),
*
FROM dbo.Employee
)
SELECT
*
FROM CTE
WHERE RN = 5--To Get the 5th Record
for mssql you can use row_number() window funciton
select * from
(
select *, row_number() over(order by col1) rn from t
) t1 where t1.rn=1 -- 2 or 3, n

Keep Track of already summed tuples sql

If we have a table with values for a and b, is there a way to only add up the b's if its not a duplicate a? For example
a b
1 2
2 3
2 3
so we would get only 5 (instead of 8)
A sort of
select sum(b if unique a),
from table
where ...
The following query selects the lowest value of b for each group a
select min(b) min_b
from mytable
group by a
You can then sum those values by selecting the sum from a derived table
select sum(min_b) from (
select min(b) min_b
from mytable
group by a
) t
http://sqlfiddle.com/#!9/d82c5/1
You haven't specified your RDBMS, but if you are using a database which supporting window functions like SQL Server, you can query the unique rows first by using WITH clause and ROW_NUMBER() function and then get the SUM out of that.
;WITH C AS(
SELECT a, b,
ROW_NUMBER() OVER (PARTITION BY a ORDER BY a) AS Rn
FROM Table1
)
SELECT SUM(b) FROM C
WHERE Rn = 1
SQL Fiddle

select duplicate columns in sql server [duplicate]

This question already has answers here:
Select statement to find duplicates on certain fields
(9 answers)
Closed 7 years ago.
Table 1
Id Name
1 xxxxx
1 ccccc
2 uuuuu
3 ddddd
I want to select where the Id have multiple entries with same Id
How to do this?
You can find ids with multiple entries and then use LEFT JOIN/IS NOT NULL pattern to retrieve corresponding data from the original table :
SELECT t1.*
FROM tbl t1
LEFT JOIN ( SELECT id
FROM tbl
GROUP BY id
HAVING COUNT(*) > 1) t2 ON t1.id = t2.id
WHERE t2.id IS NOT NULL
Other possible solutions include using EXISTS or IN clauses instead of LEFT JOIN/IS NOT NULL.
With ranking functions
Y as (
select *, count(*) over (partition by id) counter
from X)
select id, name from Y where counter > 1

Auto-increment the value only when the value of another column changes sql server

Let's say I have this (SQL server) database, sorted by value A and value B should be incremented only when the value A gets change:
BillNo Value B
SC-P1100 1
SC-P1100 1 BillNo changes
SC-blb00 2 BillNo changes
SC-P6010 3
SC-P6010 3
SC-P6010 3 BillNo changes
SB-T1810 4
How do I select the rows in the above manner? Please answer
Thanks
You can use CTE to do that.
WITH CTE (Col1, RowNumber)
AS
(
SELECT <YOUR_COLUMN_NAME_HERE> AS Col1, ROW_NUMBER() OVER (ORDER BY Col1) As RowNumber
FROM <YOUR_TABLE_NAME_HERE>
)
SELECT Col1,
(
SELECT COUNT(DISTINCT Col1)
FROM CTE InnerCTE
WHERE InnerCTE.RowNumber <= OuterCTE.RowNumber
) As NumberOfDistinctItemsInCol1
FROM CTE OuterCTE
Be sure to replace <YOUR_COLUMN_NAME_HERE> with your actual column name and <YOUR_TABLE_NAME_HERE> with your actual table name.
You can do it like this
UPDATE tableA
SET BillNo='new value'
, ValueB=ValueB+1
WHERE BillNo='Old value';

how to find the most appears one in a table using sql?

I have a table A with two columns named B and C as following:
('W1','F2')
('W1','F7')
('W2','F1')
('W2','F6')
('W2','F8')
('W4','F7')
('W6','F2')
('W6','F15')
('W7','F1')
('W7','F4')
('W7','F17')
('W8','F13')
How can I find which one in the B column appears with the most time using sql in oracle? (In this case, it's W2 and W7). Thank you!
Use a subquery to calculate the number of items in columC for each value in columnB and rank() the results of the subquery based on that count. Then in your main select return just the values of columnB where the rank of the rows returned by the subquery is 1:
SELECT ColB
FROM (
SELECT ColB,
Count(ColC),
rank() over (ORDER BY Count(ColC) DESC) AS rnk
FROM yourTable
GROUP BY ColB)
WHERE rnk = 1
Here's a sql fiddle: http://sqlfiddle.com/#!4/fa6bd/2
/*
C2 REFERS TO THE COLUMN B
T1 Refers to an alias
*/
WITH T1 AS
(
SELECT C2,COUNT(*) AS COUNT
FROM YOURTABLE
GROUP BY C2
)
SELECT C2,COUNT FROM T1 WHERE COUNT=(SELECT MAX(COUNT) FROM T1 )
;
Select ColB, Count(*)
FROM yourTable
GROUP BY ColB
ORDER BY count(*) desc