Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 2 / October 2007

Tip: Looking for answers? Try searching our database.

Sequence Grouping

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Beagle - 02 Oct 2007 01:36 GMT
Hello,

Below is my table:

Raw_Data
@S1
A02F
A12F
A22F
@TPO
@TPC
@S2
A02F
A12F
A22F
@TPO
@TPC
etc……

The logice of the data is that "@" is the header record, and the "A" is the
detail of "@".

I want to wite a quert that results the following result in the "TAG" column:

Raw_Data    Tag
@S1    1
A02F    1
A12F    1
A22F    1
@TPO    2
@TPC    3
@S2    4
A02F    4
A12F    4
A22F    4
@TPO    5
@TPC    6

Basically, the "TAG" column is grouping the header records with the detail.  
Also, the data is "always" in sequentail order (which is a good thing).

Thanks,

Beagle
James A. Fortune - 02 Oct 2007 02:10 GMT
> Hello,
>
[quoted text clipped - 40 lines]
>
> Beagle

Your problem seems to be one of the few cases where the order of the
records is important.

Have a subquery count the number of records before or equal to the
present one that start with @.

First add an autonumber primary key to your table:

tblRawData
RDID AutoNumber
Raw_Data Text

Then check to make sure your data is still in the same order.

If it is, try:

SELECT Raw_Data, (SELECT Count(A.Raw_Data) FROM tblRawData AS A WHERE
A.RDID <= tblRawData.RDID AND Left([Raw_Data], 1) = "@") AS Tag FROM
tblRawData;

I tried it on a table with the first six records you show.  It seems to
do what you want.

James A. Fortune
MPAPoster@FortuneJames.com
Beagle - 02 Oct 2007 14:41 GMT
Thanks James,

It works perfect.  Is there a way you could breakdown the statement so I
could understand it a little better?

Beagle

> > Hello,
> >
[quoted text clipped - 66 lines]
> James A. Fortune
> MPAPoster@FortuneJames.com
James A. Fortune - 02 Oct 2007 18:35 GMT
> Thanks James,
>
> It works perfect.  Is there a way you could breakdown the statement so I
> could understand it a little better?
>
> Beagle

I'll try.

tblRawData
RDID Raw_Data
1 @S1
2 A02F
3 A12F
4 A22F
5 @TPO
6 @TPC

SELECT Raw_Data, (SELECT Count(A.Raw_Data) FROM tblRawData AS A WHERE
A.RDID <= tblRawData.RDID AND Left([Raw_Data], 1) = "@") AS Tag FROM
tblRawData;

When the query is at tblRawData.RDID = 1 you get:

@S1, (SELECT Count(Raw_Data) FROM tblRawData WHERE RDID <= 1 AND
Left([Raw_Data], 1) = "@") AS Tag
= @S1, 1

When tblRawData.RDID = 2 you get:

A02F, (SELECT Count(Raw_Data) FROM tblRawData WHERE RDID <= 2 AND
Left([Raw_Data], 1) = "@") As Tag
= A02F, 1 + 0

The 0 is because the leftmost character of A02F is not '@'

When tblRawData.RDID = 3 you get:

A12F, (SELECT Count(Raw_Data) FROM tblRawData WHERE RDID <= 3 AND
Left([Raw_Data], 1) = "@") As Tag
= A12F, 1 + 0 + 0

When tblRawData.RDID = 4 you get:

A22F, (SELECT Count(Raw_Data) FROM tblRawData WHERE RDID <= 4 AND
Left([Raw_Data], 1) = "@") As Tag
= A22F, 1 + 0 + 0 + 0

When tblRawData.RDID = 5 you get:

@TPO, (SELECT Count(Raw_Data) FROM tblRawData WHERE RDID <= 5 AND
Left([Raw_Data], 1) = "@") As Tag
= @TPO, 1 + 0 + 0 + 0 + 1

When tblRawData.RDID = 6 you get:

@TPC, (SELECT Count(Raw_Data) FROM tblRawData WHERE RDID <= 6 AND
Left([Raw_Data], 1) = "@") As Tag
= @TPC, 1 + 0 + 0 + 0 + 1 + 1

Note: The query assumes that there are no records with Raw_Data = Null.

James A. Fortune
MPAPoster@FortuneJames.com
Beagle - 04 Oct 2007 01:51 GMT
Thanks James, Very Cool....  This will gove me someting to think about.

> > Thanks James,
> >
[quoted text clipped - 60 lines]
> James A. Fortune
> MPAPoster@FortuneJames.com
Beagle - 19 Oct 2007 13:52 GMT
James,

Having a problem appying the SQL statement.

The main datatable has 7,000 rows of data (not very much).  When the SQL
statement is applied, the query will not finish running.  If I reduce the
number of records to 58 from 7,000, then the query will finish running.

Any suggestions on how I could keep the 7,000+ rows of data and have the
query finish?

Thanks,

Beagle

> > Thanks James,
> >
[quoted text clipped - 60 lines]
> James A. Fortune
> MPAPoster@FortuneJames.com
James A. Fortune - 21 Oct 2007 06:04 GMT
> James,
>
[quoted text clipped - 10 lines]
>
> Beagle

Even 7,000 records should not cause the query to take over a couple of
minutes.  Make sure RDID is the primary key and that Raw_Data is
indexed.  If it's too slow after doing that, post back and I'll try
writing a VBA routine that makes a single pass through the data.

James A. Fortune
MPAPoster@FortuneJames.com
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.