如何获取所有列的所有不同值但保留原始表结构?

时间:2016-12-14 05:45:16

标签: sql odata hana

我在HANA中有一个包含5列的表。表名是学生,以下是字段名称,年龄,地址,性别,分数。我需要获取每列的不同值。例如,表格是 -

      Name  Age  Address  Gender  Score
      A      1     abc      F      10
      C      3     abc      M      10
      B      2     def      M      5
      C      3     ghi      F      10
      D      2     def      M      5

预期结果是 -

      Name  Age  Address  Gender  Score
      A      1     abc      F      10
      B      2     def      M      5
      C      3     ghi            

请建议如何使用视图或odata实现此目的。现有的讨论似乎没有用。我不想要UNION,因为它将所有值组合到结果中的一列中。

1 个答案:

答案 0 :(得分:2)

我无法访问Hana,但这可能会起作用

select      min (name)    as name
           ,min (Age)     as Age
           ,min (Address) as Address
           ,min (Gender)  as Gender
           ,min (Score)   as Score

from        (           select Name ,Age  ,Address ,Gender ,Score ,1000000000  as n                     from students where 1=2

            union all   select Name ,null ,null    ,null   ,null  ,row_number() over (order by Name)    from students group by Name
            union all   select null ,Age  ,null    ,null   ,null  ,row_number() over (order by Age)     from students group by Age
            union all   select null ,null ,Address ,null   ,null  ,row_number() over (order by Address) from students group by Address
            union all   select null ,null ,null    ,Gender ,null  ,row_number() over (order by Gender)  from students group by Gender
            union all   select null ,null ,null    ,null   ,Score ,row_number() over (order by Score)   from students group by Score
            ) s

group by    n   

order by    n
;
+------+-----+---------+--------+--------+
| name | age | address | gender | score  |
+------+-----+---------+--------+--------+
| A    | 1   | abc     | F      | 5      |
+------+-----+---------+--------+--------+
| B    | 2   | def     | M      | 10     |
+------+-----+---------+--------+--------+
| C    | 3   | ghi     |        |        |
+------+-----+---------+--------+--------+

出于教育目的,以下是内部查询的结果:

+------+-----+---------+--------+-------+---+
| name | age | address | gender | score | n |
+------+-----+---------+--------+-------+---+
| A    |     |         |        |       | 1 |
+------+-----+---------+--------+-------+---+
| B    |     |         |        |       | 2 |
+------+-----+---------+--------+-------+---+
| C    |     |         |        |       | 3 |
+------+-----+---------+--------+-------+---+
|      | 1   |         |        |       | 1 |
+------+-----+---------+--------+-------+---+
|      | 2   |         |        |       | 2 |
+------+-----+---------+--------+-------+---+
|      | 3   |         |        |       | 3 |
+------+-----+---------+--------+-------+---+
|      |     | abc     |        |       | 1 |
+------+-----+---------+--------+-------+---+
|      |     | def     |        |       | 2 |
+------+-----+---------+--------+-------+---+
|      |     | ghi     |        |       | 3 |
+------+-----+---------+--------+-------+---+
|      |     |         | F      |       | 1 |
+------+-----+---------+--------+-------+---+
|      |     |         | M      |       | 2 |
+------+-----+---------+--------+-------+---+
|      |     |         |        | 5     | 1 |
+------+-----+---------+--------+-------+---+
|      |     |         |        | 10    | 2 |
+------+-----+---------+--------+-------+---+