[英]How to filter a Query Sql so that it shows only one value when more rows of the same table have two identical values?
Example: I have a table "person" with 6 columns: id, name, lastname, age, job, genre.示例:我有一个包含 6 列的“人”表:id、name、lastname、age、job、genre。 I need to make a query SELECT so that it shows only one value when a person has both the same age and job.
我需要查询 SELECT 以便当一个人具有相同的年龄和工作时它只显示一个值。
Person:人:
+--------+------------+--------+--------+---------+
| id | name |lastname| age | job |
+--------+------------+--------+--------+---------+
| 101 | francesco | pieri | 18 | soldier |
| 102 | martin | gofman | 25 | grocer |
| 103 | paulina | arani | 44 | engineer|
| 104 | george | gobelt | 25 | grocer |
| 105 | marcus | derona | 18 | soldier |
+--------+------------+--------+--------+---------+
I want the result to show up something like this我希望结果显示像这样
+--------+------------+--------+--------+---------+
| id | name |lastname| age | job |
+--------+------------+--------+--------+---------+
| 101 | francesco | pieri | 18 | soldier |
| 102 | martin | gofman | 25 | grocer |
| 103 | paulina | arani | 44 | engineer|
+--------+------------+--------+--------+---------+
I don't care about the order or the way the row are grouped, just need that a row with same age and job is shown ONCE.我不在乎顺序或行的分组方式,只需要显示一次具有相同年龄和工作的行。
I tried something with partition by and row number, but only found examples with conditions on a single field and I don't know how to merge it with my personal case.我尝试了分区依据和行号,但只找到了单个字段条件的示例,我不知道如何将它与我的个人案例合并。
You can use row_number()
for this:您可以为此使用
row_number()
:
select t.*
from (select t.*, row_number() over (partition by age, job order by id) as seqnum
from t
) t
where seqnum = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.