0

the order# is first. we need for a report how many cartons per order. the carton col is PHBNO#. IN SQL I want to get a result of PHORD# and PHBNO# for the final tally that is for example 05360237 5

PHORD#    PHSFX#  PHBNO#   PHPKGN           
05360207   000         1   CUSTOM           
05360220   000         1   CUSTOM           
05360225   000         1   CUSTOM        
05360237   000         1   CUSTOM           
05360237   000         2   CUSTOM           
05360237   000         3   CUSTOM           
05360237   000         4   CUSTOM           
05360237   000         5   CUSTOM    

05360249   000         1   CUSTOM           
05360249   000         2   CUSTOM           
05360249   000         3   CUSTOM           
05360260   000         1   CUSTOM           
05360260   000         2   CUSTOM           
05360269   000         1   CUSTOM           
05360315   000         1   CUSTOM           
05360323   000         1   CUSTOM           
  • possible duplicate of [SQL Server: SELECT only the rows with MAX(DATE)](http://stackoverflow.com/questions/7118170/sql-server-select-only-the-rows-with-maxdate) – Andrew Apr 02 '15 at 21:57

1 Answers1

0

Your values seem to be increasing, so I think you can just use aggregation:

select PHORD, max(PHBNO)
from table t
group by PHORD;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786