Indu
Indu

Reputation: 1

How to Combine multiple data sets based on a single column horizontally

Below datasets are written within "WITH" Clause

Date Data 1
14-Nov abc
15-Nov def
Date Data 2
14-Nov 123
15-Nov 456
Date Data 3
14-Nov !@#
15-Nov $%^

Need the final result as below

Date Data 1 Data 2 Data 3
14-Nov abc 123 !@#
15-Nov def 456 $%^

Above result can be achieved through join , but the problem I am facing is that these individual result sets are part of each data set within a query using "WITH" clause.

For one date it is working fine , unable to collate for multiple dates.

Any view on this.


Got the results using

WITH
cte1 AS (
    SELECT '14-Nov' AS Date, 'abc' AS Data1
    UNION ALL
    SELECT '15-Nov', 'def'
),
cte2 AS (
    SELECT '14-Nov' AS Date, '123' AS Data2
    UNION ALL
    SELECT '15-Nov', '456'
),
cte3 AS (
    SELECT '14-Nov' AS Date, '!@#' AS Data3
    UNION ALL
    SELECT '15-Nov', '$%^'
)
SELECT
    cte1.Date,
    cte1.Data1,
    cte2.Data2,
    cte3.Data3
FROM
    cte1
JOIN
    cte2 ON cte1.Date = cte2.Date
JOIN
    cte3 ON cte1.Date = cte3.Date;

Upvotes: 0

Views: 63

Answers (0)

Related Questions