Solved

Need to parse one column into multiple fields..

Posted on 2010-09-07
9
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
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 51

Expert Comment

by:Huseyin KAHRAMAN
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 51

Expert Comment

by:Huseyin KAHRAMAN
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 92

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 92

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 92

Accepted Solution

by:
Patrick Matthews earned 500 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 51

Expert Comment

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

Expert Comment

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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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.

733 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