declare @t table(酒店名称 varchar(10),网上价 int)
insert into @t select '锦江饭店',300
insert into @t select '锦江饭店',400
insert into @t select '锦江饭店',5000
insert into @t select '港丽酒店',300
insert into @t select '港丽酒店',277SELECT * FROM @t a WHERE not exists(SELECT 1 FROM @t WHERE 酒店名称 = a.酒店名称 AND 网上价 <a.网上价)/*
酒店名称 网上价
---------- -----------
锦江饭店 300
港丽酒店 277
*/
insert into @t select '锦江饭店',300
insert into @t select '锦江饭店',400
insert into @t select '锦江饭店',5000
insert into @t select '港丽酒店',300
insert into @t select '港丽酒店',277SELECT * FROM @t a WHERE not exists(SELECT 1 FROM @t WHERE 酒店名称 = a.酒店名称 AND 网上价 <a.网上价)/*
酒店名称 网上价
---------- -----------
锦江饭店 300
港丽酒店 277
*/
Select 酒店名称,max(网上价) as 网上价 from 酒店 Where 网上价>=1000
group by 酒店名称
union
Select 酒店名称,max(网上价) as 网上价 from 酒店 Where 网上价<1000
group by 酒店名称
(
酒店名称 varchar(10),
网上价 int
)
insert into 酒店
select '锦江饭店',300 union all
select '锦江饭店',400 union all
select '锦江饭店',5000 union all
select '港丽酒店',300 union all
select '港丽酒店',277
go
select a.酒店名称,a.网上价 from 酒店 a where (select COUNT(1) from 酒店 where 酒店名称=a.酒店名称 and 网上价<=a.网上价)=1
--结果
酒店名称 网上价
---------- -----------
锦江饭店 300
港丽酒店 277(2 行受影响)
select 酒店名称,min(网上价) as '网上价'
from 酒店
group by 酒店名称
insert into @t select '锦江饭店',300
insert into @t select '锦江饭店',400
insert into @t select '锦江饭店',5000
insert into @t select '港丽酒店',300
insert into @t select '港丽酒店',277SELECT
酒店名称,
case
when exists(select 1 from @t where 酒店名称=a.酒店名称 and 网上价>1000)
then max(网上价)
else min(网上价)
end 网上价
FROM
@t a
group by
酒店名称/*
酒店名称 网上价
---------- -----------
港丽酒店 277
锦江饭店 5000
*/
Where not exists(Select * from 酒店 where 酒店名称=a.酒店名称 and 网上价>=1000)
group by 酒店名称
union
Select 酒店名称,max(网上价) as 网上价 from 酒店
Where exists(Select * from 酒店 where 酒店名称=a.酒店名称 and 网上价>=1000)
group by 酒店名称
酒店名称 nvarchar(100),
网上价 int
)
insert tb
select '锦江饭店', 300 union all
select '锦江饭店', 400 union all
select '锦江饭店', 5000 union all
select '港丽酒店', 300 union all
select '港丽酒店', 277select tmp.* from tb tmp
where not exists(select 1 from tb where 酒店名称=tmp.酒店名称 and 网上价<tmp.网上价)--result:
酒店名称 网上价
----------------
锦江饭店 300
港丽酒店 277
------------------------------------------------------------------------------------
declare @t table(酒店名称 varchar(10),网上价 int)
insert into @t select '锦江饭店',300
insert into @t select '锦江饭店',400
insert into @t select '锦江饭店',5000
insert into @t select '港丽酒店',300
insert into @t select '港丽酒店',277SELECT
酒店名称,
case
when exists(select 1 from @t where 酒店名称=a.酒店名称 and 网上价>1000)
then max(网上价)
else min(网上价)
end 网上价
FROM
@t a
group by
酒店名称/*
酒店名称 网上价
---------- -----------
港丽酒店 277
锦江饭店 5000
*/
insert into @t select '锦江饭店',300
insert into @t select '锦江饭店',400
insert into @t select '锦江饭店',5000
insert into @t select '港丽酒店',300
insert into @t select '港丽酒店',277
--分解成两个查询联合的方式:
SELECT
酒店名称,max(网上价) as 网上价
FROM
@t a
where
exists(select 1 from @t where 酒店名称=a.酒店名称 and 网上价>1000)
group by
酒店名称
union
SELECT
酒店名称,min(网上价) as 网上价
FROM
@t a
where
not exists(select 1 from @t where 酒店名称=a.酒店名称 and 网上价>1000)
group by
酒店名称/*
酒店名称 网上价
---------- -----------
港丽酒店 277
锦江饭店 5000
*/
(
select * from tb where 酒店名称 in (select 酒店名称 from tb where 网上价 >= 1000)
) t
group by 酒店名称
union all
select 酒店名称 , max(网上价) 网上价 from
(
select * from tb where 酒店名称 not in (select 酒店名称 from tb where 网上价 >= 1000)
) t
group by 酒店名称一个取最小,一个取最大,估计是这个意思.
(
酒店名称 varchar(10),
网上价 int
)
insert into 酒店
select '锦江饭店',300 union all
select '锦江饭店',400 union all
select '锦江饭店',5000 union all
select '港丽酒店',300 union all
select '港丽酒店',277
go
select 酒店名称,case when max(网上价)>1000 then max(网上价) else min(网上价) end from 酒店 group by 酒店名称--大于一千取最大,小于一千取最小
酒店名称
---------- -----------
港丽酒店 277
锦江饭店 5000(2 行受影响)
酒店价格中有大于等于1000取1000以上最低的
酒店价格中有小于1000的直接取最低的
=================
select * from tmpty;ID VAL
-------------------- ---------------------
锦江饭店 300
锦江饭店 400
锦江饭店 5000
港丽酒店 300
港丽酒店 277select id, val from (
select id, min(val) as val from tmpty
where val < 1000 and id <> (select id from tmpty where val >= 1000) group by id
union all
select id, min(val) as val from tmpty where val >= 1000 group by id);
ID VAL
-------------------- ----------
港丽酒店 277
锦江饭店 5000