Comparing Similar Columns for Equality

会有一股神秘感。 提交于 2019-12-04 18:50:38
Reinstate Monica

With all due respect to Lieven, I prefer this:

SELECT ItemID
FROM   ItemData
GROUP BY
       ItemID
HAVING COUNT(DISTINCT Retail)>1

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 each ItemID

The SQL Statement would be something like

SELECT ItemID
FROM   ItemData
GROUP BY
       ItemID
HAVING MAX(Retail) <> AVG(Retail)

Note that if retail is nullable there are scenario's this method fails.

See this SQL Fiddle demo

Another method: But I still love @Liven's answer :) My query tells you number of different prices as well.

select x.itemid, count(x.storefk) from (
select a.* , 
row_number() over 
(partition by a.retail order by
                   a.itemid desc) as r
from retailers a)x
group by x.itemid, x.r
having count(*) > 1
;

Results: as per OP's updated question and data.

| ITEMID | COLUMN_1 |
---------------------
| 100101 |        2 |
| 100103 |        3 |
SELECT *
  FROM ItemData
  WHERE itemID IN ( SELECT itemID
                      FROM ItemData
                      GROUP BY itemID
                      HAVING COUNT(DISTINCT Retail) > 1
                  )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!