如何查出多个字段值相同的行
用到一个表seller
id product brand volume category price
1 金龙鱼大豆调和油5升 金龙鱼 5 浸润 68
2 金龙鱼花生油5升 金龙鱼 5 浸润 80
3 金龙鱼芝麻油5升 金龙鱼 5 压榨 90
4 福临门大豆油5升 福临门 5 浸润 65
5 福临门芝麻油油5升 福临门 5 压榨 88
我想把所有数据按价格低到高排列,如果有brand, volume, category这三个字段值相同的就只取价格最低的那行,所以这个例子里的最后结果是:
id product brand volume category price
4 福临门大豆油5升 福临门 5 浸润 65
5 福临门芝麻油油5升 福临门 5 压榨 88
1 金龙鱼大豆调和油5升 金龙鱼 5 浸润 68
3 金龙鱼芝麻油5升 金龙鱼 5 压榨 90
这个查询语句怎么写,多谢!
[解决办法]
----------------------------
-- Author :DBA_Huangzj()
-- Date :2013-05-10 22:10:59
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
--Jun 17 2011 00:57:23
--Copyright (c) Microsoft Corporation
--Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[seller]
if object_id('[seller]') is not null drop table [seller]
go
create table [seller]([id] int,[product] varchar(19),[brand] varchar(6),[volume] int,[category] varchar(4),[price] int)
insert [seller]
select 1,'金龙鱼大豆调和油5升','金龙鱼',5,'浸润',68 union all
select 2,'金龙鱼花生油5升','金龙鱼',5,'浸润',80 union all
select 3,'金龙鱼芝麻油5升','金龙鱼',5,'压榨',90 union all
select 4,'福临门大豆油5升','福临门',5,'浸润',65 union all
select 5,'福临门芝麻油油5升','福临门',5,'压榨',88
--------------开始查询--------------------------
SELECT *
FROM [seller] a
WHERE EXISTS (SELECT 1 FROM (
select brand, volume, category,MIN([price])[price] from [seller]
GROUP BY brand, volume, category)b WHERE a.brand=b.brand AND a.volume =b.volume AND a.category= b.category AND a.[price]= b.[price])
ORDER BY [price]
----------------结果----------------------------
/*
id product brand volume category price
----------- ------------------- ------ ----------- -------- -----------
4 福临门大豆油5升 福临门 5 浸润 65
1 金龙鱼大豆调和油5升 金龙鱼 5 浸润 68
5 福临门芝麻油油5升 福临门 5 压榨 88
3 金龙鱼芝麻油5升 金龙鱼 5 压榨 90
*/