开发者

collapsing two columns in one in sql?

开发者 https://www.devze.com 2022-12-18 00:47 出处:网络
Say I have a table like that: x1|y1| 0 |1 | 2 |4 | 3 |2 | I know I can do something like that: SELECT a.x1 AS .., a.y1 AS ..,

Say I have a table like that:

|x1|y1|

|0 |1 |

|2 |4 |

|3 |2 |

I know I can do something like that:

SELECT a.x1 AS .., a.y1 AS .., 
       b.x1 AS .., b.y1 AS .., 
       c.x1 AS .., c.y1 AS .., 
       d.x1 AS .., d.y1 AS .. 
FROM xytable a, xytable 开发者_如何学Cb, xytable c, xytable d 
WHERE ...

In my result table, however, I would like to output a.x1 and a.y1 together, as a a tuple under one column. In other words, I want to have:

|c1          |c2            |c3             |c4           |

(a.x1, a.y1) | (a.x2, a.y2) |  (a.x3, a.y3) | (a.x4, a.y4)

Is it possible?


If you are using MySQL, you could use the CONCAT function:

SELECT 
     CONCAT('(', a.x1, ', ', a.y1, ')') AS c1
FROM 
    xytable a, xytable b, xytable c, xytable d 
WHERE 
    ...

In SQL Server, you can concatenate with the + operator to concatenate strings:

SELECT 
    '(' + a.x1 + ', ' + a.y1 + ')' AS c1
FROM 
    xytable a, xytable b, xytable c, xytable d 
WHERE 
    ...


As a character string?

You can do something like

SELECT '(' + convert(varchar, x1) + ',' + convert(varchar, y1) + ')' as "col"
0

精彩评论

暂无评论...
验证码 换一张
取 消