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

View with grouping

Hi,

I am attempting to create a view that will rollup or group like, consecutive
data. I have created a view using unions, but the statement is extremely
slow.

Here is example data

Colour Table:

Colour_id hole_number depth_from depth_to colour
1 1 0 2
red
2 1 2 6
red
3 1 6 8
blue
4 1 8 10
blue
5 1 12 14
blue
6 1 14 16
red
7 2 0 5
orange
8 2 5 10
orange
9 2 10 15
red
Using this example data, we need to create groups of all consecutive data
with the same colour for the same hole.

For example, my view needs to produce the following output where depth_from
is the smallest depth_from in the group and depth_to is the largest depth_to
in the group

hole_number depth_from depth_to colour
1 0 6 red
1 6 14 blue
1 14 16 red
2 0 10 orange
2 10 15 red

The groups can contain gaps, but no overlaps. The data in each group must
be consecutive and is grouped by the colour column.

Here is my existing statement. The first select brings back all groups
containing multiple rows, the second select brings back all groups that
aren't multiples and aren't the first or last row, the third select brings
back the last row as a group if it doesn't fall within a group already, and
the last select returns the first row as a group if it doesn't fall within a
group already.
select min(colour.depth_from) as min_depth_from,

max_colour.depth_to as max_depth_to,

colour.colour,

colour.hole_number as hole

from colour

join colour as max_colour

on max_colour.hole_number = colour.hole_number

AND max_colour.colour=colour.colour

AND max_colour.depth_from > colour.depth_from

AND max_colour.depth_to > colour.depth_to

AND (select count(colour_id)

from colour as mid

where mid.depth_from < max_colour.depth_from

AND mid.depth_from > hole_colour.depth_from

AND mid.depth_to < max_colour.depth_to

AND mid.depth_to > colour.depth_to

AND mid.hole_number = colour.hole_number

AND mid.colour= colour.colour) >= 0

AND (select count(colour_id)

from colour as mid

where mid.depth_from < max_colour.depth_from

AND mid.depth_from > colour.depth_from

AND mid.depth_to < max_colour.depth_to

AND mid.depth_to > colour.depth_to

AND mid.hole_number = colour.hole_number

AND mid.colour<> colour.colour) = 0

AND max_colour.depth_to = (select max(depth_to)

from colour. as maximum_colour

where maximum_colour.depth_to >
colour.depth_to

AND maximum_colour.depth_from >
colour.depth_from

AND maximum_colour.hole_number =
colour.hole_number

AND maximum_colour.colour= colour.colour

AND (select count(colour_id)

from colour as
mid

where
mid.depth_from <= maximum_colour.depth_from

AND
mid.depth_from > colour.depth_from

AND mid.depth_to
<= colour.depth_to

AND mid.depth_to
colour.depth_to


AND
mid.hole_number = colour.hole_number

AND mid.colour<>
colour.colour) = 0)

group by colour.hole_number, colour.colour, max_colour.depth_to,
max_colour.depth_from

UNION

select min(colour.depth_from)as min_depth_from,

max(colour.depth_to) as max_depth_to,

colour.rock_type_code,

colour.hole_number as hole

from colour

join colour as next_colour

on next_colour.depth_from = colour.depth_to

and colour.hole_number = colour.hole_number

AND next_colour.colour <> colour.colour

JOIN colour as prev_colour

on prev_colour.depth_to = colour.depth_from

and prev_colour.hole_number = colour.hole_number

AND prev_colour.colour <> colour.colour

group by colour.hole_number, colour.colour, colour.depth_from,
colour.depth_to

UNION

select min(colour.depth_from)as min_depth_from,

max(colour.depth_to) as max_depth_to,

colour.colour,

colour.hole_number as hole

from colour

JOIN colour as prev_colour

ON prev_colour.depth_to = colour.depth_from

and prev_colour.hole_number = colour.hole_number

AND prev_colour.colour<> colour.colour

and colour.depth_to = (select max(colour.depth_to)

from colour

where colour.hole_number =
prev_colour.hole_number)

group by colour.hole_number, colour.colour, colour.depth_from,
colour.depth_to

UNION

select min(colour.depth_from)as min_depth_from,

max(colour.depth_to) as max_depth_to,

colour.colour,

colour.hole_number as hole

from colour

JOIN colour as next_colour

ON next_colour.depth_from = colour.depth_to

and next_colour.hole_number = colour.hole_number

AND next_colour.colour<> colour.colour

and colour.depth_from = (select min(colour.depth_from)

from colour

where colour.hole_number =
next_colour.hole_number)

group by colour.hole_number, colour.colour, colour.depth_from,
colour.depth_to



Any help to speed this statement up or to re-write this statement so that it
is quicker would be appreciated.
Thanks,

Karen

Jul 20 '05 #1
5 1430
It helps if you post DDL with questions like this and include your sample
data as INSERT statements.

Here's what I assume your DDL looks like:

CREATE TABLE Colour (colour_id INTEGER NOT NULL UNIQUE, hole_number INTEGER
NOT NULL, depth_from INTEGER NOT NULL, depth_to INTEGER NOT NULL, colour
CHAR(10) NOT NULL, CHECK(depth_from<depth_to), PRIMARY KEY
(depth_from,colour), UNIQUE (depth_to, colour))

INSERT INTO Colour VALUES (1,1,0,2,'red')
INSERT INTO Colour VALUES (2,1,2,6,'red')
INSERT INTO Colour VALUES (3,1,6,8,'blue')
INSERT INTO Colour VALUES (4,1,8,10,'blue')
INSERT INTO Colour VALUES (5,1,12,14,'blue')
INSERT INTO Colour VALUES (6,1,14,16,'red')
INSERT INTO Colour VALUES (7,2,0,5,'orange')
INSERT INTO Colour VALUES (8,2,5,10,'orange')
INSERT INTO Colour VALUES (9,2,10,15,'red')

