如何从内部联接字段SQL获取最大记录?

时间:2019-06-24 16:37:29

标签: sql subquery max

我需要从看起来像子查询中提取MAX seendate,并且可以为特定项目执行此操作,但是一旦我删除了特定项目的WHERE子句,它就不会显示任何内容,如下所示:

  SELECT Item.Barcode,
  Hetype.Description,
  StockTakeDetails.SeenDate,
  Users.UserName,
  defcon.Defcon_text,
  JOBS.Job_Ref,
  JOBS."Due Back"
FROM Item
  INNER JOIN Hetype ON Item.Type = Hetype.Type
  INNER JOIN StockTakeDetails ON Item.Itemref = StockTakeDetails.xItemID
  INNER JOIN Users ON Users.UID = StockTakeDetails.xUserSeen
  INNER JOIN EQLISTS ON Item.CurrentJob = EQLISTS.Eql_no
  INNER JOIN JOBS ON EQLISTS.Job_no = JOBS.JobNo
  INNER JOIN defcon ON JOBS.Status = defcon.Defcon_idx
WHERE JOBS."Due Back" >= TIMESTAMP '2019-01-01 00:00:00' AND
  JOBS.Status <> 5 AND
  Item.Barcode = '038223' AND
  StockTakeDetails.IDX = (SELECT Max(StockTakeDetails.IDX)
  FROM StockTakeDetails INNER JOIN Item ON StockTakeDetails.xItemID =
      Item.Itemref WHERE Item.Barcode = '038223')

这很好用,但是如果我取出Barcode子句,它不会显示任何数据:

SELECT Item.Barcode,
  Hetype.Description,
  StockTakeDetails.SeenDate,
  Users.UserName,
  defcon.Defcon_text,
  JOBS.Job_Ref,
  JOBS."Due Back"
FROM Item
  INNER JOIN Hetype ON Item.Type = Hetype.Type
  INNER JOIN StockTakeDetails ON Item.Itemref = StockTakeDetails.xItemID
  INNER JOIN Users ON Users.UID = StockTakeDetails.xUserSeen
  INNER JOIN EQLISTS ON Item.CurrentJob = EQLISTS.Eql_no
  INNER JOIN JOBS ON EQLISTS.Job_no = JOBS.JobNo
  INNER JOIN defcon ON JOBS.Status = defcon.Defcon_idx
WHERE JOBS."Due Back" >= TIMESTAMP '2019-01-01 00:00:00' AND
  JOBS.Status <> 5 AND
  StockTakeDetails.IDX = (SELECT Max(StockTakeDetails.IDX)
  FROM StockTakeDetails INNER JOIN Item ON StockTakeDetails.xItemID =
      Item.Itemref)

请让我知道我还能怎么做

谢谢

4 个答案:

答案 0 :(得分:2)

这最终满足了我的需求谢谢大家,所有答案都帮助我到达了这里。

SELECT i1.Barcode,
  Max(StockTakeDetails.SeenDate),
  Hetype.Description
FROM Item i1
  INNER JOIN Hetype ON i1.Type = Hetype.Type
  LEFT JOIN StockTakeDetails ON i1.Itemref = StockTakeDetails.xItemID
  INNER JOIN Users ON Users.UID = StockTakeDetails.xUserSeen
  INNER JOIN EQLISTS ON i1.CurrentJob = EQLISTS.Eql_no
  INNER JOIN JOBS ON EQLISTS.Job_no = JOBS.JobNo
  INNER JOIN defcon ON JOBS.Status = defcon.Defcon_idx
WHERE JOBS."Due Back" >= TIMESTAMP '2019-01-01 00:00:00' AND
  JOBS.Status <> 5
GROUP BY i1.Barcode,
  JOBS."Due Back",
  JOBS.Status,
  Hetype.Description    

答案 1 :(得分:1)

认为您需要在子查询末尾的GROUP BY Item.Barcode处替换特定的项目代码。取出后,您将返回整个表的单个值,而不是每个条形码的最大详细信息。

答案 2 :(得分:1)

只是需要和最大库存详细信息见过吗?

SELECT 
   Item.Barcode
   ,max(StockTakeDetails.IDX) as max_idx
FROM StockTakeDetails 
INNER JOIN Item 
   ON StockTakeDetails.xItemID = Item.Itemref 
GROUP BY Item.Barcode

您可以在Barcode和max_idx上加入此子查询,而不必在WHERE中将此查询用作单值过滤器。

或者,您可以修改您的主要查询,使其按相关的唯一字段分组,然后使用max(StockTakeDetails.SeenDate)

答案 3 :(得分:1)

很可能您需要在子查询中限制条形码。注意别名,请始终使用表别名以避免混淆

  SELECT i1.Barcode,
  Hetype.Description,
  StockTakeDetails.SeenDate,
  Users.UserName,
  defcon.Defcon_text,
  JOBS.Job_Ref,
  JOBS."Due Back"
FROM Item i1
  INNER JOIN Hetype ON i1.Type = Hetype.Type
  INNER JOIN StockTakeDetails ON i1.Itemref = StockTakeDetails.xItemID
  INNER JOIN Users ON Users.UID = StockTakeDetails.xUserSeen
  INNER JOIN EQLISTS ON i1.CurrentJob = EQLISTS.Eql_no
  INNER JOIN JOBS ON EQLISTS.Job_no = JOBS.JobNo
  INNER JOIN defcon ON JOBS.Status = defcon.Defcon_idx
WHERE JOBS."Due Back" >= TIMESTAMP '2019-01-01 00:00:00' AND
  JOBS.Status <> 5 AND
  StockTakeDetails.IDX = (SELECT Max(s.IDX)
                          FROM StockTakeDetails s 
                          INNER JOIN Item i2 ON s.xItemID = i2.Itemref 
                          WHERE i2.Barcode = i1.Barcode)