我该如何更改查询?

时间:2014-03-28 16:01:49

标签: mysql sql sql-server database oracle

我的查询显示如下数据:

columnA columnB
1         a
1         b
1         c
2         a
2         x
3         z
3         w

但我想显示这样的数据:

columnA columnB
1         a
          b
          c
2         a
          x
3         z
          w

基本上在重复数据上我想显示一个空单元格。 它可以吗? 这是我原来的疑问:

SELECT t1.columnnA, t2.columnB
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id 
GROUP BY t1.columnnA, t2.columnB
ORDER BY t1.columnnA, t2.columnB;

我该如何更改查询? 提前谢谢。

2 个答案:

答案 0 :(得分:2)

对于mysql,您可以使用变量和CASE来检查值是新的还是重复的

SELECT 
CASE WHEN @check != columnA THEN columnA END refcolumnA,
columnB,
@check:=columnA
FROM Table1
,(SELECT @check:=0) t
ORDER BY columnA 

Fiddle Demo

答案 1 :(得分:2)

对于Oracle风味:

SELECT (CASE WHEN rnum = 1 THEN v1.A ELSE NULL END) A, B
  FROM (  SELECT t1.columnA A,
                 t2.columnB B,
                 ROW_NUMBER () OVER (PARTITION BY t1.id ORDER BY t1.columnA)
                    rnum
            FROM t1 INNER JOIN t2 ON (t1.id = t2.id)
        ORDER BY t1.columnA, t2.columnB) v1

SqlFiddle