简体   繁体   English

如何过滤查询 Sql 以便在同一表的多个行具有两个相同值时仅显示一个值?

[英]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.

相关问题 如何从表中选择具有相同但随机值的列中的行,而只通过一个查询却不知道该值? - How to select rows from a table with identical but random values in a column without knowing that value with only one query? 使用SQL查询检测两个或多个连续行是否具有相同条目 - Detect if two or more consecutive rows have the same entry with a sql query 当同一表的另一列中存在多于1个不同值时,用于从一列中计算不同值的SQL查询 - SQL Query for Counting Distinct Values From One Column When More Than 1 Distinct Value exists in Another Column in the same table 如何计算两列中具有相同值的行,并仅返回count计数的行 - How to count rows that have the same values in two columns and return only rows where count<N(SQL)? SQL-两行只有一个差异列值。 如何保留一个并过滤另一个? - SQL - Two rows with only one differet column value. How to keep one and filter the other? 仅选择在SQL Server中具有一个或多个特定值的值 - Select only the values that have one or more specific value in SQL Server SQL查询2行,并且仅显示一次具有相同值的行 - SQL Query 2 rows and display only once that have same value SQL Server:连接两个表,并且仅获取一个表中没有相同行的数据 - SQL Server: Join two tables and only get data of one table without identical rows 当两个或多个连续行具有相同的状态时,如何 select 一行 - How to select one row when two or more consecutive rows have same statuses SQL 如何过滤具有多个唯一值的另一列的值的表 - SQL How to filter table with values having more than one unique value of another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM