SQL SELECT Data from other table based on column value

Saravanan

Gurus, I have a table like below

Id Name Source Value
1   a      Dx   C
2   b      Rx   G
3   C      Tx   H

I have 3 other tables like T1,T2 and T3 with structure as below. Based on "Source" column in parent table, I need to fetch "Data column" and show as result. If multiple records match need all record as comma-seperated

T1

Id       Data
Dx        123
DX        011  
T2

Id       Data
Rx        456
Rx       022  

T3

Id       Data
Tx        789     

I need Output as in T-SQL

Id Name Source Value Data
1   a      Dx   C     123,011
2   b      Rx   G     456 ,022
3   C      Tx   H     789

I tried with Case when but not successful. Need inputs

Giorgi Nakeuri

Try this:

DECLARE @t TABLE(ID INT, Name CHAR(1), Source CHAR(2), Value CHAR(1))
DECLARE @t1 TABLE(ID CHAR(2), Data NVARCHAR(20))
DECLARE @t2 TABLE(ID CHAR(2), Data NVARCHAR(20))
DECLARE @t3 TABLE(ID CHAR(2), Data NVARCHAR(20))

INSERT INTO @t VALUES
(1, 'a', 'Dx', 'C'),
(2, 'b', 'Rx', 'G'),
(3, 'c', 'Tx', 'H')

INSERT INTO @t1 VALUES('Dx', '1231')
INSERT INTO @t1 VALUES('Dx', '1232')
INSERT INTO @t1 VALUES('Dx', '1233')
INSERT INTO @t2 VALUES('Rx', '4561')
INSERT INTO @t2 VALUES('Rx', '4562')
INSERT INTO @t3 VALUES('Tx', '789')


SELECT  t.ID ,
        t.Name ,
        t.Source ,
        t.Value ,
        COALESCE(c1.Data1, c2.Data2, c3.Data3) AS Data
FROM @t t
OUTER APPLY (SELECT STUFF((SELECT ',' + Data 
             FROM @t1 t1 WHERE t.Source = t1.ID
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') AS Data1) c1
OUTER APPLY (SELECT STUFF((SELECT ',' + Data 
             FROM @t2 t2 WHERE t.Source = t2.ID
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') AS Data2) c2
OUTER APPLY (SELECT STUFF((SELECT ',' + Data 
            FROM @t3 t3 WHERE t.Source = t3.ID
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'') AS Data3) c3   

Output:

ID  Name    Source  Value   Data
1   a       Dx      C       1231,1232,1233
2   b       Rx      G       4561,4562
3   c       Tx      H       789

Version with CASE expression:

SELECT  t.ID ,
        t.Name ,
        t.Source ,
        t.Value ,
        o.Data
FROM    @t t
        OUTER APPLY ( SELECT    CASE t.Source
                                  WHEN 'Dx' THEN STUFF((SELECT
                                                              ',' + Data
                                                        FROM  @t1 t1
                                                        WHERE t.Source = t1.ID
                                       FOR             XML PATH('') ,
                                                           TYPE
            ).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
                                  WHEN 'Rx' THEN STUFF((SELECT
                                                              ',' + Data
                                                        FROM  @t2 t2
                                                        WHERE t.Source = t2.ID
                                       FOR             XML PATH('') ,
                                                           TYPE
            ).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
                                  WHEN 'Tx' THEN STUFF((SELECT
                                                              ',' + Data
                                                        FROM  @t3 t3
                                                        WHERE t.Source = t3.ID
                                       FOR             XML PATH('') ,
                                                           TYPE
        ).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
                                END AS DATA
                    ) o

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL Select value from other table based on column value as treshold

SQL query to select from one table based on a column value in other table

SQL query to select out of 3 options and from other table, based on column value

How to SELECT in SQL based on a value from the same table column?

How to select 2 data from 1 column based on other relations in other table

SQL Server 2000 - Returning a single column value from a 4 column table based on the values of the other 3 columns

SQL query to extract Dates from table based on the value of other column in the same table?

Oracle SQL select final value of one table column like value from other table column that contains value minus 1 digit

SELECT an additional column from another table based on a value between 2 other values

Need column value from sql result based on other column value

Select records from a table based on column value from same table

Function that will select value from SQL table and do a sum based on another column value

How to select row value from given columns based on comparison of other column values in Pandas data frame?

How to select data from a column based on data in other columns and transpose it?

Select field based on other column max value in oracle pl/sql

Adding new column based on SELECT from other table MSSQL

SQL select value from one column based on values in another column

Updating a column value from the value of the other table SQL

Select min value from another table with other column

SELECT with temporary column with value based on other column

How to select data from a table based on 3 other tables?

SQL: New column (categories) based on criteria from other table

Get data from other table based on column with concatenated values

How to SELECT MAX(column) as max_value FROM my_table WHERE some_other_column = "something" in SQL

How to assign a value to a column based on value in other column in R data frame or data table

Select specific row from mysql table based on a column value

Select many intervals from a single table based on column value

Copying data from one pandas dataframe to other based on column value

Sort Excel data based on the dependent value from the other column