Solved

Need to parse one column into multiple fields..

Posted on 2010-09-07
9
423 Views
Last Modified: 2013-11-28
Hi Experts,

I have a table that was passed to me by a colleague. The table only has two columns, the first is just an index (specifying order) and the second contains the contents of a flat file that contains multiple rows which represent ONE record where each row has a text field with a keyword followed by a value.

Example:

Table: SOURCE_TABLE
IDX   |   KEYS
1     |    STARTKEY  333
2    |     KEY_A   4000
3    |     KEY_B    5555
4    |     KEY_C    6656
5    |    
6    |     STARTKEY  766
7    |     KEY_A   3000
8    |     KEY_B    2363
9 ....

I would like to parse through every "line" of this table and pull out the real rows to insert into another table.

For instance, the final table would look like this:

Table: DEST_TABLE
START_KEY   |   KEY_A   |   KEY_B   |   KEY_C   |   .....
333      |      4000      |     5555     |     6656     |     ....
766      |      3000      |     2363     |    .....

All key names are known, there are about 30 of them.  I guess I need something to first loop through all records in the source table and then for each row, look at the key names and record their values and also, if the key name changes to "START_KEY", then write out all of the keys found since the last "START_KEY"...

In VB, I would probably start by defining all known keys like this:

DIM START_VALUE=0
DIM KEY_A_VALUE=0
DIM KEY_B_VALUE=0
DIM KEY_C_VALUE=0
.....

But that's where I'm stuck...  How do I "select * from SORUCE_TABLE" and then feed the data to a parser that will split the data and record the values to "insert" a row in my other table?


0
Comment
Question by:bganoush
9 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
here is one which is extremely inefficient I guess ;)

SELECT idx, val, keys,
(select top 1 val from source s_a where s_a.idx>s.idx and keys='KEY_A' order by idx) as key_a,
(select top 1 val from source s_b where s_b.idx>s.idx and keys='KEY_B' order by idx) as key_b,
(select top 1 val from source s_c where s_c.idx>s.idx and keys='KEY_C' order by idx) as key_c,
(select top 1 val from source s_d where s_d.idx>s.idx and keys='KEY_D' order by idx) as key_d
from source s
where keys='STARTKEY'
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
with two columns table like you have

SELECT idx, val, keys,
(select top 1 val from source s_a where s_a.idx>s.idx and keys like 'KEY_A*' order by idx) as key_a,
(select top 1 val from source s_b where s_b.idx>s.idx and keys like 'KEY_B*' order by idx) as key_b,
(select top 1 val from source s_c where s_c.idx>s.idx and keys like 'KEY_C*' order by idx) as key_c,
(select top 1 val from source s_d where s_d.idx>s.idx and keys like 'KEY_D*' order by idx) as key_d
from source s
where keys like 'STARTKEY*'
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
if you save this query as q_source

SELECT source.idx,  source.keys,
left(keys, instr(1, keys," ")-1) as key,
right(keys,len(keys)- instrRev(keys," ")) as val
FROM source;

you can use the one posted @ 33619490 by changing the table to query

SELECT idx, val, key,
(select top 1 val from q_source s_a where s_a.idx>s.idx and key='KEY_A' order by idx) as key_a,
(select top 1 val from q_source s_b where s_b.idx>s.idx and key='KEY_B' order by idx) as key_b,
(select top 1 val from q_source s_c where s_c.idx>s.idx and key='KEY_C' order by idx) as key_c,
(select top 1 val from q_source s_d where s_d.idx>s.idx and key='KEY_D' order by idx) as key_d
from q_source s
where key='STARTKEY'
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Haven't fully tested HainKurt's SQL, but this appears to be working against your example.

Please see the attached sample file; query SQL statement is below.


