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

Help with query

Thanks in advance for your help.
Given the following table:

CREATE TABLE T1 (
seq INTEGER NOT NULL,
value CHAR(1) NOT NULL)

INSERT INTO TABLE T1 VALUES (1, 'A')
INSERT INTO TABLE T1 VALUES (2, 'A')
INSERT INTO TABLE T1 VALUES (3, 'A')
INSERT INTO TABLE T1 VALUES (5, 'B')
INSERT INTO TABLE T1 VALUES (6, 'B')
INSERT INTO TABLE T1 VALUES (7, 'A')

I would like to write a query that would return the beginning and
ending seq for each value. The desired results for the above data
would be as follows:

Beg End Value
1 3 A
5 6 B
7 7 A

I'm hesitant to post the queries I have been trying, because I don't
want to taint someone's thinking, but I have been approaching this with
the idea that a self join will be part of the solution. Simply
grouping by value using max and min functions will not return the
desired results.

Thanks again for your help.

Jun 13 '06 #1
13 1732
Fred wrote:
Thanks in advance for your help.
Given the following table:

CREATE TABLE T1 (
seq INTEGER NOT NULL,
value CHAR(1) NOT NULL)

INSERT INTO TABLE T1 VALUES (1, 'A')
INSERT INTO TABLE T1 VALUES (2, 'A')
INSERT INTO TABLE T1 VALUES (3, 'A')
INSERT INTO TABLE T1 VALUES (5, 'B')
INSERT INTO TABLE T1 VALUES (6, 'B')
INSERT INTO TABLE T1 VALUES (7, 'A')

I would like to write a query that would return the beginning and
ending seq for each value. The desired results for the above data
would be as follows:

Beg End Value
1 3 A
5 6 B
7 7 A

I'm hesitant to post the queries I have been trying, because I don't
want to taint someone's thinking, but I have been approaching this with
the idea that a self join will be part of the solution. Simply
grouping by value using max and min functions will not return the
desired results.


This will return the lower boundary of a sequence:

SELECT seq, value
FROM t1 AS o
WHERE NOT EXISTS ( SELECT 1
FROM t1 AS i1
WHERE i1.seq = t1.seq - 1 AND
i1.value = t1.value )

Now you just extend this to add the upper boundary:

SELECT seq AS begin,
( SELECT MIN(seq)
FROM t1 AS i2
WHERE i2.value = t1.value AND
i2.seq >= t1.value AND
-- no successor
NOT EXISTS ( SELECT 1
FROM t1 AS i3
WHERE i3.seq = i2.seq + 1 AND
i3.value = t2.value ) ) AS end,
value
FROM t1 AS o
WHERE NOT EXISTS ( SELECT 1
FROM t1 AS i1
WHERE i1.seq = t1.seq - 1 AND
i1.value = t1.value )
This is untested and you can probably simplify this...

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Jun 13 '06 #2
Fred wrote:
Thanks in advance for your help.
Given the following table:

CREATE TABLE T1 (
seq INTEGER NOT NULL,
value CHAR(1) NOT NULL)

INSERT INTO TABLE T1 VALUES (1, 'A')
INSERT INTO TABLE T1 VALUES (2, 'A')
INSERT INTO TABLE T1 VALUES (3, 'A')
INSERT INTO TABLE T1 VALUES (5, 'B')
INSERT INTO TABLE T1 VALUES (6, 'B')
INSERT INTO TABLE T1 VALUES (7, 'A')

I would like to write a query that would return the beginning and
ending seq for each value. The desired results for the above data
would be as follows:

Beg End Value
1 3 A
5 6 B
7 7 A
How about:

select min(seq) as beg, max(seq) as end, value
from t1
group by 3;

Art S. Kagel

I'm hesitant to post the queries I have been trying, because I don't
want to taint someone's thinking, but I have been approaching this with
the idea that a self join will be part of the solution. Simply
grouping by value using max and min functions will not return the
desired results.

Thanks again for your help.

Jun 13 '06 #3
Am I missing something? Why wouldn't the "expected results" be:
Beg End Value
1 7 A
5 6 B
?

Are you looking for multiple contiguous ranges per "CHAR"?
If INSERT INTO TABLE T1 VALUES (2, 'A') were not present, would you
expect:
1 1 A
3 3 A
5 6 B
7 7 A
?

Or should the last line have actually been "C"?

"Fred" <fr**********@yahoo.com> wrote in message
news:11**********************@h76g2000cwa.googlegr oups.com...
Thanks in advance for your help.
Given the following table:

CREATE TABLE T1 (
seq INTEGER NOT NULL,
value CHAR(1) NOT NULL)

INSERT INTO TABLE T1 VALUES (1, 'A')
INSERT INTO TABLE T1 VALUES (2, 'A')
INSERT INTO TABLE T1 VALUES (3, 'A')
INSERT INTO TABLE T1 VALUES (5, 'B')
INSERT INTO TABLE T1 VALUES (6, 'B')
INSERT INTO TABLE T1 VALUES (7, 'A')

I would like to write a query that would return the beginning and
ending seq for each value. The desired results for the above data
would be as follows:

