Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Exclude Duplicates based on column value

Posted on 2014-02-17
9
Medium Priority
?
249 Views
Last Modified: 2014-02-20
I have a view SSG_CATALOG that exports data for our catalog with columns  ITEMNMBR, ITEMDESC, DEALERPRICE, PRCLEVEL my problem is that because we used different price levels foreach item it creates a row for each item. For example Item ACCP2015BR1 has a price level of A, AAA, and B but the DEALERPRICE is the same for all 3 levels so I just want to return only the A price level if the DEALERPRICE for AAA and B = A.

Price Levels
0
Comment
Question by:skull52
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 35

Expert Comment

by:David Todd
ID: 39865936
Hi,

My thought is a group by with the min of prclevel

ie
select
    c.itemnmbr
    , c.itemdesc
    , c.dealerprice
    , min( c.prclevel ) as PRCLEVEL
from dbo.SSG_CATALOG c
where
    somewhereclause
group by
    c.itemnmbr
    , c.itemdesc
    , c.dealerprice
order by
    c.itemnmbr
    , c.itemdesc
    , c.dealerprice
;

HTH
  David
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39865938
One way would be to use RANK() to order the PRICELEVEL values, grouped by ITEMNBR and DEALERPRICE, alphabetically (Assuming A is better than AAA is better than B), then just grab all the rows that sort first.  
SELECT a.ITEMNMBR, a.ITEMDESC, a.DEALERPRICE, a.PRICELEVEL
FROM (
   SELECT ITEMNBR, ITEMDESC, DEALERPRICE, PRICELEVEL, 
      RANK() OVER (PARTITION BY ITEMNMBR, DEALERPRICE ORDER BY PRICELEVEL) as rank_order) a
WHERE a.rank_order = 1

Open in new window

0
 
LVL 35

Expert Comment

by:David Todd
ID: 39865951
Hi,

I'd be interested in the execution plan costs - if there are major differences between these two approaches.

Regards
  David
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:skull52
ID: 39866014
Thanks for the responses

David,
 I get the following error from your suggestion
Msg 4145, Level 15, State 1, Line 9
An expression of non-boolean type specified in a context where a condition is expected, near 'group'.

Jim,
I cant even get yours to run I think the reference to the  SSG_CATALOG is missing
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39866032
<correction to my above code.  You may have to check the column names, as the image was hard to read>
SELECT a.ITEMNMBR, a.ITEMDESC, a.DEALERPRICE, a.PRICELEVEL
FROM (
   SELECT ITEMNBR, ITEMDESC, DEALERPRICE, PRICELEVEL, 
      RANK() OVER (PARTITION BY ITEMNMBR, DEALERPRICE ORDER BY PRICELEVEL) as rank_order
   FROM SSG_CATALOG) a
WHERE a.rank_order = 1

Open in new window

0
 
LVL 35

Expert Comment

by:David Todd
ID: 39866036
Hi,

Did you just cut and paste my answer, or did you read it? You'll need to edit the where clause (or delete it!)

Regards
  David
0
 

Author Comment

by:skull52
ID: 39867902
David, My bad I did miss the WHERE clause.

Jim, thanks for fixing that, I knew the Reference to the table was missing so I added it

OK, so with using Jim's solution I get 25071 rows With David's I get 24974 a difference of 97 rows
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39867942
>OK, so with using Jim's solution I get 25071 rows With David's I get 24974 a difference of 97 rows
Since we don't have access to your data set, you will have to identify the 97 row difference and troubleshoot.   Perhaps there are duplicate rows being returned, in which case you can replace SELECT with SELECT DISTINCT.
0
 

Author Comment

by:skull52
ID: 39874557
Jim,
I used DISTINCT but it only returned 1 row less, and I examined the results and they look good so I am going with your solution. Thanks to David for his solution also.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question