如何获取所有列的所有不同值但保持原始 table 结构?
How to get all distinct values of all columns but keep the original table structure?
我有一个 table,在 HANA.The 中有 5 列 table 名字是学生,下面是字段-姓名、年龄、地址、性别,score.I 需要获取每个 columns.For 的不同值,例如,table 是-
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.None 可用的讨论似乎是 useful.I 不想要 UNION,因为它将所有值组合到结果中的一列中。
我没有访问 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 |
+------+-----+---------+--------+-------+---+
我有一个 table,在 HANA.The 中有 5 列 table 名字是学生,下面是字段-姓名、年龄、地址、性别,score.I 需要获取每个 columns.For 的不同值,例如,table 是-
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.None 可用的讨论似乎是 useful.I 不想要 UNION,因为它将所有值组合到结果中的一列中。
我没有访问 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 |
+------+-----+---------+--------+-------+---+