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

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;

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>

Getting top 5 most associated item [duplicate]

This question already has answers here:
How to get highest count of associated model (Rails)?
(3 answers)
Closed 1 year ago.
I have a Postgres database with a many-to-many association table that's similar to what's down below.
id | item_id | item_tag_id
1 101 3
2 102 3
3 103 1
4 104 2
5 105 2
How can I get the top 5 most associated item_tag_id?
Basically, group by item and order by the count of rows (= count of items in a proper many-to-many design):
SELECT item_id, count(*)
FROM assoc_tbl
GROUP BY 1
ORDER BY 2 DESC
LIMIT 5;
There is a remaining corner-case: how to break ties for the top 5? Either define criteria (resulting in more ORDER BY expressions), or consider WITH TIES. See:
Get top row(s) with highest value, with ties
Can I do a max(count(*)) in SQL?

SQL: Count rows where column value changed from previous row [duplicate]

This question already has an answer here:
increment row number when value of field changes in Oracle
(1 answer)
Closed 2 years ago.
Suppose I have Oracle or Postgresql database.
ID IdExample OrderByColumn What I want
---------- ---------- ---------- ----------
1 1 1300 1
2 1 2450 1
3 2 5000 2
4 2 4800 2
5 1 5100 3
6 1 6000 3
7 4 7000 4
8 1 8000 5
How do count the changes that are in idExample, data is sorted by OrderByColumn
I need output new column that is represented by "what I want"
pay attention to "1" in IdExample. It repeats but I wants to iterate.
The query should execute quickly with the table having tens of thousands of records.
THANKS
You need to use lag and sum analytical function as follows:
Select t.*,
sum(case when lg is null or lg <> idexample then 1 else 0 end)
over (order by id) as result
from
(Select t.*,
lag(idexample) over (order by id) as lg
From your_table t) 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- 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