SQL:我需要为表中的每组数据选择具有max列的行

时间:2019-06-03 08:51:53

标签: mysql

我有一个这样的联接结果表(带有更多列):

+------------+-----------+-----------+---------------+-----------------+
| salesid    | itemid    | confirmid | confirmdocnum | createddatetime |
+------------+-----------+-----------+---------------+-----------------+
| a0001      | foo       |        21 |      2501-1   |   2019-01-02    |
| a0001      | bar       |        21 |      2501-1   |   2019-01-02    |
| a0001      | baz       |        22 |      2501-2   |   2019-01-03    |
| a0001      | foo       |        23 |      2501-3   |   2019-01-04    |
| a0001      | bar       |        23 |      2501-3   |   2019-01-04    |
| a0002      | foo       |        24 |      2502-1   |   2019-01-01    |
| a0002      | bar       |        24 |      2502-1   |   2019-01-01    |
| a0002      | bar       |        25 |      2502-2   |   2019-01-03    |
| a0003      | foo       |        26 |      2503-1   |   2019-01-04    |
| a0003      | bar       |        27 |      2503-2   |   2019-01-07    |
| a0004      | baz       |        28 |      2504-1   |   2019-01-05    |
+------------+-----------+-----------+---------------+-----------------+

我需要检索以下内容:

+------------+-----------+-----------+---------------+-----------------+
| salesid    | itemid    | confirmid | confirmdocnum | createddatetime |
+------------+-----------+-----------+---------------+-----------------+
| a0001      | foo       |        23 |      2501-3   |   2019-01-04    |
| a0001      | bar       |        23 |      2501-3   |   2019-01-04    |
| a0002      | bar       |        25 |      2502-2   |   2019-01-03    |
| a0003      | bar       |        27 |      2503-2   |   2019-01-07    |
| a0004      | baz       |        28 |      2504-1   |   2019-01-05    |
+------------+-----------+-----------+---------------+-----------------+

我需要为每个salesid用最新的Confirmdocnum的所有行。最新的Confirmdocnum可以类似于'%-1','%-2','%-3',...,'%-n'

所有具有最新Confirmdocnum的行始终具有最新的createddatetime。

我用来获取第一个表的联接是:

SELECT cct.salesid, 
       cct.itemid, 
       ccj.confirmid, 
       ccj.confirmdocnum, 
       ccj.createddatetime
FROM AxDynamics.dbo.custconfirmjour AS ccj
LEFT JOIN AxDynamics.dbo.custconfirmtrans AS cct
ON cct.salesid=ccj.salesid
  AND cct.confirmid=ccj.confirmid
  AND cct.confirmdate=ccj.confirmdate
WHERE ccj.CREATEDDATETIME >= CONVERT (DATE,'2019/01/01')

但是我不知道如何只选择我需要的行。我简化了几个额外的列。这些额外的列具有我真正需要的数据。我尝试了GROUP BY的近似值,但是无法获取所需行的数据。

1 个答案:

答案 0 :(得分:1)

您必须GROUP BY salesid,itemidMAX(confirmdocnum)JOIN连同您的结果一起获得所需的行。我无法对其进行测试,但是这样的方法应该可以工作:

    SELECT t1.* 
    FROM (
    SELECT cct.salesid, 
           cct.itemid, 
           ccj.confirmid, 
           ccj.confirmdocnum, 
           ccj.createddatetime
    FROM AxDynamics.dbo.custconfirmjour AS ccj
    LEFT JOIN AxDynamics.dbo.custconfirmtrans AS cct
    ON cct.salesid=ccj.salesid
      AND cct.confirmid=ccj.confirmid
      AND cct.confirmdate=ccj.confirmdate
    WHERE ccj.CREATEDDATETIME >= CONVERT (DATE,'2019/01/01')
    ) t1 
    INNER JOIN 
    (
    SELECT cct.salesid,cct.itemid,MAX(ccj.confirmdocnum) AS max_confirmdocnum
    FROM AxDynamics.dbo.custconfirmjour AS ccj
    LEFT JOIN AxDynamics.dbo.custconfirmtrans AS cct
    ON cct.salesid=ccj.salesid
      AND cct.confirmid=ccj.confirmid
      AND cct.confirmdate=ccj.confirmdate
    WHERE ccj.CREATEDDATETIME >= CONVERT (DATE,'2019/01/01')
    GROUP BY cct.salesid,cct.itemid
    ) t2 
ON t1.salesid=t2.salesid AND t1.itemid=t2.itemid AND t1.confirmdocnum=t2.max_confirmdocnum