【问题标题】:Select the first row from the join myself从加入自己中选择第一行
【发布时间】:2020-01-10 20:02:10
【问题描述】:

我要求的是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;

表格示例1中的此数据:

    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

【问题讨论】:

    标签: sql sql-server database join


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      根据您的示例数据,您需要以下窗口函数:

      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 问题,因为它鼓励人们在已经为他们完成打字工作时回答。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        • 2020-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        相关资源
        最近更新 更多