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

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.

Related

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

How can I get only last run of each SSIS job using T-SQL query?

How can I get only the last run of each SSIS job using a T-SQL query?
Please check the image. These details are stored in a single Table XYZ. Can anyone help me by providing a T-SQL query?
I am using this query:
select Job_Name, Start_Date, Finish_Time, Remarks
from #XYZ
order by Job_Name
You can use row_number()
select x.*
from (select x.*,
row_number() over (partition by job_name order by try_convert(datetime, Finish_Time) desc) as seq
from #XYZ x
) x
where x.seq = 1;
You can do this by adding a row_number to sort the data and then filtering to where a 1 is returned:
select cols
from(select cols
,row_number() over (partition by Job_Name order by Start_Date desc) as rn
from table
) as t
where rn = 1

SQL Server window function implementation issue

I have a table structure like below:
I have the following query to get the unique result from the table:
WITH Dupes AS
(
SELECT
ID, Template_ID, Address, Job_Number, Other_Info,
Assigned_By, Assignees, Active, seen,
ROW_NUMBER() OVER (PARTITION BY Template_ID,Job_Number ORDER BY ID) AS RowNum
FROM
Schedule
WHERE
Assignees IN ('9', '16', '22')
)
SELECT
ID, Template_ID, Job_Number, Address, Other_Info,
Assigned_By, Assignees, Active, seen
FROM
Dupes
WHERE
RowNum = 1
Output of the above query is:
If the Job_Number and Template_ID are same, only return one row(first row using ID). That is why I did use ROW_NUMBER() OVER(PARTITION BY Template_ID,Job_Number ORDER BY ID) AS RowNum. I am not sure how to fix this as I rarely used this function.
I need to get the output like below:
Updated Code
Tried the code below:
seems your trying to group by Job_Number, remove Template_ID on your partition by clause
WITH Dupes AS
(
SELECT ID,Template_ID,Address,Job_Number,Other_Info,Assigned_By,Assignees,Active,seen,
ROW_NUMBER() OVER(PARTITION BY rtrim(ltrim(Job_Number)) ORDER BY ID) AS RowNum
FROM Schedule
WHERE Assignees IN('9','16','22')
)
SELECT ID,Template_ID,Job_Number,Address,Other_Info,Assigned_By,Assignees, Active,seen FROM Dupes WHERE RowNum=1

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

cannot use alias in ROW_NUMBER() over in SQL Server?

I have to create a row_number column ordered by a grouped sum, when using sql:
select Sales.Name, SUM(Sales.Bill) as billsum, ROW_NUMBER() over (order by billsum DESC) as rn
from Sales group by Sales.Name
It reports error because row_number over cannot parse the "billsum" alias, I have to write:
select Sales.Name, SUM(Sales.Bill) as billsum, ROW_NUMBER() over (order by SUM(Sales.Bill) DESC) as rn
from Sales group by Sales.Name
so here I write SUM(Sales.Bill) twice, is there anyway to use the alias here?
The MSDN docs for the T-SQL OVER clause say:
value_expression cannot refer to expressions or aliases in the select list.
As already stated out by other member you either have to use CTE or SubQuery.
Not only Row_Number() function but in tsql you can not reference alias in same query, so either you have to use one of the mentioned way above or the expression you used in your post. I hope it makes sense!! :)
Possible work-arounds are to use CTE or a subquery:
SELECT Name, billsum, ROW_NUMBER() OVER (ORDER BY billsum DESC) AS rn
FROM
( SELECT Sales.Name, SUM(Sales.Bill) AS billsum
FROM Sales
GROUP BY Sales.Name
) tmp
-- Reorder after cutting out qty = 0.
SELECT *,ROW_NUMBER() OVER (partition by claimno ORDER BY itemno) as 'alias name'
from dbo.OrderCol
where QTY <> 0

Resources