简体   繁体   中英

How do I split two columns in two separate tables into joined multiple rows in a view?

I have two separate tables, each containing character-separated cells. One table contains all the key data, the other all the val data. In the application, both tables are loaded into recordsets, the two cells are split into two arrays and the arrays are then used 'side-by side' to create key/val pair.

Trying to decouple the database from the application, I will basically create a view that emulates this behavior.

I created some sample tables and data to better illustrate it.

/* Create tables */
CREATE TABLE [dbo].[tblKeys](
    [KeyId] [int] NOT NULL, /*PK*/
    [KeyData] [nvarchar](max) NULL
)
CREATE TABLE [dbo].[tblValues](
    [ValId] [int] NOT NULL, /*PK*/
    [KeyId] [int] NOT NULL, /*FK*/
    [Language] [nvarchar](5) NOT NULL,
    [ValData] [nvarchar](max) NULL
)

/* Populate tables */
INSERT INTO [dbo].[tblKeys] ([KeyId], [KeyData]) VALUES
    (1, '1|2|3'),
    (2, '1|2|3|4'),
    (3, '2|1')
INSERT INTO [dbo].[tblValues] ([ValId], [KeyId], [Language], [ValData]) VALUES
    (1, 1, 'en',    'Apple|Orange|Pear'),
    (2, 1, 'sv-se', 'Äpple|Apelsin|Päron'),
    (3, 2, 'en',    'Milk|Butter|Cheese|Cream'),
    (4, 2, 'sv-se', 'Mjölk|Smör|Ost|Grädde'),
    (5, 3, 'en',    'Male|Female'),
    (6, 3, 'sv-se', 'Man|Kvinna')

The desired end result in the view looks like this:

| KeyId | KeyData | Language | ValData
+-------+---------+----------+----------+
|     1 |       1 |       en | Apple    |
|     1 |       2 |       en | Orange   |
|     1 |       3 |       en | Pear     |
|     1 |       1 |    sv-se | Äpple    |
|     1 |       2 |    sv-se | Apelsin  |
|     1 |       3 |    sv-se | Päron    |
...

etc.

I've seen similar questions here on StackOverflow, but they all deal with a single table being tilted in similar ways. I need to tilt both tables while using the positions of the data in the two columns KeyData and ValData as siginficants when recombining them into proper key/val pairs.

How would I do this in an efficient manner?

[Edit]: The database design is not mine. It's some old crap I inhereted. I know it's bad. Bad as in horrendous .

This would work for you. But: you really should change your data design!

;WITH Splitted AS
(
    SELECT v.Language
          ,v.ValData
          ,v.ValId
          ,k.KeyData
          ,k.KeyId
          ,CAST('<x>' + REPLACE(v.ValData,'|','</x><x>') + '</x>' AS XML) AS ValData_XML 
          ,CAST('<x>' + REPLACE(k.KeyData,'|','</x><x>') + '</x>' AS XML) AS KeyData_XML 
    FROM tblValues AS v
    INNER JOIN tblKeys AS k ON v.KeyId=k.KeyId
)
,NumberedValData AS
(
    SELECT ROW_NUMBER() OVER(PARTITION BY KeyId,Language ORDER BY (SELECT NULL)) AS RowNumber
      ,KeyId
      ,Language
      ,A.B.value('.','varchar(max)') AS ValD 
    FROM Splitted
    CROSS APPLY Splitted.ValData_XML.nodes('/x') AS A(B)
)
,NumberedKeyData AS
(
    SELECT ROW_NUMBER() OVER(PARTITION BY KeyId,Language ORDER BY (SELECT NULL)) AS RowNumber
      ,KeyId
      ,Language
      ,A.B.value('.','varchar(max)') AS KeyD 
    FROM Splitted
    CROSS APPLY Splitted.KeyData_XML.nodes('/x') AS A(B)
)
,Combined AS
(
    SELECT nk.KeyId
          ,nk.KeyD
          ,nk.Language
          ,nv.ValD
    FROM NumberedKeyData AS nk
    INNER JOIN NumberedValData AS nv ON nk.KeyId=nv.KeyId AND nk.Language=nv.Language AND nk.RowNumber=nv.RowNumber
)
SELECT Combined.KeyId
      ,Combined.KeyD AS KeyData
      ,Splitted.Language  
      ,Combined.ValD AS ValData
FROM Splitted
INNER JOIN Combined ON Splitted.KeyId=Combined.KeyId AND Splitted.Language=Combined.Language
ORDER BY Splitted.KeyId,Splitted.

The result

KeyId   KeyData Language    ValData
   1    1       en          Apple
   1    2       en          Orange
   1    3       en          Pear
   1    1       sv-se       Äpple
   1    2       sv-se       Apelsin
   1    3       sv-se       Päron
   2    1       en          Milk
   2    2       en          Butter
 [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM