从我自己的联接中选择第一行

时间:2019-09-09 03:18:24

标签: sql sql-server database join

我已请求id从表中查询数据的第一行,但是表中的数据有很多id

这是我的SQL查询

SELECT DISTINCT 
    mb.gid, mbt.model, mbt.berths, mbt.berthsext,
    mbt.year, mbt.length, mbt.cabins, mbt.cabinsext, mbt.heads,
    mbt.sedna_id_model
FROM 
    boat mb
INNER JOIN
    boat mbt ON mbt.gid = (SELECT TOP 1 gid FROM boat WHERE gid = mb.gid) 
WHERE 
    1 = 1 
ORDER BY 
    mbt.model;

此数据在表example1中:

    id | MODEL | berths|berthsext| year  | length| cabins | heads
    ---+-------+-------+---------+-------+-------+--------+------
     1 | Joe    | 5    | 5       | 5     | 5     | 5      | 5
     2 | Sally  | 3    | 3       | 3     | 3     | 3      | 3
     1 | Joe    | 2    | 2       | 2     | 2     | 2      | 2
     4 | Sally  | 1    | 1       | 1     | 1     | 1      | 1
     4 | Sally  | 1    | 1       | 1     | 1     | 1      | 1
     4 | Sally  | 1    | 1       | 1     | 1     | 1      | 1
     4 | Sally  | 1    | 1       | 1     | 1     | 1      | 1
     4 | Sally  | 1    | 1       | 1     | 1     | 1      | 1
     6 | Sally  | 5    | 5       | 6     | 8     | 7      | 8

图片上会有很多gid,但我需要查询第一行

这些是我想要的results

id | MODEL  | berths|berthsext| year  | length| cabins| heads
---+--------+-------+---------+-------+-------+-------+------
 1 | Joe    | 5     | 5       | 5     | 5     | 5     | 5
 2 | Sally  | 3     | 3       | 3     | 3     | 3     | 3
 4 | Sally  | 1     | 1       | 1     | 1     | 1     | 1
 6 | Sally  | 5     | 5       | 6     | 8     | 7     | 8

2 个答案:

答案 0 :(得分:1)

您似乎正在尝试获得ASC年度订单的第一笔记录。您可以使用row_number()

SELECT * FROM (
    SELECT mb.gid , mbt.model,mbt.berths,mbt.berthsext,mbt.year,mbt.length,mbt.cabins,mbt.cabinsext,mbt.heads,mbt.sedna_id_model,
        ROW_NUMBER() OVER (PARTITION BY mb.gid ORDER BY mbt.year ASC) as rn
    FROM MMK_boat mb
    inner join mmk_boat mbt on mbt.gid = (select top 1 gid from mmk_boat  where gid = mb.gid) 
) t1 
WHERE t1.rn = 1 order by t1.model

答案 1 :(得分:1)

基于示例数据,您需要以下窗口功能:

declare @Example1 table (id int, model varchar(6), berths int, berthsnext int, [year] int, [length] int, cabins int, heads int)

insert into @Example1 (id, model, berths, berthsnext, [year], [length], cabins, heads)
  select 1 , 'Joe'    , 5    , 5       , 5     , 5     , 5     , 5 union all
  select 2 , 'Sally'  , 3    , 3       , 3     , 3     , 3     , 3 union all
  select 1 , 'Joe'    , 2    , 2       , 2     , 2     , 2     , 2 union all
  select 4 , 'Sally'  , 1    , 1       , 1     , 1     , 1     , 1 union all
  select 4 , 'Sally'  , 1    , 1       , 1     , 1     , 1     , 1 union all
  select 4 , 'Sally'  , 1    , 1       , 1     , 1     , 1     , 1 union all
  select 4 , 'Sally'  , 1    , 1       , 1     , 1     , 1     , 1 union all
  select 4 , 'Sally'  , 1    , 1       , 1     , 1     , 1     , 1 union all
  select 6 , 'Sally'  , 5    , 5       , 6     , 8     , 7     , 8

select id, model, berths, berthsnext, [year], [length], cabins, heads
from (
  select id, model, berths, berthsnext, [year], [length], cabins, heads
    , row_number() over (partition by model, id order by id desc) row#
  from @Example1 -- Obviously replace this by your joined query
) X
where row# = 1

哪个会产生:

id  model   berths  berthsnext  year    length  cabins  heads
1   Joe     2       2           2       2       2       2
2   Sally   3       3           3       3       3       3
4   Sally   1       1           1       1       1       1
6   Sally   5       5           6       8       7       8

请以这种格式发布任何将来的SQL问题,因为它会鼓励人们在已经为他们完成键入工作后回答。