SQL Server window function implementation issue - sql-server

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

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

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

SQL Query Distinct two columns, Max(date) and retrieve ID

I'm having trouble figuring out how to make this query work. I've tried everything under the sun to avoid looping.
The table has ID (pk), UserID, BookID, BookDate (datetime), and SellerID. There are duplicate combinatins of UserID and BookID.
I am trying to retrieve distinct records by UserID and BookID that have the most recent BookDate. That's easy enough (below), but I also need to retrieve the ID and SellerID columns for the returned record. That's where I'm having trouble...
Select Distinct
UserID, CourseID, MAX(AssignedON)
From
AssignmentS
Group By
UserID, CourseID
Every time I add a join I get all records. I've tried rowover, exists and nothing seems to work. Any help would be greatly appreciated!
select userid,courseid,bookdate,sellerid from
(select userid,courseid,bookdate,sellerid,
row_number() over (partition by userid,courseid
order by bookdate desc) as RNUM
from yourtable where yourwhere)
where rnum = 1;
[This][1]
[1]: http://coding.feron.it/2012/08/mssql-having-maxid-id-problem-row_number-partition/ blog post describes in detail how to do this with multiple tables
Figured it out. I just had to move things around a bit and this is working perfectly!
select userid,courseid,bookdate,sellerid from (
select * row_number() over(partition by userid,courseid, order by bookdate desc) as RNUM
from yourtable where yourwhere)
where rnum = 1;

Use percentile_cont with a "group by" statment in T-SQL

I'd like to use the percentile_cont function to get median values in T-SQL. However, I also need to get mean values as well. I'd like to do something like the following:
SELECT CustomerID ,
AVG(Expenditure) AS MeanSpend , percentile_cont
( .5) WITHIN GROUP(ORDER BY Expenditure) OVER( ) AS MedianSpend
FROM Customers
GROUP BY CustomerID
Can this be accomplished? I know I can use the OVER clause to group the percentile_cont results...
but then I'm stuck using two queries, am I not?
Just figured it out... gotta drop the group by and give both aggregation functions a over statement.
SELECT CustomerID,
AVG(Expenditure) OVER(PARTITION BY CustomerID) AS MeanSpend,
percentile_cont(.5) WITHIN GROUP(ORDER BY Expenditure) OVER(PARTITION BY CustomerID) AS MedianSpend
FROM Customers
You can't use "group by" with window functions. These functions return the aggregated values for every row. One way is to use "select distinct" to get rid of the duplicate rows. Just make sure you partition each window function by the non-aggregated columns (groupId in this example).
--Generate test data
SELECT TOP(10)
value.number%3 AS groupId
, value.number AS number
INTO #data
FROM master.dbo.spt_values AS value
WHERE value."type" = 'P'
ORDER BY NEWID()
;
--View test data
SELECT * FROM #data ORDER BY groupId,number;
--CALCULATE MEDIAN
SELECT DISTINCT
groupId
, AVG(number) OVER(PARTITION BY groupId) AS mean
, percentile_cont(.5) WITHIN GROUP(ORDER BY number) OVER(PARTITION BY groupId) AS median
FROM #data
;
--Clean up
DROP TABLE #data;

Resources