How to group adjacent row and sum the data in SQL - sql-server

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

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.

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

Determining Percentile Difference from lag, lead and current row using windows function?

Is there a better way to get the moving percentile using a windows function without utilizing a CTE or derived table etc? I wanted to fit it all in one query utilizing windows functions, but im having a hell of a time converting it to a percentile value. The only resolution I could think of was to create the numeric values and then do the math with the table. Just would be cool if there was a more streamlined way to do this?
WITH numberdata AS
(
SELECT
custid
,orderid
,LAG(VAL) OVER(PARTITION BY CUSTID ORDER BY ORDERID DESC) as lagval
,LEAD(VAL) OVER(PARTITION BY CUSTID ORDER BY ORDERID DESC) as leadval
,val - LAG(VAL) OVER(PARTITION BY CUSTID ORDER BY ORDERID DESC) as lagvaldiff
,val - LEAD(VAL) OVER(PARTITION BY CUSTID ORDER BY ORDERID DESC) as leadvaldiff
,val
FROM sales.ordervalues
)
select
CAST((lagval)/val AS NUMERIC(10,2)) as lagpctdiff
,CAST((leadval)/val AS NUMERIC(10,2)) as leadpctdiff
,CAST((lagvaldiff)/leadvaldiff AS NUMERIC(10,2)) as pctdiff
,val
,lagval
from numberdata
order by custid desc
This is just me studying to learn more about the code in preparation of a test. Data comes from the sales.ordervalues table located in training db TSQL_2012.
How can I convert the leadvaldiff and lagvaldiff columns to a percentage without placing it within a CTE?
dataset
Am i missing something or is this what you are looking for?
SELECT LAG(VAL) OVER(PARTITION BY CUSTID ORDER BY ORDERID DESC)/val as lagval
,LEAD(VAL) OVER(PARTITION BY CUSTID ORDER BY ORDERID DESC)/val as leadval
,(val - LAG(VAL) OVER(PARTITION BY CUSTID ORDER BY ORDERID DESC))/
(val - LEAD(VAL) OVER(PARTITION BY CUSTID ORDER BY ORDERID DESC)) as pctdiff
FROM sales.OrderValues;

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

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

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