473,413 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Remove neighbouring duplicates

Welcome,

how can I alter following table in order to reduce neighbouring
duplicates (symbol, position, quantity, price).

Nr Symbol Position Quantity
Price Date
1. wz9999b 1 1.0
2500.0 2007-05-09 08:09:42.653
2. wz9999b 2 12.0
2500.0 2007-05-09 08:09:42.653
3. wz9999b 1 100.0
2590.0 2007-05-10 15:47:04.140
4. PZ0008VX 1 2280.884 2090.5500000000002 2007-05-16
12:43:12.403
5. PZ0008VX 1 2280.884 2102.0500000000002 2007-05-16
12:45:27.420
6. wz9999b 1 0.001
2500.0 2007-05-18 09:47:16.033
7. wz9999b 1 0.001
2500.0 2007-05-18 09:47:53.270
8. wz9999b 1 1.0
1.0 2007-05-22 12:35:07.893
9. PZ0008VX 1 2280.884 2102.0500000000002 2007-05-24
09:38:26.160
10. PZ0008VX 1 2280.884 2102.0500000000002 2007-05-24
09:38:38.800
11. wz9999b 1 0.001 2500.0
2007-05-24 12:35:07.207
12 wz9999b 1 0.002 2500.0
2007-05-24 12:35:14.987
13. wz9999b 1 0.001 2500.0
2007-05-24 12:38:07.207

In the result set I would like to get the rows number 6 and 10.

Any suggestions??

May 24 '07 #1
2 1721
mGracz wrote:
how can I alter following table in order to reduce neighbouring
duplicates (symbol, position, quantity, price).

Nr Symbol Position Quantity
Price Date
1. wz9999b 1 1.0
2500.0 2007-05-09 08:09:42.653
2. wz9999b 2 12.0
2500.0 2007-05-09 08:09:42.653
3. wz9999b 1 100.0
2590.0 2007-05-10 15:47:04.140
4. PZ0008VX 1 2280.884 2090.5500000000002 2007-05-16
12:43:12.403
5. PZ0008VX 1 2280.884 2102.0500000000002 2007-05-16
12:45:27.420
6. wz9999b 1 0.001
2500.0 2007-05-18 09:47:16.033
7. wz9999b 1 0.001
2500.0 2007-05-18 09:47:53.270
8. wz9999b 1 1.0
1.0 2007-05-22 12:35:07.893
9. PZ0008VX 1 2280.884 2102.0500000000002 2007-05-24
09:38:26.160
10. PZ0008VX 1 2280.884 2102.0500000000002 2007-05-24
09:38:38.800
11. wz9999b 1 0.001 2500.0
2007-05-24 12:35:07.207
12 wz9999b 1 0.002 2500.0
2007-05-24 12:35:14.987
13. wz9999b 1 0.001 2500.0
2007-05-24 12:38:07.207

In the result set I would like to get the rows number 6 and 10.
Assuming that row numbers are not stored in the table, but indicate
sorting on (Date, Position):

select a.Symbol, a.Position, a.Quantity, a.Price
from the_table a
join the_table b on a.Symbol = b.Symbol
and a.Position = b.Position
and a.Quantity = b.Quantity
and a.Price = b.Price
and a.Date < b.Date
left join the_table c on a.Date < c.Date
and c.Date < b.Date
where c.Date is null

But why do you require Position to match? Suppose you had this:

Symbol | Position | Quantity | Price | Date
--------+----------+----------+--------+------------------------
wz9999b | 1 | 1.0 | 2500.0 | 2007-05-09 08:09:42.653
wz9999b | 2 | 12.0 | 2500.0 | 2007-05-09 08:09:42.653
wz9999b | 1 | 12.0 | 2500.0 | 2007-05-10 15:47:04.140
wz99995 | 2 | 100.0 | 2590.0 | 2007-05-10 15:47:04.140

would the two rows with Quantity = 12.0 count as duplicates? Why
or why not?
May 24 '07 #2
mGracz (M.************@gmail.com) 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, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wm | last post by:
I have a table of users in mySQL that appears to have a lot of duplicates. What's the best way to look at the userID and email and delete the duplicates? Thanx, Wm
3
by: m|sf|t | last post by:
All, Is it possible to use PHP to open/read a TXT file (i.e. IP.TXT) that contains ip addresses (1 per line), remove any duplicates found, and re-write the file back out to IP_NEW.TXT ? The...
6
by: Ragnorack67 | last post by:
Hello again, I have one further question about improving a validation. Currently this will validate a string of words separated by commas, and if it's greater than 9 will do something, or something...
2
by: CV | last post by:
How can I match 'n' number of neighbouring words of a pattern using regular expressions? For example, suppose I am looking for the pattern "length xyz cm" in some text. where xyz is a number -...
7
by: Voetleuce en fênsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have,...
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
14
by: BarrySDCA | last post by:
I have a database being populated by hits to a program on a server. The problem is each client connection may require a few hits in a 1-2 second time frame. This is resulting in multiple database...
2
by: shapper | last post by:
Hello, I have an Enum and a Generic.List(Of Enum) 1 Public Enum Mode 2 Count 3 Day 4 Month 5 End Enum
8
natalie99
by: natalie99 | last post by:
hi everyone i have reseached this topic and cannot seem to find a solution that suits my problem, please help!! I have two tables, "Inventory" which contains 30,000 or so records, with about...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.