SQL MIN Statement

I have the following SQL query that I need to select the MIN of the column NewProdCL. There are only 2 columns in the query and there could be multiple rows that have the same SalesOrderNo but different NewProdCL for those SalesOrders. I need to only pull the MIN of the NewProdCL so I will only get 1 record for each SalesOrderNo. Not sure how to write the MIN statement in my query.

SELECT        TOP (100) PERCENT S.fsono AS SalesOrderNo, CASE WHEN S.fstatus = 'Awaiting Approval' THEN S1.fprodcl WHEN (S1.fprodcl = '03' OR
                         S1.fprodcl = '02') THEN S1.fprodcl WHEN ISNULL(I1.WorkCenterName, '') = '' THEN '04' ELSE '01' END AS NewProdCL
FROM            dbo.somast AS S INNER JOIN
                         dbo.soitem AS S1 ON S.fsono = S1.fsono LEFT OUTER JOIN
                         dbo.InternalJobOrders AS I1 ON S1.fpartno = I1.RMPartNo
WHERE        (S.fstatus = 'OPEN' OR
                         S.fstatus = 'ON HOLD' OR
                         S.fstatus = 'CLOSED') AND (S1.fsource = 'S') AND (S1.fgroup <> 'SCHG')

Open in new window

LVL 1
Lawrence SalvucciInformation Technology ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
A lot of your OR can be consolidated into an IN clause. The following will select the MIN from an embedded query:
SELECT t.SalesOrderNo,
       MIN(t.NewProdCL) AS MinProdCL
FROM  (SELECT S.fsono AS SalesOrderNo,
              CASE
                  WHEN S.fstatus = 'Awaiting Approval' THEN S1.fprodcl
                  WHEN S1.fprodcl IN ('02', '03') THEN S1.fprodcl
                  WHEN ISNULL(I1.WorkCenterName, '') = '' THEN '04'
                  ELSE '01'
              END AS NewProdCL
       FROM   dbo.somast AS S
       INNER JOIN dbo.soitem AS S1 ON S.fsono = S1.fsono
       LEFT OUTER JOIN dbo.InternalJobOrders AS I1 ON S1.fpartno = I1.RMPartNo
       WHERE  S.fstatus IN ('OPEN', 'ON HOLD', 'CLOSED')
       AND    S1.fsource = 'S'
       AND    S1.fgroup <> 'SCHG') t
GROUP BY t.SalesOrderNo

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
That's a very simple way to write my query. Thank you very much for your help!!! I appreciate it!
dsackerContract ERP Admin/ConsultantCommented:
Very welcome. All the best.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.