Beg End Value
1 3 A
5 6 B
7 7 A

I'm hesitant to post the queries I have been trying, because I don't
want to taint someone's thinking, but I have been approaching this with
the idea that a self join will be part of the solution. Simply
grouping by value using max and min functions will not return the
desired results.

Thanks again for your help.

Jun 14 '06 #4
Hello.
---
with t1(i, c) as
( values
(1, 'A')
,(2, 'A')
,(3, 'A')
,(5, 'B')
,(6, 'B')
,(7, 'A')
),
R (B, E, C) AS
(
SELECT I AS B, I AS E, C
FROM T1 A
WHERE NOT EXISTS
(
SELECT 1
FROM T1 B
WHERE A.C=B.C AND B.I=A.I-1
)
UNION ALL
SELECT R.B, T1.I, R.C
FROM T1, R
WHERE T1.C=R.C AND T1.I=R.E+1
)
SELECT B, MAX(E) E, C
FROM R
GROUP BY C, B;
---

Sincerely,
Mark B.

CREATE TABLE T1 (
seq INTEGER NOT NULL,
value CHAR(1) NOT NULL)

INSERT INTO TABLE T1 VALUES (1, 'A')
INSERT INTO TABLE T1 VALUES (2, 'A')
INSERT INTO TABLE T1 VALUES (3, 'A')
INSERT INTO TABLE T1 VALUES (5, 'B')
INSERT INTO TABLE T1 VALUES (6, 'B')
INSERT INTO TABLE T1 VALUES (7, 'A')

I would like to write a query that would return the beginning and
ending seq for each value. The desired results for the above data
would be as follows:

Beg End Value
1 3 A
5 6 B
7 7 A

I'm hesitant to post the queries I have been trying, because I don't
want to taint someone's thinking, but I have been approaching this with
the idea that a self join will be part of the solution. Simply
grouping by value using max and min functions will not return the
desired results.

Thanks again for your help.


Jun 14 '06 #5
Art S. Kagel wrote:
Fred wrote:
Thanks in advance for your help.
Given the following table:

CREATE TABLE T1 (
seq INTEGER NOT NULL,
value CHAR(1) NOT NULL)

INSERT INTO TABLE T1 VALUES (1, 'A')
INSERT INTO TABLE T1 VALUES (2, 'A')
INSERT INTO TABLE T1 VALUES (3, 'A')
INSERT INTO TABLE T1 VALUES (5, 'B')
INSERT INTO TABLE T1 VALUES (6, 'B')
INSERT INTO TABLE T1 VALUES (7, 'A')

I would like to write a query that would return the beginning and
ending seq for each value. The desired results for the above data
would be as follows:

Beg End Value
1 3 A
5 6 B
7 7 A


How about:

select min(seq) as beg, max(seq) as end, value
from t1
group by 3;


This won't work because you don't detect the gap in the sequence for the
group A.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Jun 14 '06 #6
Hi, Fred.

How about a solution like this:

WITH T(seq, val, grp) AS(
SELECT L.seq, L.value, L.seq - COUNT(*)
FROM t1 AS L
INNER JOIN L AS R
ON R.value = L.value
AND R.seq <= L.seq
GROUP BY L.value, L.seq)
SELECT MIN(seq) AS beg, MAX(seq) AS end, val
FROM T
GROUP BY val, grp

---
Andrey Odegov
av******@yandex.ru
(remove GOV to respond)

Jun 14 '06 #7
sorry, i did a mistake in the query

it must be the following instead:

WITH T(seq, val, grp) AS(
SELECT L.seq, L.value, L.seq - COUNT(*)
FROM t1 AS L
INNER JOIN t1 AS R
ON R.value = L.value
AND R.seq <= L.seq
GROUP BY L.value, L.seq)
SELECT MIN(seq) AS beg, MAX(seq) AS end, val
FROM T
GROUP BY val, grp

---
Andrey Odegov
av******@yandex.ru
(remove GOV to respond)

Jun 14 '06 #8
I am not trying to find the max and min seq for a given value. I'm
trying to show the upper and lower limits of a range for a given value.
Your latter example is correct, if (2,'A') were omitted, the results
would be as you have it.
William Bub wrote:
Am I missing something? Why wouldn't the "expected results" be:
Beg End Value
1 7 A
5 6 B
?

Are you looking for multiple contiguous ranges per "CHAR"?
If INSERT INTO TABLE T1 VALUES (2, 'A') were not present, would you
expect:
1 1 A
3 3 A
5 6 B
7 7 A
?

Or should the last line have actually been "C"?

"Fred" <fr**********@yahoo.com> wrote in message
news:11**********************@h76g2000cwa.googlegr oups.com...
Thanks in advance for your help.
Given the following table:

CREATE TABLE T1 (
seq INTEGER NOT NULL,
value CHAR(1) NOT NULL)

INSERT INTO TABLE T1 VALUES (1, 'A')
INSERT INTO TABLE T1 VALUES (2, 'A')
INSERT INTO TABLE T1 VALUES (3, 'A')
INSERT INTO TABLE T1 VALUES (5, 'B')
INSERT INTO TABLE T1 VALUES (6, 'B')
INSERT INTO TABLE T1 VALUES (7, 'A')

