how do i get the rank of a specific row in SQL? - sql-server

I tried to use this query to get the ranks of each vendr by their rating
SELECT vendorid, rating, RANK() over(ORDER BY rating DESC)ranking
FROM vendors
but I want to get the ranking of a specific vendor so I put the where clause like this:
SELECT vendorid, rating, RANK() over(ORDER BY rating DESC)ranking
FROM vendors
WHERE vendorid=1
but it returns a value of 1 in ranking even though it is not rank 1.
how should I fix this?

In this case
SELECT
vendorid, rating,
RANK() OVER (ORDER BY rating DESC) ranking
FROM
vendors
WHERE
vendorid = 1
Rank is calculated after where, so after filtering, SQL Server will assign ranks and show rank for whatever values left
How to fix this?
Use subquery or cte like below.
;With cte as
(
SELECT
vendorid, rating,
RANK() OVER (ORDER BY rating DESC) ranking
FROM
YOURTABLE
)
select *
from cte
where vendorid = 1

Related

SQL Server - How to set Row ID for duplicate or similar content based on insertion first. Select query

Priority is the output column.
The group contains duplicate content.
how can I fix this using SQL query?.
enter image description here
One way to achieve the desired result with this data is to use Dense_rank() function like below:
select *, dense_rank() over (order by [Group]) as Priority
from tab
order by No
For any value, please try the following
;with cte as
(
select [Group], ROW_NUMBER() over (order by No_min) as rn
from
(
select [Group], min([No]) No_min
from tab
group by [Group]
)t
)
select t.*, x.rn as [Priority]
from cte x
join tab t on t.[Group] = x.[Group]
order by 1
Please find the db<>fiddle here.

How to group adjacent row and sum the data in SQL

I would like to sum the Value and group the adjacent row in SQL as shown below. May I know how to do that?
My code now:
Select ID, Value from Table_1
Further question
how about this?
This is a typical gaps and island problem.
As a starter: keep in mind that SQL tables represents unordered set of rows. So for your question to be solved, you need a column that defines the ordering of rows across the table - I assumed ordering_id.
Here is an approach that uses the difference between row_numbers() to build the groups of adjacent rows having the same id:
select
id,
sum(value) value
from (
select
t.*,
row_number() over(order by ordering_id) rn1
row_number() over(partition by id order by ordering_id) rn2
from mytable t
) t
group by id, rn1 - rn2
If you want this on a per user basis:
select
user,
id,
sum(value) value
from (
select
t.*,
row_number() over(partition by user order by ordering_id) rn1
row_number() over(partition by user, id order by ordering_id) rn2
from mytable t
) t
group by user, id, rn1 - rn2

SQL ORDER BY clause ERROR in the ROW_NUMBER

I want to use ROW_NUMBER() function and get first and latest values.
I write bellow query. But I got an error.
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
help me to solve the issue. Below the sql query
SELECT *
FROM(
SELECT OPP_ID,PRJ_ID,
ROW_NUMBER() OVER (PARTITION BY OPP_ID ORDER BY MAX(CREATION_DATE) DESC) AS RN
FROM OPPOR
GROUP BY OPP_ID,PRJ_ID
ORDER BY MAX(CREATION_DATE) DESC) OP
WHERE OP.RN = 1
The row_number function can do it's own aggregation and ordering, so no need to use group by or order by in your subquery (order by won't work in subqueries as you've seen). It is a little unclear if you want to partition by opp_id or opp_id and prj_id though. But this should be what you're looking for:
SELECT *
FROM(
SELECT OPP_ID,PRJ_ID,
ROW_NUMBER() OVER (PARTITION BY OPP_ID ORDER BY CREATION_DATE DESC) AS RN
FROM OPPOR
) OP
WHERE OP.RN = 1

SQL Server loop for changing multiple values of different users

I have the next table:
Supposing these users are sort in descending order based on their inserted date.
Now, what I want to do, is to change their sorting numbers in that way that for each user, the sorting number has to start from 1 up to the number of appearances of each user. The result should look something like:
Can someone provide me some clues of how to do it in sql server ? Thanks.
You can use the ROW_NUMBER ranking function to calculate a row's rank given a partition and order.
In this case, you want to calculate row numbers for each user PARTITION BY User_ID. The desired output shows that ordering by ID is enough ORDER BY ID.
SELECT
Id,
User_ID,
ROW_NUMBER() OVER (PARTITION BY User_ID ORDER BY Id) AS Sort_Number
FROM MyTable
There are other ranking functions you can use, eg RANK, DENSE_RANK to calculate a rank according to a score, or NTILE to calculate percentiles for each row.
You can also use the OVER clause with aggragets to create running totals or moving averages, eg SUM(Id) OVER (PARTITION BY User_ID ORDER BY Id) will create a running total of the Id values for each user.
use ROW_NUMBER() PARTITION BY User_Id
SELECT
Id,
[User_Id],
Sort_Number = ROW_NUMBER() OVER(PARTITION BY [User_Id]
ORDER BY [User_Id],[CreatedDate] DESC)
FROM YourTable
select id
,user_id
,row_number() over(partition by user_id order by user_id) as sort_number
from table
Use ranking function row_number()
select *,
row_number() over (partition by User_id order by user_id, date desc)
from table t

T-SQL Group skip and take

Consider following tables:
How to skip and take groups from the table? Tried using Row_Number() but it doesn't help. Any ideas?
Used query
;WITH cte AS (SELECT Room.Id, Room.RoomName,
ROW_NUMBER() OVER
(ORDER BY Room.Id) AS RN
FROM Room INNER JOIN
RoomDetails ON Room.Id = RoomDetails.RoomId)
SELECT Id, RoomName
FROM cte
WHERE RN = 1
You need to use partition as part of the dense_rank function
dense_rank() over (partition by roomid) as row
see here for some more examples Windowing functions

Resources