Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL value changes

Suppose i have a table t1:

Product id Price
1 p1
1 p2
2 p1
2 p1
2 p1

What i want to know is, for a unique product id i want to see if there has been a new price.

Only having constant values for a specific product id is of not interest.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Would appreciate some tips.

Best regards

My thoughts is something like "Select the product ids’ where its distinct(count(price)) > 1. This should give me only the products ids that has a price change?

If a product id only has the same price without a change, it’s of no interest to select.

>Solution :

Your idea to check COUNT(DISTINCT price > 1) is correct.

We just need to use GROUP BY with a HAVING clause:

SELECT product_id
FROM t1
GROUP BY product_id
HAVING COUNT (DISTINCT price) > 1
ORDER BY product_id;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading