Re: Remove neighbouring duplicates



mGracz (M.Graczykowski@xxxxxxxxx) writes:
Welcome,

how can I alter following table in order to reduce neighbouring
duplicates (symbol, position, quantity, price).
...
In the result set I would like to get the rows number 6 and 10.

Any suggestions??

Since you did not say which version of SQL Server you are using, I
will assume SQL 2005, because the query is a lot easier to write
on SQL 2005. And performance will be a lot better.

WITH numbered_items (rownum, symbol, position, qty, price, date)
SELECT rownum = row_number() OVER (
PARTITION BY Symbol, Position, Quantity, Price
ORDER BY Date),
Symbol, Position, Quantity, Date
FROM tbl
)
SELECT a.symbol, a.position, a.qty, a.date
FROM numbered_items a
JOIN numbered_items b ON a.symbol = b.symbol
AND a.position = b.positon
AND a.qty = b.qty
AND a.price = b.price
AND a.rownum = b.rownum - 1


--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: SQL Problem
    ... when_expression is any valid SQL Server expression. ... > price AS Price ... > Modern Cooking Silicon Valley Gastronomi 19.99 ... > Coffee Table Title Cooking with Compute ...
    (microsoft.public.dotnet.general)
  • Re: Getting @@RowCount when using ROW_NUMBER()
    ... The methods suggested would result in running the query twice. ... ROW_NUMBEROVER AS rownum ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (microsoft.public.sqlserver.programming)
  • Re: Selecting Lowest Value from a Table
    ... I also meant to ask you earlier where in this query would I put the INTO ... "Tom Moreau" wrote: ... > Columnist, SQL Server Professional ... > the ManPartNum and Price Fields are repeated in the returned records. ...
    (microsoft.public.sqlserver.server)
  • Re: SQL Problem
    ... when_expression is any valid SQL Server expression. ... > price AS Price ... > Modern Cooking Silicon Valley Gastronomi 19.99 ... > Coffee Table Title Cooking with Compute ...
    (microsoft.public.dotnet.general)
  • Re: Query training -- Complex queries
    ... > Update PositionsEOM ... Here you don't have a derived table, but a correlated subquery. ... UPDATE in Books Online. ... is not written for SQL Server. ...
    (microsoft.public.sqlserver.server)