0

我的这个查询从 shipwynum 151513 的转售表中获取 6 条记录:-

 select re.recdat,
        re.resaleid,
        IF (re.benownsal != '', owsal.ownshortnam, if ((re.grptypsal != '' &&
         re.bogrpidsal != ''), if (bosal.bogrpshort != '', bosal.bogrpshort,
          bosal.bogrpnam), cou1.nation)) as seller,
        IF (re.benownpur != '', owpur.ownshortnam, if ((re.grptyppur != '' &&
         re.bogrpidpur != ''), if (bopur.bogrpshort != '', bopur.bogrpshort,
          bopur.bogrpnam), cou.nation)) as buyer,
        re.benownpur,
        re.grptyppur,
        re.bogrpidpur,
        re.statuscod,
        re.showinob,
        re.benownsal,
        re.grptypsal,
        re.bogrpidsal
 from resale as re
      left join owner as owpur on owpur.ownwynum = re.benownpur
      left join owner as owsal on owsal.ownwynum = re.benownsal
      left join bogroup as bopur on bopur.bogrpid = re.bogrpidpur
      left join bogroup as bosal on bosal.bogrpid = re.bogrpidsal
      left join country as cou on cou.coucod = re.buynation
      left join country as cou1 on cou1.coucod = re.selnation
 where re.shipwynum = '151513' and
       re.deleted = 'N' 
 order by re.saltyp desc,
          re.recdat

在这里,我添加了从上面查询 shipwynum 15153 获得的示例数据屏幕截图:-

在此处输入图像描述 当我添加限制子句时LIMIT 1,它会获得我期望的第一个记录。但它只适用于一艘船有shipwynum = 151513。我想为每艘船获得第一个记录。

我所做的更改是为了获得每艘船的此类记录。

任何帮助将不胜感激。

谢谢。

4

2 回答 2

0

您可以将查询用作内部查询并将其分组到外部。尝试您的查询:

select abc.recdat, abc.resaleid, abc.seller, abc.buyer,abc.benownpur,
abc.grptyppur, abc.bogrpidpur, abc.statuscod, abc.showinob, abc.benownsal, abc.grptypsal, abc.bogrpidsal

  from ( select re.recdat,
        re.resaleid,
        IF (re.benownsal != '', owsal.ownshortnam, if ((re.grptypsal != '' &&
         re.bogrpidsal != ''), if (bosal.bogrpshort != '', bosal.bogrpshort,
          bosal.bogrpnam), cou1.nation)) as seller,
        IF (re.benownpur != '', owpur.ownshortnam, if ((re.grptyppur != '' &&
         re.bogrpidpur != ''), if (bopur.bogrpshort != '', bopur.bogrpshort,
          bopur.bogrpnam), cou.nation)) as buyer,
        re.benownpur,
        re.grptyppur,
        re.bogrpidpur,
        re.statuscod,
        re.showinob,
        re.benownsal,
        re.grptypsal,
        re.bogrpidsal,
        re.shipwynum
 from resale as re
      left join owner as owpur on owpur.ownwynum = re.benownpur
      left join owner as owsal on owsal.ownwynum = re.benownsal
      left join bogroup as bopur on bopur.bogrpid = re.bogrpidpur
      left join bogroup as bosal on bosal.bogrpid = re.bogrpidsal
      left join country as cou on cou.coucod = re.buynation
      left join country as cou1 on cou1.coucod = re.selnation
 where re.shipwynum = '151513' and
       re.deleted = 'N' 
 order by re.saltyp desc,
          re.recdat)abc group by abc.shipwynum 
于 2013-04-15T11:24:25.663 回答
0

您可以使用此查询 - 目的是为每个 shipwynum 获取 [n] 个转售标识,其中 [n=2] 是

SELECT (SUBSTRING_INDEX(GROUP_CONCAT(CONVERT(resaleid, CHAR(8)) ORDER BY resaleid DESC), ',', 2)) AS ID FROM resale WHERE deleted = 'N' GROUP BY shipwynum

请注意,GROUP_CONCAT 结果被截断为 max len 1024 (group_concat_max_len)。这可能存在性能问题,因此请随时根据需要进行更新和优化。

其他建议是设置排名,但不设置如何为您工作。您可以查看排名:http ://www.oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions.php#rank

希望这可以帮助。

SELECT  re.recdat,
        re.resaleid,
        IF (re.benownsal != '', owsal.ownshortnam, if ((re.grptypsal != '' && re.bogrpidsal != ''), if (bosal.bogrpshort != '', bosal.bogrpshort, bosal.bogrpnam), cou1.nation)) as seller,
        IF (re.benownpur != '', owpur.ownshortnam, if ((re.grptyppur != '' && re.bogrpidpur != ''), if (bopur.bogrpshort != '', bopur.bogrpshort, bopur.bogrpnam), cou.nation)) as buyer,
        re.benownpur,
        re.grptyppur,
        re.bogrpidpur,
        re.statuscod,
        re.showinob,
        re.benownsal,
        re.grptypsal,
        re.bogrpidsal

FROM resale AS re
      left join owner as owpur on owpur.ownwynum = re.benownpur
      left join owner as owsal on owsal.ownwynum = re.benownsal
      left join bogroup as bopur on bopur.bogrpid = re.bogrpidpur
      left join bogroup as bosal on bosal.bogrpid = re.bogrpidsal
      left join country as cou on cou.coucod = re.buynation
      left join country as cou1 on cou1.coucod = re.selnation 

WHERE LOCATE (re.resaleid, 
(
    SELECT GROUP_CONCAT(it.resaleid) AS ids
        FROM (
                SELECT (SUBSTRING_INDEX(GROUP_CONCAT(CONVERT(resaleid, CHAR(8)) ORDER BY resaleid DESC), ',', 2)) AS ID FROM resale WHERE deleted = 'N' GROUP BY shipwynum
            ) it
)) <> 0 
    /* AND re.deleted = 'N' */
 order by re.saltyp desc,
          re.recdat
于 2013-04-16T09:34:04.913 回答