Display higher value between to numbers

I have a table with an Item Number and a Price, the table contains the same item number with different prices I want to display the item with the higher price so that I can delete it and keep the Item with the Lower price.
SELECT        SSGPROMO, ITEMNMBR, CUSTNMBR, STRTDATE, ENDDATE, SSG_Customer_Price, MRKDNAMT, PRICELVL, UOFM
FROM            PROMO_TEMP
WHERE        (ITEMNMBR IN ('AGUA1E092110', 'AGUA1E097704', 'AGUA1E452110', 'AGUA1E802110', 'LCS10877', 'LCS10907', 'LCS10915', 'PLT97069'))
ORDER BY ITEMNMBR

Open in new window

PriceDups.jpg
skull52IT director Asked:
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.

minnirokCommented:
Try using a subquery to pull the max price relative to each of your promo/item number combinations (assuming they are essentially operating as a composite primary key for this data).

SELECT    pt.SSGPROMO, pt.ITEMNMBR, pt.CUSTNMBR, pt.STRTDATE, pt.ENDDATE, pt.SSG_Customer_Price, pt.MRKDNAMT, pt.PRICELVL, pt.UOFM
FROM    PROMO_TEMP pt
WHERE    pt.ITEMNMBR IN ('AGUA1E092110', 'AGUA1E097704', 'AGUA1E452110', 'AGUA1E802110', 'LCS10877', 'LCS10907', 'LCS10915', 'PLT97069') AND pt.SSG_CustomerPrice = (SELECT MAX(SSG_Customer_Price) FROM PROMO_TEMP WHERE SSG_PROMO = pt.SSG_PROMO AND ITEMNMBR = pt.ITEMNMBR) 
ORDER BY    pt.ITEMNMBR

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
or try this:
Select * 
from
(

SELECT SSGPROMO, ITEMNMBR, CUSTNMBR, STRTDATE, ENDDATE, SSG_Customer_Price, MRKDNAMT, PRICELVL, UOFM,
Row_Number() over (Partition by ITEMNMBR order by SSG_Customer_Price) idx
FROM PROMO_TEMP
WHERE (ITEMNMBR IN ('AGUA1E092110', 'AGUA1E097704', 'AGUA1E452110', 'AGUA1E802110', 'LCS10877', 'LCS10907', 'LCS10915', 'PLT97069'))

) a
Where a.idx = 1
ORDER BY a.ITEMNMBR

Open in new window

for ref:

How to use ROW_NUMBER() to enumerate and partition records in SQL Server
http://www.codeproject.com/Articles/308281/How-to-Use-ROW-NUMBER-to-Enumerate-and-Partition-R
Vitor MontalvãoMSSQL Senior EngineerCommented:
If the price is the only column that distinguish them, then it's more easy and fast to use the MAX() function:
SELECT  SSGPROMO, ITEMNMBR, CUSTNMBR, STRTDATE, ENDDATE, MRKDNAMT, PRICELVL, UOFM, MAX(SSG_Customer_Price)
FROM  PROMO_TEMP
WHERE  ITEMNMBR IN ('AGUA1E092110', 'AGUA1E097704', 'AGUA1E452110', 'AGUA1E802110', 'LCS10877', 'LCS10907', 'LCS10915', 'PLT97069')
GROUP BY SSGPROMO, ITEMNMBR, CUSTNMBR, STRTDATE, ENDDATE, MRKDNAMT, PRICELVL, UOFM
ORDER BY ITEMNMBR

Open in new window

The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

PortletPaulEE Topic AdvisorCommented:
the purpose is to retain the lower price only, and to delete any/all higher prices
the sample data seen above suggests there are always pairs

however I think it would be prudent to concentrate on the MINIMUM (the one you want to keep) and then delete any rows with prices higher than that, but being careful not to delete single rows. The following is utterly untested!

Identify how many similar rows exist for each combination of SSGPROMO & ITEMNMBR (cn)
Identify the rows holding the minimum price (rn)

if cn > 1 and rn > 1 -- those rows are unwanted

with CTE as (
  select *
  , count(*)     over(partition by SSGPROMO,ITEMNMBR)  as cn
  , row_number() over(partition by SSGPROMO,ITEMNMBR 
                      order by SSG_Customer_Price ASC) as rn
  from PROMO_TEMP
  )
delete from CTE 
where rn > 1
and cn > 1
;

select
*
from PROMO_TEMP
;

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
PortletPaulEE Topic AdvisorCommented:
a test was run, here is the detail
    CREATE TABLE PROMO_TEMP
        ([SSGPROMO] varchar(8), [ITEMNMBR] varchar(8), [CUSTNMBR] varchar(8), [STRTDATE] varchar(8)
        , [ENDDATE] varchar(7), [SSG_Customer_Price] decimal(12,3), [MRKDNAMT] varchar(8), [PRICELVL] varchar(8), [UOFM] varchar(4))
    ;
        
    INSERT INTO PROMO_TEMP
        ([SSGPROMO], [ITEMNMBR], [CUSTNMBR], [STRTDATE], [ENDDATE], [SSG_Customer_Price], [MRKDNAMT], [PRICELVL], [UOFM])
    VALUES
        ('11305', 'A1001', '', '', '', 1.1, '', '', ''),
        ('11305', 'B1001', '', '', '', 2.1, '', '', ''),
        ('11305', 'B1001', '', '', '', 2.2, '', '', ''),
        ('11305', 'C1001', '', '', '', 3.1, '', '', ''),
        ('11305', 'C1001', '', '', '', 3.2, '', '', ''),
        ('11305', 'C1001', '', '', '', 3.3, '', '', '')
    ;
    
    with CTE as (
      select *
      , count(*)     over(partition by SSGPROMO,ITEMNMBR)  as cn
      , row_number() over(partition by SSGPROMO,ITEMNMBR 
                          order by SSG_Customer_Price ASC) as rn
      from PROMO_TEMP
      )
    delete from CTE 
    where rn > 1
    and cn > 1
    ;
    
    
**Query 1**:

    select
    *
    from PROMO_TEMP
    

**[Results][2]**:
    | SSGPROMO | ITEMNMBR | CUSTNMBR | STRTDATE | ENDDATE | SSG_Customer_Price | MRKDNAMT | PRICELVL | UOFM |
    |----------|----------|----------|----------|---------|--------------------|----------|----------|------|
    |    11305 |    A1001 |          |          |         |                1.1 |          |          |      |
    |    11305 |    B1001 |          |          |         |                2.1 |          |          |      |
    |    11305 |    C1001 |          |          |         |                3.1 |          |          |      |

  [1]: http://sqlfiddle.com/#!3/2031c/1

Open in new window

skull52IT director Author Commented:
Thanks to all who answered but Paul Maxwell's Solution worked best because it not only gave me the higher of the 2 values but found the the duplicates as well.
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
Microsoft SQL Server

From novice to tech pro — start learning today.