I have used ranked function to find and remove duplicate rows in my procedures but i have found another option that may be more feasible with my queries. Sql server introduce Ties function that return one value from a column that has duplicate values as we can see in following example
Declare @Table table(id int ,brand varchar(5),price money)
insert into @Table
values (1,'GE',20),(2,'GE',21),(3,'Sony',21)
;with dup
as
(
select rank() over(partition by brand order by price) as dup,* from @Table
)
select * from dup where dup=1
SELECT Top(1) With Ties *
FROM @Table
WHERE price between 19 and 21
Order By ROW_NUMBER() OVER (PARTITION By Brand ORDER BY price)
results for both queries are same but this is good use of Ties function in queries.
No comments:
Post a Comment