How to join CTE query with another table in SQL Server 2008

tang

I have created a CTE query, and I manage to join the CTE when the data in CTE is match with another table.

For example, this is how my CTE query result looks like:

ID     NAME    REG      INV      CUS       BR
-----------------------------------------------
1     A0001   R0001    I0001    C0001     B0001
2     A0002   R0002    I0002    C0002     B0002
3     A0003   R0003    I0003    C0003     B0003
4     A0004   R0004    I0004    C0004     B0004

And this is the table I manage to join it to:

ID    NAME     CUS 
---------------------
1     TEST1   C0001
2     TEST2   C0002
3     TEST3   C0003
4     TEST4   C0004

And this is my code to select CTE query

;WITH BaseQuery AS 
(
    SELECT  
        Id, Name, Comment, 
        CONVERT(XML, '<root><item>' 
                + REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE((SELECT Comment AS '*' FOR XML PATH('')), 
                            'Reg:', 
                            '</item><item type="Reg">'), 
                        'Inv:', 
                        '</item><item type="Inv">'), 
                    'Cus:', 
                    '</item><item type="Cus">'), 
                'Br:', 
                '</item><item type="Br">') + '</item></root>') CommentAsXml
    FROM    
        GenTransaction
), Query (
SELECT  
    Id, Name, Comment, 
    Reg = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Reg"])[1]', 'VARCHAR(11)'))),
    Inv = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Inv"])[1]', 'VARCHAR(11)'))),
    Cus = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Cus"])[1]', 'VARCHAR(11)'))),
    Br  = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Br"])[1]', 'VARCHAR(11)'))) 
FROM    
    BaseQuery bq
)

select ArCustomer.Name, Query.Cus from ArCustomer left join Query on ArCustomer.Customer = Query.ArCustomer.Customer order by ArCustomer.Name

Bogdan Sahlean

[ 1 ] Using two CTEs (BaseQuery, Query and join between table and Query):

;WITH BaseQuery AS 
(
    SELECT  
        Id, Name, Comment, 
        CONVERT(XML, '<root><item>' 
                + REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE((SELECT Comment AS '*' FOR XML PATH('')), 
                            'Reg:', 
                            '</item><item type="Reg">'), 
                        'Inv:', 
                        '</item><item type="Inv">'), 
                    'Cus:', 
                    '</item><item type="Cus">'), 
                'Br:', 
                '</item><item type="Br">') + '</item></root>') CommentAsXml
    FROM    
        GenTransaction
), Query (
SELECT  
    Id, Name, Comment, 
    Reg = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Reg"])[1]', 'VARCHAR(11)'))),
    Inv = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Inv"])[1]', 'VARCHAR(11)'))),
    Cus = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Cus"])[1]', 'VARCHAR(11)'))),
    Br  = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Br"])[1]', 'VARCHAR(11)'))) 
FROM    
    BaseQuery bq
)
SELECT ...
FROM Table1 t1 INNER/LEFT OUTER/... JOIN Query q ON ... join condition ... -- Query represents the second CTE
ORDER BY ...

[ 2 ] Second solution is based also on two CTEs (BaseQuery and Query) but instead of JOIN is using APPLY operator thus:

;WITH BaseQuery AS 
(
    SELECT  
        Id, Name, Comment, 
        CONVERT(XML, '<root><item>' 
                + REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE((SELECT Comment AS '*' FOR XML PATH('')), 
                            'Reg:', 
                            '</item><item type="Reg">'), 
                        'Inv:', 
                        '</item><item type="Inv">'), 
                    'Cus:', 
                    '</item><item type="Cus">'), 
                'Br:', 
                '</item><item type="Br">') + '</item></root>') CommentAsXml
    FROM    
        GenTransaction
), Query (
SELECT  
    Id, Name, Comment, 
    Reg = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Reg"])[1]', 'VARCHAR(11)'))),
    Inv = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Inv"])[1]', 'VARCHAR(11)'))),
    Cus = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Cus"])[1]', 'VARCHAR(11)'))),
    Br  = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Br"])[1]', 'VARCHAR(11)'))) 
FROM    
    BaseQuery bq
)
SELECT ... t1.Col1 ... x.Col2 ...
FROM Table1 t1 
OUTER/CROSS APPLY (
    SELECT ...
    FROM Query q 
    WHERE ... join condition ... -- Query represents the second CTE
) x
ORDER BY ...

[ 3 ] Another solution is to insert those rows (extracted from Comment column) into a temp table (#Results) and then JOIN temp (#Results) table with another table (Table1):

;WITH BaseQuery AS 
(
    SELECT  
        Id, Name, Comment, 
        CONVERT(XML, '<root><item>' 
                + REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE((SELECT Comment AS '*' FOR XML PATH('')), 
                            'Reg:', 
                            '</item><item type="Reg">'), 
                        'Inv:', 
                        '</item><item type="Inv">'), 
                    'Cus:', 
                    '</item><item type="Cus">'), 
                'Br:', 
                '</item><item type="Br">') + '</item></root>') CommentAsXml
    FROM    
        GenTransaction
)
SELECT  
    Id, Name, Comment, 
    Reg = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Reg"])[1]', 'VARCHAR(11)'))),
    Inv = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Inv"])[1]', 'VARCHAR(11)'))),
    Cus = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Cus"])[1]', 'VARCHAR(11)'))),
    Br  = LTRIM(RTRIM(bq.CommentAsXml.value('(root/item[@type="Br"])[1]', 'VARCHAR(11)'))) 
INTO #Results
FROM    
    BaseQuery bq; -- ORDER BY here should be used within final query

SELECT ...
FROM Table1 t1 INNER/LEFT OUTER/... JOIN #Results r ON ... join condition build using t1./r... ...
ORDER BY ...


Id, Name, Comment

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL Server 2008 Query - table join

SQL server 2008 query move table data from one to another

SQL Server : how to join record not in another table?

How to generate JSON in SQL Server 2008 and insert into another table

Improve WITH CTE Query Optimization SQL Server 2008 R2

Self-join a table in SQL Server 2008

How to create a table from select query result in SQL Server 2008

Alter table query plan for sql server 2008

How to pass join sql query table result to another method?

SQL Server : getting DISTINCT TOP list from a another CTE query

How to Compare information schema column with Table Column in Sql Server2008 using inner Join?

SQL Server 2008: Query performance using inner join

Select last 5 rows in join query SQL Server 2008

Compare each row of a table to another table in SQL Server 2008

How to write this query in SQL server 2008

How to write this query in SQL Server 2008?

SQL Server Join a result with another table

How to use result of query to select in to another table in Sql Server

How to truncate table with SQL Server 2008?

SQL Server - Join between a table and a Where IN query

Error in executing dynamic SQL Query with table variable in SQL Server 2008

Recursive CTE query in SQL Server

How to get few column names among all columns in the one table with query in sql server 2008

MySQL CTE Recursive With Join Another Table

Move a varbinary from one table to another with a check in SQL Server 2008

Loop Join in SQL Server 2008

SQL Server 2008 PIVOT and JOIN

Pivot table query using SQL Server 2008 R2

Using query export SQL Server 2008 table to Excel Sheet