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

selecting changed rows only

Hi experts,

I have been trying to limit the table rows in the following situation,
any suggestions will be appreciated.

we have table called tempTb has columns id, c_id, c_name, rating, date
columns.
id is an identity column.
date is a datetime column, the rest are varchar datatype.
Here is the table structure with sample data,

id c_id c_name rating date
1 ao amer onli 1 1/1/2002
2 ao amer onli 1 3/1/2002
3 ao amer onli 1 6/1/2002
4 ao amer onli 3 9/1/2002
5 ao amer onli 3 12/1/2002
6 ao amer onli 3 3/1/2003
7 ao amer onli 3 6/1/2003
8 ao amer onli 3 9/1/2003
9 ao amer onli 2 12/1/2003
10 ao amer onli 1 6/1/2004
11 ao amer onli 1 12/1/2004
12 xy xabs yasd 1 1/1/2002
13 xy xabs yasd 2 3/1/2002
14 xy xabs yasd 2 6/1/2002
15 xy xabs yasd 2 9/1/2002
16 xy xabs yasd 1 12/1/2002
17 xy xabs yasd 1 3/1/2003
18 xy xabs yasd 3 6/1/2003
19 xy xabs yasd 3 9/1/2003
20 xy xabs yasd 2 12/1/2003
21 xy xabs yasd 1 6/1/2004
22 xy xabs yasd 1 12/1/2004
From this table I need to select the rows with rating changes only,

i.e if two or three consecutive rows have same rating only the first
row should be selected.

the selection should look like...
id c_id c_name rating date
1 ao amer onli 1 1/1/2002
4 ao amer onli 3 9/1/2002
9 ao amer onli 2 12/1/2003
10 ao amer onli 1 6/1/2004
12 xy xabs yasd 1 1/1/2002
13 xy xabs yasd 2 3/1/2002
16 xy xabs yasd 1 12/1/2002
18 xy xabs yasd 3 6/1/2003
20 xy xabs yasd 2 12/1/2003
21 xy xabs yasd 1 6/1/2004

I was trying to do this by self-joining the table like....

select t1.* from tempTb t1, tempTb t2
where t1.id!=t2.id,
t1.c_id=t2.c_id,
t1.c_name=t2.c_name,
t1.rating!=t2.rating.
But this is generating cartesian products,
I have tried some other combinations after where clause with date colmn
wtc,
but none seems to give the required result.

so if anybody can guide me in the right direction I would appreciate
it.

Thanks alot,
Remote

Jul 23 '05 #1
4 1589
Off the top of my head, I would conjecture

select min (id), c_id, c_name, rating, min (rating_date)
from tempTb
group by c_id, c_name, rating

By "first" I assume that you mean "lowest id value" - else Joe Celko will
point out the error of your terminology. You may also need "select
distinct" instead of "select". Upon further reflection, the above will
include rows without a rating change. So ...

select distinct min (T1.id), T1.c_id, T1.c_name, T1.rating, min
(T1.rating_date)
from tempTb T1
where exists (select T2.id
from tempTb T2
where T2.c_id = T1.c_id
and T2.rating <> T1.rating)
group by T1.c_id, T1.c_name, T1.rating
having exists (select T2.id
from tempTb T2
where T2.c_id = T1.c_id
and T2.rating <> T1.rating)

I'm not sure as to whether "having exists" is valid or not. "distinct" is
definitely needed with this SQL.

<re******@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hi experts,

I have been trying to limit the table rows in the following situation,
any suggestions will be appreciated.

we have table called tempTb has columns id, c_id, c_name, rating, date
columns.
id is an identity column.
date is a datetime column, the rest are varchar datatype.
Here is the table structure with sample data,

