我正在运行以下查询以获取数据:
SELECT DISTINCT student_id, student_name from class_students where city = 'foobar' and student_id <> 0;
我得到的数据如下:
student_id | student_name
------------+----------------------------------------
41990 | James
48220 | John
00000 |
00000 | lkjkj
00000 | random name
00000 | somethingelse
我更想找回这些数据:
student_id | student_name
------------+----------------------------------------
41990 | James
48220 | John
00000 | Name-doesnt-exist
也就是说把所有的 00000
一行中的名称 NA
3条答案
按热度按时间9rnv2umw1#
icomxhvb2#
如果存在多个名称,则标记它。
您还可以从结果中完全消除这些组:
eoxn13cs3#
你可以试着用
row_number()
```select student_id,coalesce(student_name,'Name-doesnt-exist') as name
from
(
SELECT student_id, student_name,row_number() over(partition by student_id order by case when student_name is null then 1 else 2 end) as rn
from class_students where city = 'foobar' and student_id <> 0
)A where rn=1