SELECT s2.KEYS AS StartKey, Max(IIf(z.KeyName = "KEY_A", z.KeyValue, Null)) AS KEY_A, Max(IIf(z.KeyName = "KEY_B", z.KeyValue, Null)) AS KEY_B, Max(IIf(z.KeyName = "KEY_C", z.KeyValue, Null)) AS KEY_C, Max(IIf(z.KeyName = "KEY_D", z.KeyValue, Null)) AS KEY_D, Max(IIf(z.KeyName = "KEY_E", z.KeyValue, Null)) AS KEY_E, Max(IIf(z.KeyName = "KEY_F", z.KeyValue, Null)) AS KEY_F, Max(IIf(z.KeyName = "KEY_G", z.KeyValue, Null)) AS KEY_G, Max(IIf(z.KeyName = "KEY_H", z.KeyValue, Null)) AS KEY_H, Max(IIf(z.KeyName = "KEY_Z", z.KeyValue, Null)) AS KEY_Z
FROM SOURCE_TABLE AS s2 INNER JOIN [SELECT CLng(DMax("IDX","SOURCE_TABLE","[KEYS] Like 'STARTKEY*' AND [IDX] < " & s.[IDX])) AS MyKey, s.IDX, Left(s.[KEYS],InStr(1,s.[KEYS] & " "," ")-1) AS KeyName, Val(Mid([s.KEYS],InStr(1,s.[KEYS] & " "," ")+1)) AS KeyValue
FROM SOURCE_TABLE s
WHERE s.KEYS Like "KEY*"
ORDER BY s.IDX]. AS z ON s2.IDX = z.MyKey
GROUP BY s2.KEYS
ORDER BY s2.KEYS;

Open in new window

Q-26456478.mdb
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Better formatting:


SELECT s2.KEYS AS StartKey, 
    Max(IIf(z.KeyName = "KEY_A", z.KeyValue, Null)) AS KEY_A, 
    Max(IIf(z.KeyName = "KEY_B", z.KeyValue, Null)) AS KEY_B, 
    Max(IIf(z.KeyName = "KEY_C", z.KeyValue, Null)) AS KEY_C, 
    Max(IIf(z.KeyName = "KEY_D", z.KeyValue, Null)) AS KEY_D, 
    Max(IIf(z.KeyName = "KEY_E", z.KeyValue, Null)) AS KEY_E, 
    Max(IIf(z.KeyName = "KEY_F", z.KeyValue, Null)) AS KEY_F, 
    Max(IIf(z.KeyName = "KEY_G", z.KeyValue, Null)) AS KEY_G, 
    Max(IIf(z.KeyName = "KEY_H", z.KeyValue, Null)) AS KEY_H, 
    Max(IIf(z.KeyName = "KEY_Z", z.KeyValue, Null)) AS KEY_Z
FROM SOURCE_TABLE AS s2 INNER JOIN 
    [SELECT CLng(DMax("IDX","SOURCE_TABLE","[KEYS] Like 'STARTKEY*' AND [IDX] < " & s.[IDX])) AS MyKey, 
        s.IDX, Left(s.[KEYS],InStr(1,s.[KEYS] & " "," ")-1) AS KeyName, 
        Val(Mid([s.KEYS],InStr(1,s.[KEYS] & " "," ")+1)) AS KeyValue
    FROM SOURCE_TABLE s
    WHERE s.KEYS Like "KEY*"
    ORDER BY s.IDX]. AS z ON s2.IDX = z.MyKey
GROUP BY s2.KEYS
ORDER BY s2.KEYS;

Open in new window

0
 

Author Comment

by:bganoush
Comment Utility

It gives a syntax error at 'startkey*'
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
Comment Utility
bganoush,

Please see the sample file I uploaded, which very closely replicates the example from your question.  The query "qryResults" works perfectly there.

I suggest that you post a sample file of your own.  Please be sure to sanitize any sensitive data first.

Patrick
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
did you try my posts (especially 2nd & third one)? any comment?
0
 
LVL 30

Expert Comment

by:hnasr
Comment Utility
Do you need more comments? I can't check all previous comments.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now