id c_id c_name rating date
1 ao amer onli 1 1/1/2002
2 ao amer onli 1 3/1/2002
3 ao amer onli 1 6/1/2002
4 ao amer onli 3 9/1/2002
5 ao amer onli 3 12/1/2002
6 ao amer onli 3 3/1/2003
7 ao amer onli 3 6/1/2003
8 ao amer onli 3 9/1/2003
9 ao amer onli 2 12/1/2003
10 ao amer onli 1 6/1/2004
11 ao amer onli 1 12/1/2004
12 xy xabs yasd 1 1/1/2002
13 xy xabs yasd 2 3/1/2002
14 xy xabs yasd 2 6/1/2002
15 xy xabs yasd 2 9/1/2002
16 xy xabs yasd 1 12/1/2002
17 xy xabs yasd 1 3/1/2003
18 xy xabs yasd 3 6/1/2003
19 xy xabs yasd 3 9/1/2003
20 xy xabs yasd 2 12/1/2003
21 xy xabs yasd 1 6/1/2004
22 xy xabs yasd 1 12/1/2004
From this table I need to select the rows with rating changes only,

i.e if two or three consecutive rows have same rating only the first
row should be selected.

the selection should look like...
id c_id c_name rating date
1 ao amer onli 1 1/1/2002
4 ao amer onli 3 9/1/2002
9 ao amer onli 2 12/1/2003
10 ao amer onli 1 6/1/2004
12 xy xabs yasd 1 1/1/2002
13 xy xabs yasd 2 3/1/2002
16 xy xabs yasd 1 12/1/2002
18 xy xabs yasd 3 6/1/2003
20 xy xabs yasd 2 12/1/2003
21 xy xabs yasd 1 6/1/2004

I was trying to do this by self-joining the table like....

select t1.* from tempTb t1, tempTb t2
where t1.id!=t2.id,
t1.c_id=t2.c_id,
t1.c_name=t2.c_name,
t1.rating!=t2.rating.
But this is generating cartesian products,
I have tried some other combinations after where clause with date colmn
wtc,
but none seems to give the required result.

so if anybody can guide me in the right direction I would appreciate
it.

Thanks alot,
Remote

Jul 23 '05 #2
>>From this table I need to select the rows with rating changes only,
i.e if two or three consecutive rows have same rating only the first
row should be selected.
From your brief it looks like you want rows where there is no matching row
preceeding it.

SELECT
id, c_id, c_name, rating, date
FROM tempTb out
WHERE NOT EXISTS(SELECT * FROM tempTb pre WHERE pre.id=(out.id-1) AND
pre.c_id=out.c_id AND pre.c_name=out.c_name AND pre.rating=out.rating)

there should be an index(preferably clustered) on the id column for best
performance.

Mr Tea
http://mr-tea.blogspot.com

<re******@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com... Hi experts,

I have been trying to limit the table rows in the following situation,
any suggestions will be appreciated.

we have table called tempTb has columns id, c_id, c_name, rating, date
columns.
id is an identity column.
date is a datetime column, the rest are varchar datatype.
Here is the table structure with sample data,

id c_id c_name rating date
1 ao amer onli 1 1/1/2002
2 ao amer onli 1 3/1/2002
3 ao amer onli 1 6/1/2002
4 ao amer onli 3 9/1/2002
5 ao amer onli 3 12/1/2002
6 ao amer onli 3 3/1/2003
7 ao amer onli 3 6/1/2003
8 ao amer onli 3 9/1/2003
9 ao amer onli 2 12/1/2003
10 ao amer onli 1 6/1/2004
11 ao amer onli 1 12/1/2004
12 xy xabs yasd 1 1/1/2002
13 xy xabs yasd 2 3/1/2002
14 xy xabs yasd 2 6/1/2002
15 xy xabs yasd 2 9/1/2002
16 xy xabs yasd 1 12/1/2002
17 xy xabs yasd 1 3/1/2003
18 xy xabs yasd 3 6/1/2003
19 xy xabs yasd 3 9/1/2003
20 xy xabs yasd 2 12/1/2003
21 xy xabs yasd 1 6/1/2004
22 xy xabs yasd 1 12/1/2004
From this table I need to select the rows with rating changes only,

