I have a (simplified) table that is structured like so:
Table: ItemData
PK | ItemID | StoreFK | Retail
1 | 100101 | 1 | 4.99
4 | 100101 | 2 | 4.99
7 | 100101 | 3 | 0.99
2 | 100102 | 1 | 6.99
5 | 100102 | 2 | 6.99
8 | 100102 | 3 | 6.99
3 | 100103 | 1 | 7.99
6 | 100103 | 2 | 8.99
9 | 100103 | 3 | 9.99
I would like to return all the items that have a different retail at one or more stores:
Returns:
ItemID
100101
100103
Item
100101
has a lower retail at store3
then at store1
&2
it is returned.Item
100103
has a different retail at each store location so it is returned.Item
100102
has equality in it's retail at all three stores so it are not returned.
I am not new to SQL, but I am lost as to how to make this inequality check in an efficient manor. What is the best way to check for equality in one column based on groupings on another column?
The easiest solution I can think of would be to just compare the average of each
ItemID
with the maximum (or minimum for that matter) of eachItemID
The SQL Statement would be something like
Note that if
retail
is nullable there are scenario's this method fails.See this SQL Fiddle demo
With all due respect to Lieven, I prefer this:
Another method: But I still love @Liven's answer :) My query tells you number of different prices as well.
Results: as per OP's updated question and data.