数据按最大值过滤

时间:2017-12-12 13:55:09

标签: sql sql-server

我有一个很长的查询,最后返回超过10K行。由于乘法请求Id,它们中的一些出现两次。 我想知道如何根据“material_request_id”更新我的查询以仅获取MAX值。

我的疑问:

SELECT 
 distinct vortex_dbo.vw_public_material_location.material_name
,vw_public_request_material_location_mir.material_request_id
 ,vw_public_request_material_location_mir.parttype_name
 ,operation_code
 ,vw_public_request_material_location_mir.result_name
 ,vw_public_request_material_location_mir.qdf_number
  , requestor
      ,[vortex_hvc].[vortex_dbo].[material_request].recipient_name
      ,[vortex_hvc].[vortex_dbo].[material_request].created_by
      ,[vortex_hvc].[vortex_dbo].[material_request].created_datetime as time1
      , DATEPART(WW,[vortex_hvc].[vortex_dbo].[material_request].created_datetime) as WW
      ,vw_public_request_material_location_mir.product_code_name
      ,task_name
      ,vw_public_request_material_location_mir.full_location_name

  FROM [vortex_hvc].[vortex_dbo].[vw_public_request_material_location_mir]
         left join request on vw_public_request_material_location_mir.material_request_id = request.request_key
        left join vortex_dbo.material_request on vw_public_request_material_location_mir.material_request_id = vortex_dbo.material_request.material_request_id
        left join vortex_dbo.vw_public_material_location on (vw_public_request_material_location_mir.last_result_id = vortex_dbo.vw_public_material_location.last_result_id 
        or vw_public_request_material_location_mir.material_id = vortex_dbo.vw_public_material_location.material_id)
 left join vortex_dbo.vw_public_material_history on  vw_public_request_material_location_mir.material_request_id like (substring(vw_public_material_history.comments,12,6)) 

  where (vw_public_request_material_location_mir.qdf_number not like 'null' and vw_public_request_material_location_mir.qdf_number  not like '')
  and operation_code not like '7757'
  and vw_public_request_material_location_mir.product_code_name like 'cannon LAKE%'
  and vw_public_request_material_location_mir.task_id not like 'null'
  and (vw_public_request_material_location_mir.result_name like 'bin 100' or  vw_public_request_material_location_mir.result_name like 'bin 01'
   or  vw_public_request_material_location_mir.result_name like 'bin 02' or vw_public_request_material_location_mir.result_name like 'pass')
 and (requestor like 'BUGANIM, RINAT' and employee_name like 'BUGANIM, RINAT')
  and  ( DateDiff(DD,[vortex_hvc].[vortex_dbo].[material_request].created_datetime, getdate()) < 30)
and ((substring(vortex_dbo.vw_public_material_location.comments,12,6)) like vw_public_request_material_location_mir.material_request_id
  or vortex_dbo.vw_public_material_location.comments like 'Changed by Matrix Transaction Handler' or vortex_dbo.vw_public_material_location.comments like 'Unit Ownership:%' 
  or vortex_dbo.vw_public_material_location.comments like  '%')
  and (unit_number = vortex_dbo.vw_public_material_location.material_name or unit_number is null)
  and (vortex_dbo.vw_public_material_location.material_id = vw_public_material_history.material_id )

  and material_name like 'D71G8V8900049'

返回的数据是:

material_name   material_request_id parttype_name   operation_code  result_name qdf_number  requestor   recipient_name  created_by  time1   WW  product_code_name   task_name   full_location_name
D71G8V8900049   329136  H6 4SXDYV K U   7295    BIN 01  QPCR    BUGANIM, RINAT  Nicola, Michael mir_interface   2017-11-15 09:35:18.520 46  CANNON LAKE U 2+2   PPV-M   WFIQ: FROM WIP
D71G8V8900049   330463  H6 4SXDYV K U   7295    BIN 01  QPCR    BUGANIM, RINAT  Nicola, Michael SMS_Interface   2017-11-22 16:30:53.693 47  CANNON LAKE U 2+2   PPV-M   ACTIVE: NPP REALLOCATION

此外,还有任何选项可以在临时表中存储当前数据并在其上制作其他过滤器吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果我将结果标记为tab(material_name, material_request_id, ...),则可以使用以下命令检索每material_request_id material_name with tab as ( -- your query comes here ) select tab.* from tab join ( --subquery select material_name, max(material_request_id) max_request_id from tab group by material_name ) t on t.material_name = tab.material_name and t.max_request_id = tab.material_request_id 行的行

material_request_id

Here您可以找到类似的问题/答案,并使用窗口函数与解决方案进行一些性能比较。

子查询每material_name找到最高t.max_request_id = tab.material_request_id(您可以单独运行它以查看)。然后,您只需将其与结果相关联,您只需选择满足material_request_id的行,因此,只选择material_name*, *::before, *::after { box-sizing: border-box; } *行。{/ p>