---
title: "SQL行合并"
author: "jupiterpan"
date: "2010-04-15T20:32:49Z"
tags: []
categories: ["编程语言"]
slug: "sql"
draft: false
lastmod: "2010-04-15T20:32:49Z"
id: 644946
url: "https://jstyle.iteye.com/blog/644946"
---

mysql: groupconcat

mysql> SELECT student_name,

    ->     GROUP_CONCAT(test_score)

    ->     FROM student

    ->     GROUP BY student_name;

Or: 

mysql> SELECT student_name,

    ->     GROUP_CONCAT(DISTINCT test_score

    ->               ORDER BY test_score DESC SEPARATOR ' ')

    ->     FROM student

    ->     GROUP BY student_name;


oracle: sys_connect_by_path()
如：select name, ltrim(sys_connect_by_path(email,','),',') email from(
select name,email,
row_number() over(partition by name order by email) rn,
count(*) over(partition by name) cnt
from student 
) where level = cnt
start with rn = 1
connect by prior name = name and prior rn + 1 = rn