Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

sql - Select max value of a column

I have this query that selects some fields based on some conditions:

 SELECT [Qty].[plant_no]
      ,[Qty].[ord_no]
      ,[Qty].[form_no]
      ,[Qty].[form_var]
      ,[Qty].[form_part]
      ,[Qty].[res_no]
      ,isnull([RFORMPARTS].[cost_center],'_') as [RFORMPARTS_cost_center]
      ,[BARTICLES].[wght_net] AS [Unit_kg]
      ,CASE
        WHEN [Qty].[qty_unit] IN ('KG','KG-C') THEN [Qty].[fb_qty]
        WHEN [Qty].[qty_unit] = 'NR' THEN [Qty].[fb_qty]*[BARTICLES].[wght_net]
        END AS [Qty_KG]
  FROM [dbo].[E00_PRD_003] AS [Qty]
  LEFT JOIN [dbo].[RFORMPARTS] as [RFORMPARTS] ON [RFORMPARTS].[form_no] =  [Qty].[form_no] AND [RFORMPARTS].[form_var] = [Qty].[form_var] and [RFORMPARTS].[form_part] = [qty].form_part
  LEFT JOIN [dbo].[BARTICLES] AS [BARTICLES] ON [BARTICLES].[art_no] = [Qty].[form_no]
  WHERE CONVERT(VARCHAR(10),[Qty].[date_start],112) >= 20180101 AND [Qty].[in_out] = '02' AND LEFT([Qty].[res_no],2) <> 'SL'

I would like to get the max Qty.form_part of each [Qty].[ord_no] and [Qty].[form_no] keeping also all the selected fields. Is it possible to do it with a group by? If yes, how can I do it? If no, is there another way to do it?

question from:https://stackoverflow.com/questions/66067147/select-max-value-of-a-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I am understanding your requirements, One option would be to create a subquery which returns the max of each ord_no & form_no pairs, you would use Group By but it would only be in the subquery and then you'd use the subquery's max form_part value in your outer queries results, if you want only the row with the max value of each ord_no & form_no pair, you would add one more condition to the INNER JOIN of the subquery adding in the join of the form_part value so that it joins on the max value as well, this will filter out all the extra rows with the same ord_no & form_no combination with less than the max form_part value:

 SELECT [Qty].[plant_no]
      ,[Qty].[ord_no]
      ,[Qty].[form_no]
      ,[Qty].[form_var]
      --,[Qty].[form_part] --Replaced with the subquery max
      ,[maxVals].[form_part] --Max values returned by subquery
      ,[Qty].[res_no]
      ,isnull([RFORMPARTS].[cost_center],'_') as [RFORMPARTS_cost_center]
      ,[BARTICLES].[wght_net] AS [Unit_kg]
      ,CASE
        WHEN [Qty].[qty_unit] IN ('KG','KG-C') THEN [Qty].[fb_qty]
        WHEN [Qty].[qty_unit] = 'NR' THEN [Qty].[fb_qty]*[BARTICLES].[wght_net]
        END AS [Qty_KG]
  FROM [dbo].[E00_PRD_003] AS [Qty]
  INNER JOIN --INNER JOIN the max values on the Qty table
  (
      SELECT [ord_no], [form_no], MAX([form_part]) AS [form_part]
      FROM [dbo].[E00_PRD_003]
      GROUP BY [ord_no], [form_no]
  ) AS maxVals
  ON [maxVals].[ord_no] = [Qty].[ord_no] AND [maxVals].[form_no] = [Qty].[form_no] 
  AND [maxVals].[form_part] = [Qty].[form_part] 
  LEFT JOIN [dbo].[RFORMPARTS] as [RFORMPARTS] ON [RFORMPARTS].[form_no] =  [Qty].[form_no] AND [RFORMPARTS].[form_var] = [Qty].[form_var] and [RFORMPARTS].[form_part] = [qty].form_part
  LEFT JOIN [dbo].[BARTICLES] AS [BARTICLES] ON [BARTICLES].[art_no] = [Qty].[form_no]
  WHERE CONVERT(VARCHAR(10),[Qty].[date_start],112) >= 20180101 AND [Qty].[in_out] = '02' AND LEFT([Qty].[res_no],2) <> 'SL'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...