I would like to write a query that would return the beginning and
ending seq for each value. The desired results for the above data
would be as follows:

Beg End Value
1 3 A
5 6 B
7 7 A

I'm hesitant to post the queries I have been trying, because I don't
want to taint someone's thinking, but I have been approaching this with
the idea that a self join will be part of the solution. Simply
grouping by value using max and min functions will not return the
desired results.

Thanks again for your help.


Jun 14 '06 #9
Thanks to all. Andrey's solution is very concise, quick and accurate.

Andrey Odegov wrote:
sorry, i did a mistake in the query

it must be the following instead:

WITH T(seq, val, grp) AS(
SELECT L.seq, L.value, L.seq - COUNT(*)
FROM t1 AS L
INNER JOIN t1 AS R
ON R.value = L.value
AND R.seq <= L.seq
GROUP BY L.value, L.seq)
SELECT MIN(seq) AS beg, MAX(seq) AS end, val
FROM T
GROUP BY val, grp

---
Andrey Odegov
av******@yandex.ru
(remove GOV to respond)


Jun 14 '06 #10
Fred
What is your answer for William's quesstion?
If INSERT INTO TABLE T1 VALUES (2, 'A') were not present, would you
expect:
1 1 A
3 3 A
5 6 B
7 7 A


Or do you want this?
1 3 A
5 6 B
7 7 A

Jun 15 '06 #11
Another version with the outer limits of each sequential run. I assume
a table of sequential numbers called Sequence and the little-used ALL()
predicate.

SELECT val, MIN(lft), rgt
FROM (
SELECT F1.val, S1.seq, MAX(S2.seq)
FROM Foobar AS F1, Sequence AS S1, Sequence AS S2
WHERE S2.seq BETWEEN S1.seq AND (SELECT MAX(seq) FROM Foobar)
AND F1.val
= ALL(SELECT val
FROM Foobar AS F2
WHERE F2.seq BETWEEN S1.seq AND S2.seq
AND S1.seq <= S2.seq)
GROUP BY F1.val, S1.seq) AS X (val, lft, rgt)
GROUP BY X.val, X.rgt;

Jun 16 '06 #12
Fred
If INSERT INTO TABLE T1 VALUES (2, 'A') were not present, which
result would you expect?
1)
1 1 A
3 3 A
5 6 B
7 7 A

Or

2)
1 3 A
5 6 B
7 7 A

By, using OLAP function, you can easily chane the result. DIfference is
only RANGE or ROWS in OLAP functons.

If you expect 1)
SELECT MIN(seq) AS Beg
, MAX(seq) AS End
, MAX(value) AS Value
FROM (SELECT seq, value, fol_value
, ROWNUMBER() OVER(ORDER BY seq) rn
FROM (SELECT seq
, value
, MAX(value) OVER(ORDER BY seq
RANGE BETWEEN 1 PRECEDING
AND 1 PRECEDING)
, MAX(value) OVER(ORDER BY seq
RANGE BETWEEN 1 FOLLOWING
AND 1 FOLLOWING)
FROM Fred.t1
) Q (seq, value, pre_value, fol_value)
WHERE value <> COALESCE(pre_value, '*')
OR value <> COALESCE(fol_value, '*')
) R
GROUP BY
rn + CASE WHEN value = fol_value THEN 1 ELSE 0 END
ORDER BY
Beg;
--------------------------------------------------------------------

BEG END VALUE
----------- ----------- -----
1 1 A
3 3 A
5 6 B
7 7 A

4 record(s) selected.

If you expect 2)
SELECT MIN(seq) AS Beg
, MAX(seq) AS End
, MAX(value) AS Value
FROM (SELECT seq, value, fol_value
, ROWNUMBER() OVER(ORDER BY seq) rn
FROM (SELECT seq
, value
, MAX(value) OVER(ORDER BY seq
ROWS BETWEEN 1 PRECEDING
AND 1 PRECEDING)
, MAX(value) OVER(ORDER BY seq
ROWS BETWEEN 1 FOLLOWING
AND 1 FOLLOWING)
FROM Fred.t1
) Q (seq, value, pre_value, fol_value)
WHERE value <> COALESCE(pre_value, '*')
OR value <> COALESCE(fol_value, '*')
) R
GROUP BY
rn + CASE WHEN value = fol_value THEN 1 ELSE 0 END
ORDER BY
Beg;
-------------------------------------------------------------------

BEG END VALUE
----------- ----------- -----
1 3 A
5 6 B
7 7 A

3 record(s) selected.

Jun 18 '06 #13
Fred,
I'm very sorry.
I realized just now that you already answered Wiiliam's question.
if (2,'A') were omitted, the results would be as you have it.

I beg your perdon for my carelessness and to bother you.

Jun 18 '06 #14

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

Similar topics

11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: Steve Patrick | last post by:
Hi All You guys are my last hope, despite spending money on books and hours reading them I still can not achieve the results I need. I have designed a database in Access 2000 based on 1 table,...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.