Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need to parse one column into multiple fields..

Posted on 2010-09-07
9
Medium Priority
?
469 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 61

Expert Comment

by:HainKurt
ID: 33619490
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 61

Expert Comment

by:HainKurt
ID: 33619545
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 61

Expert Comment

by:HainKurt
ID: 33619584
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33619791
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
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33619811
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
ID: 33620360

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

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 33620465
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 61

Expert Comment

by:HainKurt
ID: 33623457
did you try my posts (especially 2nd & third one)? any comment?
0
 
LVL 31

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

926 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