简体   繁体   English

按条款分组并具有最高记录-MYsql

[英]Group by Clause with Top Record - MYsql

I have written the following SQL query which works great 我写了下面的SQL查询,效果很好

SELECT  W.LineItems_LineItemID
     , P.CurrentRate
     , P.CurrentEffectiveDate 
  FROM WorkOrderLineItems W 
  JOIN PayScaleLoaclJObCodes P 
    ON P.JobCodeID = W.LineItems_LineItemID 
 WHERE P.PayScalesLocal_ID = 29
   AND W.WorkOrderCurrent_WorkOrderID = 120420
   AND P.CurrentEffectiveDate <= '2018-05-27' 
 ORDER 
    BY P.CurrentEffectiveDate DESC

Shows following results 显示以下结果

在此处输入图片说明

Is there a way to return only a single that is the Top record, I do not want to show multiple records only the first record in the resulset 有没有一种方法只返回单个记录,即Top记录,我不想在resulset中仅显示多个记录

SELECT TOP 1 W.LineItems_LineItemID, P.CurrentRate, P.CurrentEffectiveDate 
  FROM WorkOrderLineItems W 
  JOIN PayScaleLoaclJObCodes P ON P.JobCodeID = W.LineItems_LineItemID 
  WHERE P.PayScalesLocal_ID = 29 AND W.WorkOrderCurrent_WorkOrderID = 120420
  AND P.CurrentEffectiveDate <= '2018-05-27' 
  ORDER BY P.CurrentEffectiveDate DESC

The SELECT TOP clause is used to specify the number of records to return. SELECT TOP子句用于指定要返回的记录数。 For your case all you would need is TOP 1 for one. 对于您的情况,您所需要的只是TOP 1 for a。

OR 要么

SELECT W.LineItems_LineItemID, P.CurrentRate, P.CurrentEffectiveDate 
  FROM WorkOrderLineItems W 
  JOIN PayScaleLoaclJObCodes P ON P.JobCodeID = W.LineItems_LineItemID 
  WHERE P.PayScalesLocal_ID = 29 AND W.WorkOrderCurrent_WorkOrderID = 120420
  AND P.CurrentEffectiveDate <= '2018-05-27' 
  ORDER BY P.CurrentEffectiveDate DESC 
  LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM