Pregunta

I have 2 tables that are given (table_A, Table_B) and

Need help for a sub query or possible solution to SELECT multiple values from column TABLE_A.DESCRIPString

TABLE_A:
ID     PARTNUM     DESCRIPString
1      4456        121~134~111
2      4457        122~111
3      4458        122~134
4      4459        111
5      4460        121~134~111

CROSS MATCH with the TABLE_B.DESCRIPID

TABLE_B:
ID     DESCRIPID   DECSRIPLong
1      121         Silver
2      122         Black
3      111         Mask
4      134         Pickle

And at last DISPLAY the following:

Table_AB

ID     PARTNUM     DESCRIPString    PARTDESCRIP
1      4456        121~134~111      Silver~Pickle~Mask
2      4457        122~111          Black~Mask
3      4458        122~134          Black~Pickle
4      4459        111              Mask
5      4460        121~134~111      Silver~Pickle~Mask

I understand most people will recommend to redo the database to Single variable per column however that can not be done on this case.

¿Fue útil?

Solución

Drop this function into a module and run this query. It should give you the requested results. You didn't specify data types so I went with variants in the function.

Public Function fnParseDescr(strWork As Variant) As String
Dim vEnd
Dim strTemp As String
Dim strLook As String
Dim strResult As String
strResult = ""
strWork = Trim(CStr(strWork))
Do While Len(strWork) > 0
    If InStr(strWork, "~") = 0 Then
        strLook = Trim(DLookup("[DESCRIPLong]", "TABLE_B", "[DESCRIPID] = CVar('" & strWork & "')"))
        strResult = strResult & "~" & strLook
        strWork = ""
    Else
        vEnd = InStr(strWork, "~") - 1
        strTemp = Left(strWork, vEnd)
        strLook = Trim(DLookup("[DESCRIPLong]", "TABLE_B", "[DESCRIPID] = CVar('" & strTemp & "')"))
        strResult = strResult & "~" & strLook
        strWork = Right(strWork, (Len(strWork) - (vEnd + 1)))
        strTemp = ""
        strLook = ""
    End If
Loop
If Left(strResult, 1) = "~" Then strResult = Right(strResult, (Len(strResult) - 1))
fnParseDescr = strResult
Exit Function
End Function


SELECT ID,
    PARTNUM, 
    DESCRIPString, 
    fnParseDescr([DESCRIPString]) AS PARTDESCRIP
FROM TABLE_A;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top