how get first row from duplicata values Oracle SQL query [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)
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>

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>

How to select from SQL table so even and odd rows would be in separate columns?

There is a table ID in my PostgreSQL database.
select * from ID;
ID
1
2
3
4
5
6
7
8
I need to write a query that will give me this output:
id id
1 2
3 4
5 6
7 8
There are no such things as even and odd rows. SQL tables represent unordered sets (well technically multi-sets).
Assuming id is the ordering, you can split them using aggregation like this:
select min(id), max(id)
from (select t.*, row_number() over (order by id) - 1 as seqnum
from t
) t
group by floor(seqnum / 2)

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 - select single ID for each group with the lowest value

Consider the following table:
ID GroupId Rank
1 1 1
2 1 2
3 1 1
4 2 10
5 2 1
6 3 1
7 4 5
I need an sql (for MS-SQL) select query selecting a single Id for each group with the lowest rank. Each group needs to only return a single ID, even if there are two with the same rank (as 1 and 2 do in the above table). I've tried to select the min value, but the requirement that only one be returned, and the value to be returned is the ID column, is throwing me.
Does anyone know how to do this?
Use row_number():
select t.*
from (select t.*,
row_number() over (partition by groupid order by rank) as seqnum
from t
) t
where seqnum = 1;

Calculate "position in run" in SQL

I have a table of consecutive ids (integers, 1 ... n), and values (integers), like this:
Input Table:
id value
-- -----
1 1
2 1
3 2
4 3
5 1
6 1
7 1
Going down the table i.e. in order of increasing id, I want to count how many times in a row the same value has been seen consecutively, i.e. the position in a run:
Output Table:
id value position in run
-- ----- ---------------
1 1 1
2 1 2
3 2 1
4 3 1
5 1 1
6 1 2
7 1 3
Any ideas? I've searched for a combination of windowing functions including lead and lag, but can't come up with it. Note that the same value can appear in the value column as part of different runs, so partitioning by value may not help solve this. I'm on Hive 1.2.
One way is to use a difference of row numbers approach to classify consecutive same values into one group. Then a row number function to get the desired positions in each group.
Query to assign groups (Running this will help you understand how the groups are assigned.)
select t.*
,row_number() over(order by id) - row_number() over(partition by value order by id) as rnum_diff
from tbl t
Final Query using row_number to get positions in each group assigned with the above query.
select id,value,row_number() over(partition by value,rnum_diff order by id) as pos_in_grp
from (select t.*
,row_number() over(order by id) - row_number() over(partition by value order by id) as rnum_diff
from tbl t
) t