i.e if two or three consecutive rows have same rating only the first
row should be selected.

the selection should look like...
id c_id c_name rating date
1 ao amer onli 1 1/1/2002
4 ao amer onli 3 9/1/2002
9 ao amer onli 2 12/1/2003
10 ao amer onli 1 6/1/2004
12 xy xabs yasd 1 1/1/2002
13 xy xabs yasd 2 3/1/2002
16 xy xabs yasd 1 12/1/2002
18 xy xabs yasd 3 6/1/2003
20 xy xabs yasd 2 12/1/2003
21 xy xabs yasd 1 6/1/2004

I was trying to do this by self-joining the table like....

select t1.* from tempTb t1, tempTb t2
where t1.id!=t2.id,
t1.c_id=t2.c_id,
t1.c_name=t2.c_name,
t1.rating!=t2.rating.
But this is generating cartesian products,
I have tried some other combinations after where clause with date colmn
wtc,
but none seems to give the required result.

so if anybody can guide me in the right direction I would appreciate
it.

Thanks alot,
Remote

Jul 23 '05 #3
Thank you very much Mr Tea, I appreciate your help.
I was looking for something that can compare the present row data with
its neighbors
like pre.id=out.id-1
you got the bullseye right away.

Thanks for trying Carl,
I tried the query you have suggested.
Its eliminating the duplicate records but it doesn't detect the change
as I needed.
just for the record, it gives out the resuts like....

id c_id c_name rating ratingdate
1 ao amer onli 1 2002-01-01
9 ao amer onli 2 2003-12-01
4 ao amer onli 3 2002-09-01
12 xy xabs yasd 1 2002-01-01
13 xy xabs yasd 2 2002-03-01
18 xy xabs yasd 3 2003-06-01

Jul 23 '05 #4
Hi All,

Here is a more generalized approach ... you assume that id is
sequential (it may not be as identity columns can contain gaps) ... I
would also worry about the ordering of the data as well (I agree with
Mr Tea on the index, for sure) ... the query works with gaps ... let me
know if you see a flaw ...

Good Luck ...

Sara (DBAGAL 4 ever!!)

....

SELECT
id, c_id, c_name, rating, date1
FROM test1 t1
WHERE NOT EXISTS (SELECT 1
FROM test1 t2
WHERE t2.id=(select max(t3.id)
from test1 t3
where t2.c_id=t1.c_id
AND t2.c_name=t1.c_name
AND t2.rating=t1.rating
and t3.id < t1.id)
and t2.c_name = t1.c_name
and t2.rating = t1.rating)

Jul 23 '05 #5

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

Similar topics

1
by: Ramesh | last post by:
hi, I am selecting fields from three table for manupulating data and i want to display total number of records selected. But i am always getting -1 value, eventhough 1000 of records are selected....
6
by: aaj | last post by:
Hi all I use a data adapter to read numerous tables in to a dataset. The dataset holds tables which in turn holds full details of the records i.e. keys, extra colums etc.. In some cases I...
1
by: Jay | last post by:
Hi All, My users are complaining about the page refreshing when they are selecting multiple rows in a datagrid. Has anyone tried to manage this using javascript? I tried smartnavigation but that...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
8
by: Kari Lavikka | last post by:
Hi! I have to select a random row from a table where primary key isn't continuous (some rows have been deleted). Postgres just seems to do something strange with my method. -- -- Use the...
1
by: Andy | last post by:
Hi Gang I have a simple single table that has many duplicate rows in them. The distinctrows only works on more than one table. Is there a quick and easy way to select only the distict rows...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
5
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset....
3
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
In Visual Studio 2005, I have my DataSet filled with several static tables (they might get changed weekly, but that's about it). With one of these tables in the DataSet, is it possible to select...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.