Clickhouse: How to get join columns?

Created on 27 Feb 2017  Β·  2Comments  Β·  Source: ClickHouse/ClickHouse

e.g.:this is table t1 and t2 data.

:] select * from t1;

SELECT *
FROM t1 

β”Œβ”€x─┬─y───┐
β”‚ 1 β”‚ aaa β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
β”Œβ”€x─┬─y───┐
β”‚ 2 β”‚ bbb β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
β†– Progress: 2.00 rows, 32.00 B (635.66 rows/s., 10.17 KB/s.) 
2 rows in set. Elapsed: 0.003 sec. 

:] select * from t2;

SELECT *
FROM t2 

β”Œβ”€x─┬─y────┐
β”‚ 2 β”‚ bbbb β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
β”Œβ”€x─┬─y────┐
β”‚ 3 β”‚ cccc β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
↑ Progress: 2.00 rows, 34.00 B (638.61 rows/s., 10.86 KB/s.) 
2 rows in set. Elapsed: 0.003 sec. 

I need select t1.x and t2.x,ClickHouse can't support alias . how to fix this problem

:] select x,y,y from (select x,y from t1) as t1  any inner  join (select x,y from t2) as t2 using x;

SELECT 
    x, 
    y, 
    y
FROM 
(
    SELECT 
        x, 
        y
    FROM t1 
) AS t1 
ANY INNER JOIN 
(
    SELECT 
        x, 
        y
    FROM t2 
) AS t2 USING (x)

β”Œβ”€x─┬─y────┬─y────┐
β”‚ 2 β”‚ bbbb β”‚ bbbb β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
β†— Progress: 4.00 rows, 42.00 B (370.81 rows/s., 3.89 KB/s.) 
1 rows in set. Elapsed: 0.011 sec. 

:] 
question

Most helpful comment

Here is a way to do it in ClickHouse:

:) SELECT x, y1, y2 FROM (SELECT x, y AS y1 FROM t1) ALL INNER JOIN (SELECT x, y AS y2 FROM t2) USING (x)

SELECT
    x,
    y1,
    y2
FROM
(
    SELECT
        x,
        y AS y1
    FROM t1
)
ALL INNER JOIN
(
    SELECT
        x,
        y AS y2
    FROM t2
) USING (x)

β”Œβ”€x─┬─y1─┬─y2─┐
β”‚ 1 β”‚ t1 β”‚ t2 β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜

1 rows in set. Elapsed: 0.002 sec.

All 2 comments

:] select x,y,yy from (select x,y,y as yy from t1) as t1  any inner  join (select x,y from t2) as t2 using x;

SELECT 
    x, 
    y, 
    yy
FROM 
(
    SELECT 
        x, 
        y, 
        y AS yy
    FROM t1 
) AS t1 
ANY INNER JOIN 
(
    SELECT 
        x, 
        y
    FROM t2 
) AS t2 USING (x)

β”Œβ”€x─┬─y────┬─yy──┐
β”‚ 2 β”‚ bbbb β”‚ bbb β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
β†’ Progress: 4.00 rows, 66.00 B (418.75 rows/s., 6.91 KB/s.) 
1 rows in set. Elapsed: 0.010 sec. 

:] 

My god...is very trouble. I need query this column again add alias .
This is very diff standard SQL

Here is a way to do it in ClickHouse:

:) SELECT x, y1, y2 FROM (SELECT x, y AS y1 FROM t1) ALL INNER JOIN (SELECT x, y AS y2 FROM t2) USING (x)

SELECT
    x,
    y1,
    y2
FROM
(
    SELECT
        x,
        y AS y1
    FROM t1
)
ALL INNER JOIN
(
    SELECT
        x,
        y AS y2
    FROM t2
) USING (x)

β”Œβ”€x─┬─y1─┬─y2─┐
β”‚ 1 β”‚ t1 β”‚ t2 β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜

1 rows in set. Elapsed: 0.002 sec.
Was this page helpful?
0 / 5 - 0 ratings