为1个表中的每个记录选择前1个记录

时间:2014-03-18 04:44:28

标签: sql

我在查询时遇到问题。我需要从具有相同id的表中的每个记录显示前1的结果。这是表格描述。

people

id   | name  |
--------------
01   | john  |
02   | alex  |

job

id   | job       | date start  |
---------------------------------
01   | student   | 1/1/2013    |
01   | employee  | 1/1/2014    |
01   | manager   | 3/18/2014   |
02   | student   | 3/1/2013    |
02   | engineer  | 3/3/2014    |

我需要每个id显示1行的结果。

这是我的SQL查询:

select id,name,job,date_start
from people,job
group by date_start,id,name,job

我从该查询得到了错误的结果。我不知道如何只显示每个id的1条记录。

这是我想要的结果:

查询结果

id | name | job     | date_start |
----------------------------------
01 | john | manager | 3/18/2014  |    -> max date from id='01'
02 | alex | engineer| 3/3/2014   |    -> max date from id='02'

按日期排序,并且每个ID只选择1个结果,并带有最大日期。

我该怎么做?

8 个答案:

答案 0 :(得分:3)

试试这个

WITH j AS
(
    SELECT
        ROW_NUMBER() OVER(PARTITION BY id  ORDER BY date_Start DESC) AS RowNumber,
        id, job, date_start
    FROM job
)
select  p.id, p.name, j.job, j.date_start
from    people p
        inner join j
            on p.id = j.id
            and j.RowNumber = 1

按照你的要求..

  • OVER就像你的选择查询中的分组和排序区域,但它仅由ROW_Number()/ RANK()/ DENSE_RANK()/ NTILE(n)之类的函数使用。 see here for more info
  • PARTITION BY就像拥有一个组一样,在我的例子中,我通过id
  • 对行进行分区或分组
  • ORDER By就像在选择查询中使用order by子句

ROW_Number()是SQL Server中的一个函数,它生成一个从1到N的整数值序列(直到最后一条记录),并且每次PARTITION BY区域更改其值时,我们重置由ROW_Number()函数生成的数字

答案 1 :(得分:2)

试试这个

SELECT S.id,s.name,S.job,s.date_start
FROM
(
  SELECT T2.id,T1.Name,T2.job,T2.date_start
  from people T1 Left Join job T2 ON T1.id =T2.id
  group by T2.date_start,T2.id,T1.name,T2.job
) AS S Inner JOIN 
( 
  SELECT Max(date_start) AS date_start,id From job 
  Group by id
) AS T ON S.date_start = T.date_start
Order By S.ID

Fiddle Demo

输出enter image description here

答案 2 :(得分:2)

试试这个

select id_s,job,date_s,name 
from (select id as id_s, max(date_start) as date_s from job group by id),job j,people p
where id_s=j.id and id_s=p.id and date_s=j.date_start;

希望这能解决问题。

答案 3 :(得分:1)

您可以尝试使用GROUP BY

SELECT p.id, p.name, j.job, MAX(j.date_start)
FROM people p
    INNER JOIN job j
        ON p.id= j.id
GROUP BY p.id, p.name, j.job

答案 4 :(得分:1)

试试这个: -

select p.id,p.name,j.job,j.date_start
from people p left join job j on p.id=j.id
group by p.id order by max(j.date_start) desc;

希望它会对你有所帮助。

答案 5 :(得分:1)

尝试这样

 Select id,name,job,date_start from pepople p,job j,(select id,max(date_start) 
 as maxdate from job) as tbl where p.id=j.id and j.id=tbl.id and 
 j.date_start=tbl.maxdate

答案 6 :(得分:1)

select j.id,name,(Select k.job from job k where k.date_start=
max(j.date_start) and k.id=j.id ) as 'job', max(j.date_start) as 'date_start'
from people p inner join job j on j.id=p.id group by p.name,p.id
having max(j.date_start) order by j.id

答案 7 :(得分:1)

更新了答案

SELECT p.id, p.name, j.job, j.dateStart
FROM tbl_people p
INNER JOIN
(
 SELECT a.id, b.job, a.dateStart
 FROM (
 SELECT id, MAX(date_start) as dateStart
 FROM tbl_job
 GROUP BY id
 ORDER BY MAX(date_start) DESC
) A
INNER JOIN tbl_job B
ON A.id = B.id and A.dateStart = B.date_start
) j
ON p.id = j.id

Sql Fiddle Demo