sql group by function

后端 未结 3 1397
误落风尘
误落风尘 2021-01-28 09:45

I need to only get the line with the highest transactiontime per productId. So In this case I need to get the first line and all the other lines with productid 224 should be gon

3条回答
  •  孤城傲影
    2021-01-28 09:55

    You could do:

    SELECT  MT.NQ, MT.ProductId, MT.Product, MT.Warehouse, MT.ProductType, MT.Transactiontime
    FROM    @MaxTime MT
    INNER JOIN (
                    SELECT  ProductId
                    ,       MAX(Transactiontime) AS 'TransactionTime'
                    FROM    @MaxTime
                    GROUP BY Productid
                ) GR
            ON  MT.ProductId = GR.ProductId
            AND MT.TransactionTime = GR.TransactionTime
    ORDER BY MT.ProductId
    

提交回复
热议问题