如果我有相同的信息,请选择一个特定的行

时间:2022-09-23 01:06:15

I have a table with a data as bellow :

我有一个数据表如下:

+--------+----------+-------+------------+--------------+
| month  |   code   |  type   |      date       |    PersonID  |
+--------+----------+-------+------------+--------------+
| 201501 |   178954 |     3 | 2014-12-3  |           10 |
| 201501 |   178954 |     3 | 2014-12-3  |           10 |
| 201501 |   178955 |     2 | 2014-12-13 |           10 |
| 201501 |   178955 |     2 | 2014-12-13 |           10 |
| 201501 |   178956 |     2 | 2014-12-11 |           10 |
| 201501 |   178958 |     1 | 2014-12-10 |           10 |
| 201501 |   178959 |     2 | 2014-12-12 |           15 |
| 201501 |   178959 |     2 | 2014-12-12 |           15 |
| 201501 |   178954 |     1 | 2014-12-11 |           13 |
| 201501 |   178954 |     1 | 2014-12-11 |           13 |
+--------+----------+-------+------------+--------------+

In my first 6 lines i have the same PersonID in the same Month What i want if i have the same personID in the same Month i want to select the person who have the type is 2 with the recent date in my case the output will be like as bellow:

在我的前6行中,我在同一个月有相同的PersonID我想要的是如果我在同一个月有相同的personID我想选择类型为2的人,在我的情况下最近的日期输出将是像下面一样:

+--------+--------+------+------------+----------+
| month   |  code    | type|    date       | PersonID |
+--------+--------+------+------------+----------+
| 201501 | 178955 |    2 | 2014-12-13 |       10 |
| 201501 | 178959 |    2 | 2014-12-12 |       15 |
| 201501 | 178954 |    2 | 2014-12-11 |       13 |
+--------+--------+------+------------+----------+

Also if they are some duplicate rows i don't want to display it

此外,如果他们是一些重复的行,我不想显示它

They are any solution to that ?

他们可以解决这个问题吗?

1 个解决方案

#1


0  

Simply use GROUP BY: https://msdn.microsoft.com/de-de/library/ms177673(v=sql.120).aspx

只需使用GROUP BY:https://msdn.microsoft.com/de-de/library/ms177673(v = sql.120).aspx

SELECT mont, code, ... FROM tabelname GROUP BY PersonID, date, ...

Note that you have to specifiy all columns in the group by.

请注意,您必须指定组中的所有列。

#1


0  

Simply use GROUP BY: https://msdn.microsoft.com/de-de/library/ms177673(v=sql.120).aspx

只需使用GROUP BY:https://msdn.microsoft.com/de-de/library/ms177673(v = sql.120).aspx

SELECT mont, code, ... FROM tabelname GROUP BY PersonID, date, ...

Note that you have to specifiy all columns in the group by.

请注意,您必须指定组中的所有列。