2

I have a pricing table for a retail software that holds a UPC for an item, a property code (i.e. "REGULAR_PRICE, PROMO_PRICE"), and a price.

example table:

400000320243 REGULAR_PRICE 80
400000320243 PROMO_PRICE 80
400000320250 REGULAR_PRICE 50
400000320250 PROMO_PRICE 40

I am trying to write a query to find where the PROMO_PRICE = REGULAR_PRICE for any UPCs and output a list of the UPCs where this condition holds.

I cannot figure out how to write this in SQL. I am using SQL Server 2008 R2.

Attempted pseudo code:

for each upc:
  if upc.regular_price = upc.promo_price:
      print upc
2
  • Are promo and regular in separate tables? Commented Feb 12, 2013 at 17:43
  • No, they are in the same table. Commented Feb 12, 2013 at 18:56

5 Answers 5

5

You could do that a bunch of ways. One way is to create two sets, one with upcs and regular prices, the other with upcs and promo prices, and join those two sets, like this:

select r.upc, r.price from
   (select upc, price from t where propertyCode = 'regular_price') r inner join
   (select upc, price from t where propertyCode = 'promo_price') p on
   r.upc = p.upc and
   r.price = p.price

You can try it on sqlfiddle.

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you're table is named Pricing and you have columns named upc, propertycode, and price, you could perform a self join on this data. It would look something like:

SELECT Reg.upc
FROM Pricing Reg JOIN Pricing Promo ON Reg.upc = Promo.upc
WHERE Reg.propertycode = 'REGULAR_PRICE' 
    AND Promo.propertycode = 'PROMO_PRICE'
    AND Reg.price = Promo.price

2 Comments

I don't think that will run on MS Sql Server 2008 R2. What is USING ? Can be fixed like this: sqlfiddle.com/#!3/cc32f/4
@zespri ahh, yes. I didn't realize that MS SQL Server didn't have the USING construct. It's a simplififed syntax for equi-joins (en.wikipedia.org/wiki/Join_(SQL)#Equi-join). I'll edit my question to add that in.
1

An INTERSECT query will also work here:

SELECT upc, price
FROM atable
WHERE property_code = 'REGULAR_PRICE'
INTERSECT
SELECT upc, price
FROM atable
WHERE property_code = 'PROMO_PRICE'
;

SQL Fiddle Demo (uses @Beth's schema).

Comments

0

In addition... Judging by your example - looks like a table to me - you may use LAG() / LEAD() analytic functions if available in you version of SQL to compare data - prev or next row with current row values...

1 Comment

FYI, LAG() / LEAD() analytic functions available in SQLServer2012
0

Option with EXISTS subquery

SELECT *
FROM dbo.test3 t1
WHERE EXISTS (
              SELECT 1
              FROM dbo.test3 t2
              WHERE t2.PriceName = CASE WHEN t1.PriceName = 'PROMO_PRICE' THEN 'REGULAR_PRICE'
                                        WHEN t1.PriceName = 'REGULAR_PRICE' THEN 'PROMO_PRICE' END
                AND t1.Price = t2.Price                                                    
              )

Demo on SQLFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.