And here's a query to produce the result you want:

SELECT A.hole_number,
MIN(A.depth_from) AS depth_from,
MAX(A.depth_to) AS depth_to, A.colour
FROM Colour AS A
JOIN
(SELECT C1.colour_id, MIN(C2.depth_to) AS next_depth
FROM Colour AS C1
LEFT JOIN Colour AS C2
ON C1.hole_number = C2.hole_number
AND C1.colour <> C2.colour
AND (C1.depth_from < C2.depth_from
OR (C1.depth_from = C2.depth_from)
AND C1.depth_to <= C2.depth_to)
GROUP BY C1.colour_id) AS B
ON A.colour_id = B.colour_id
GROUP BY A.hole_number, A.colour, B.next_depth

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
See also this identical post in reply to Dave Pylatuk. (Familiar?! :-))

http://tinyurl.com/thm5

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #3
Hi David,

Yes, that looks very familiar. Thanks alot. I was looking for those posts.

The problem is, the examples given do not correctly group none consecutive
colours when they are seperated by a different colour.

For Example:
Data:
Hole Depth From Depth To Colour
hole1 0 5 red
hole1 5 10 red
hole1 10 15 blue
hole1 15 20 red

Using the examples given, there would only be 1 group for the colour red.

Example Result
Hole Depth From Depth To Colour
hole1 0 20 red
hole1 10 15 blue

We need the view to correctly seperate the groups and return the following
result.

Good Result
Hole Depth From Depth To Colour
hole1 0 10 red
hole1 10 15 blue
hole1 15 20 red

My sql statement i provided in my previous post correctly groups the data,
but it is extremely slow and needs to be quicker to be usefull.

Thanks for your time,

Karen
"David Portas" <RE****************************@acm.org> wrote in message
news:SM********************@giganews.com...
See also this identical post in reply to Dave Pylatuk. (Familiar?! :-))

http://tinyurl.com/thm5

--
David Portas
------------
Please reply only to the newsgroup
--

Jul 20 '05 #4
I don't think you tried the query I posted earlier:

SELECT A.hole_number,
MIN(A.depth_from) AS depth_from,
MAX(A.depth_to) AS depth_to, A.colour
FROM Colour AS A
JOIN
(SELECT C1.colour_id, MIN(C2.depth_to) AS next_depth
FROM Colour AS C1
LEFT JOIN Colour AS C2
ON C1.hole_number = C2.hole_number
AND C1.colour <> C2.colour
AND (C1.depth_from < C2.depth_from
OR (C1.depth_from = C2.depth_from)
AND C1.depth_to <= C2.depth_to)
GROUP BY C1.colour_id) AS B
ON A.colour_id = B.colour_id
GROUP BY A.hole_number, A.colour, B.next_depth

Here's the result I get:

hole_number depth_from depth_to colour
----------- ----------- ----------- ----------
1 0 10 red
1 10 15 blue
1 15 20 red

(3 row(s) affected)

Which is what you say you wanted.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #5
Your correct Dave. Sorry about that.

Works Great.

Thank you very much.

Karen

"David Portas" <RE****************************@acm.org> wrote in message
news:oO********************@giganews.com...
I don't think you tried the query I posted earlier:

SELECT A.hole_number,
MIN(A.depth_from) AS depth_from,
MAX(A.depth_to) AS depth_to, A.colour
FROM Colour AS A
JOIN
(SELECT C1.colour_id, MIN(C2.depth_to) AS next_depth
FROM Colour AS C1
LEFT JOIN Colour AS C2
ON C1.hole_number = C2.hole_number
AND C1.colour <> C2.colour
AND (C1.depth_from < C2.depth_from
OR (C1.depth_from = C2.depth_from)
AND C1.depth_to <= C2.depth_to)
GROUP BY C1.colour_id) AS B
ON A.colour_id = B.colour_id
GROUP BY A.hole_number, A.colour, B.next_depth

Here's the result I get:

hole_number depth_from depth_to colour
----------- ----------- ----------- ----------
1 0 10 red
1 10 15 blue
1 15 20 red

(3 row(s) affected)

Which is what you say you wanted.

--
David Portas
------------
Please reply only to the newsgroup
--

Jul 20 '05 #6

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

Similar topics

5
by: Michael Krzepkowski | last post by:
All, I have a view that returns the following values: Item Vendor 70807 1234 70807 5678 If I am looking for items that have more...
1
by: MR71 | last post by:
I've created a SQL View in SQL 2000 using a table that has columns defined as Numeric data type. In the view, I am grouping and summing. When I link the view via ODBC to Access, all of the data...
1
by: amber | last post by:
Hello, I have a report in VB.NET/Crystal Reports. I have a criteria form that users select between 2 different types of grouping (group by category or group by year). Can I programmatically...
0
by: AMDIRT | last post by:
I have a few questions about IssueVision (from WindowsForms) concerning its scalability and performance. Rather, if I were to implement techniques described here into another application, how...
3
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
6
by: Bethany Holliday | last post by:
Hi All, I'm hoping someone can help me. I think I'm missing something very basic. I'm trying to put a clustered index on a view that I have created. I keep getting the error: Server: Msg...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
0
MMcCarthy
by: MMcCarthy | last post by:
Rather than using the Access design view change the view to SQL. I am going to attempt to outline the general syntax used for SQL queries in Access. Angle brackets <> are used in place of some...
0
by: Roman Bertle | last post by:
Hello, I try to format monetary values using the locale module, python2.5: Python 2.5.2a0 (r251:54863, Jan 3 2008, 17:59:56) on linux2 Type "help", "copyright", "credits" or "license" for...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.