'Compare multiple columns from same table and only report if any of the columns have different values

I've got a table called Price table and trying to write a select query to fetch records only when one of the columns - Price or Fromdate or Todate or Packsize is different between store region's = Metro and Regional.

How is such a query implemented?

The expected output in this case is enter image description here



Solution 1:[1]

We can use COUNT() as an analytic function:

WITH cte AS (
    SELECT t.*, COUNT(*) OVER (PARTITION BY Price, Fromdate, Todate, Packsize) cnt
    FROM yourTable t
)

SELECT StoreRegion, Itemid, Price, Fromdate, Todate, Packsize
FROM cte
WHERE cnt = 1;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Biegeleisen