我有一张表,其中包含对象的位置,名称和价格。例如
Location | Name | Price
Store 1 Apple $.50
Store 1 Pear $.75
Store 2 Peach $.75
Store 3 Mango $1.50
Store 3 Melon $2.00
我想要的是
Location | Name | Price
Store 1 Apple $.50
Store 2 Peach $.75
Store 3 Mango $1.50
我该怎么做?
答案 0 :(得分:1)
在groub by
中使用row_subquery
:
select location,name,price
from table_name
where (location,price) in
( select t.location,min(t.price)
from table_name t
group by t.location
)
您还可以使用self join
select t1.location,t1.name,t1.price
from table_name t1
join
( select location,min(price) as price
from table_name
group by location
) t2 on t1.location=t2.location and t1.price=t2.price
答案 1 :(得分:0)
使用以下sql查询。
Declare @table1 table(
Location varchar(256),
Name varchar(256),
Price varchar(256)
)
insert into @table1
select 'Store 1','Apple','$.50'
union select 'Store 1','Pear','$.75'
union select 'Store 2','Peach','$.75'
union select 'Store 3','Mango','$1.50'
union select 'Store 3','Melon','$2.00'
select * from @table1
select Location,Min(Name) as Name,Min(Price) as Price from
@table1
group by Location