我有一个包含6个字段的表。这些列是ID、new_id价格、标题、Img、活动。我有一个重复的价格列的数据。当我执行select操作时,我希望只显示new_id不同的不同行。例如:
ID New_ID Price Title Img Active
1 1 20.00 PA-1 0X4... 1
2 1 10.00 PA-10 0X4... 1
3 3 20.00 PA-11 0X4... 1
4 4 30.00 PA-5 0X4... 1
5 9 20.00 PA-99A 0X4... 1
6 3 50.00 PA-55 0X4... 1当select语句运行时,应该只显示ID为(1,4,9,6)的行。原因是价格较高的new_ID应该会出现。我该怎么做呢?
发布于 2010-05-07 07:50:23
在支持窗口聚合的数据库(oracle、sql server 2005、postgresql 8.4)中,如下所示:
select id, new_id, price, title, img, active
from (select id, new_id, price, title, img, active,
row_number() over (partition by new_id order by price desc) as position
from the_table
) where position = 1发布于 2010-09-19 05:34:03
select *
from T as t
where exists ( select 1 from T where new_id = t.new_id
group by new_id having max(price) = t.price )要测试是否存在,请使用exists!在这里,您希望这些行具有基于new_id的最高价格。
我只想显示不同的行
通常,当有人想要“不同的行”时,他们真的想要“最新的”行或具有“最多”内容的行。它几乎总是可以用类似于上面的形式来表达。
发布于 2013-02-26 17:12:02
select * from ABC where charge_amt in(
SELECT charge_amt
FROM ABC
GROUP BY charge_amt
HAVING (COUNT(charge_amt) = 1) )
order by charge_amt aschttps://stackoverflow.com/questions/2785274
复制相似问题