我想知道是否可以在sqlite中使用SELECT语句将两行中的数据合并为一个,类似于下面的sql Server论坛中的建议.
考虑下面基于sql Server的场景(取自http://forums.aspfree.com/microsoft-sql-server-14/merge-the-two-rows-in-one-row-245550.html)
鉴于有一张桌子
EMP
ID |名称|
1 | x |
1 | P |
2 | y |
2 |问|
3 | W |
我们希望select语句中的结果数据输出:
Emp_Data
Id |名字-1 |名字-2 |
1 | x | P |
2 | y |问|
3 | w | |
- SELECT
- a.ID,[Name-1] = ISNULL((
- SELECT TOP 1 Name
- FROM emp
- WHERE ID = a.ID),''),[Name-2] = ISNULL((
- SELECT TOP 1 b.Name
- FROM emp b
- WHERE b.ID = a.ID
- AND Name NOT IN(
- SELECT TOP 1 Name
- FROM emp
- WHERE ID = b.ID
- )),'')
- FROM emp a
- GROUP BY a.ID
使用sqlite可以生成列[Name-1]& [Name-2]使用嵌套的SELECT语句,就像我们在sql Server中可以做的那样?
- SELECT
- a.ID,COALESCE(a.Name,'') as "Name-1",COALESCE((SELECT b.Name FROM Emp b
- WHERE b.ID = a.ID
- AND b.rowid != a.rowid LIMIT 1),'') as "Name-2"
- FROM emp a
- GROUP BY a.ID