473,385 Members | 1,397 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,385 software developers and data experts.

SQL Query to display records that dont match in consecutive rows

6
I have a table with records like:
ID End Price
A 20060625 5.70250000000000000000
A 20060725 5.76500000000000000000
A 20060825 5.70438000000000000000
A 20060925 5.71000000000000000000
A 20061025 5.70000000000000000000
A 20061125 5.70000000000000000000
A 20061225 5.73000000000000000000
A 20070125 5.70000000000000000000

I want to get a query which would give the result as:
ID End Price
A 20060625 5.70250000000000000000
A 20060725 5.76500000000000000000
A 20060825 5.70438000000000000000
A 20060925 5.71000000000000000000
A 20061025 5.70000000000000000000
A 20061225 5.73000000000000000000
A 20070125 5.70000000000000000000

The reason is that the 5th and 6th record had the same price. Then I dont want to display that. So in other words i want to be able to compare the prices between each consecutive rows and return only the rows that dont match. Is it possible to do it in a single query?
Feb 1 '12 #1

✓ answered by Rabbit

You could use the row_number function to number the rows and then join it to itself to get the previous row to see if they match.

3 2282
Rabbit
12,516 Expert Mod 8TB
You could use the row_number function to number the rows and then join it to itself to get the previous row to see if they match.
Feb 2 '12 #2
Ann VK
6
@Rabbit
Thanks you so much for your suggestion. I tried this way and it worked!!

Just in case someone wants to see the query, i created a view for this with query:

with cte_rownum (Ranks,ID,End_Date,Price) as
(select rank() over (order by ID,End_Date) as rank,
ID,End_Date,Price from PriceTable)

select a.* from cte_rownum a inner join cte_rownum b on a.ranks = 1 where a.Price <> b.Price
union
select b.* from cte_rownum a inner join cte_rownum b on a.ranks = b.ranks-1 where a.Price <> b.Price
Feb 2 '12 #3
Rabbit
12,516 Expert Mod 8TB
Not a problem Ann, good luck.
Feb 2 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: yoshitha | last post by:
hi, i want to display records in datagrid. strtestids() is a string array. it contains some testids. based on these ids i've to fill the datagrid. i wrote code like this
0
by: Tony | last post by:
vs 2002 - vb - access 2002 - xp - web forms I want to display records, one at a time. I have about 25 fields to display. I've created SQL and retrieved records. I placed data in 25 separate...
3
by: Kai Zhang | last post by:
I am trying to display some database records in datagrid using dataset. the records need to be displayed are couple of thousands, but the records in database that the SQL query needs to exam are...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
9
by: rochelle28 | last post by:
i want to compare the two consecutive rows in the table such that if the first row contains handset in the category column and the next row contins simcard in category column it will alert the user,...
3
by: emeped | last post by:
I am using C#.NET 2003. Please I want to know if it is possible and how to insert the same data into 2 or more consecutive rows in a datatable column, at the same time, by a single click of a button....
5
by: dorandoran | last post by:
How do I display records using object oriented method? All I have the link to their api help http://www.e-topco.com/oa/oa_help/OpenAir_SOAP.htm and few example. I am want display all the...
4
by: selvamariappan | last post by:
Hi all , how to display various color for rows in datagrid in vb.net 2005, Not a Alternate colors, Thanks and regards selvamariappan.c
8
by: jawad malik | last post by:
I want that each time i execute query it display records in a way that the top record will be positioned last and so on. First time when i executed query it will display like this name A B ...
0
by: Rahul Metha | last post by:
o Select table from drop down list. o Display records in Grid – Display all records belong to table in grid. o Table Fields – This section will have all the fields which are belongs to table.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.