declare @t table
(
公司 char(2),
部门 varchar(10),
姓名 varchar(20),
商品名称 varchar(10),
价格 int,
折扣 decimal(10,2)
)insert @t select 'PS','市场','小张','苹果',60,0.8
union all select 'PY','业务','小红','苹果',50,0.5
insert @t select 'PS','市场','小张','葡萄',50,0.5
union all select 'PY','业务','小红','葡萄',60,0.8 select
a.*
from @t a
join(
select
商品名称,
min(价格*折扣) as col
from @t
group by 商品名称
)b
on b.商品名称 = a.商品名称
and b.col = a.价格 * a.折扣
/**
PY 业务 小红 苹果 50 0.50
PS 市场 小张 葡萄 50 0.50
**//**
当如果上面的面容换为
insert @t select 'PS','市场','小张','苹果',60,0.8
union all select 'PY','业务','小红','苹果',50,0.5
insert @t select 'PS','市场','小张','葡萄',50,0.5
union all select 'PY','业务','小红','葡萄',60,0.8
**/
我想得出每种水果的最小价格的记录,每种水果只能有一条
谢谢
(
公司 char(2),
部门 varchar(10),
姓名 varchar(20),
商品名称 varchar(10),
价格 int,
折扣 decimal(10,2)
)insert @t select 'PS','市场','小张','苹果',60,0.8
union all select 'PY','业务','小红','苹果',50,0.5
insert @t select 'PS','市场','小张','葡萄',50,0.5
union all select 'PY','业务','小红','葡萄',60,0.8
select * from @t t where not exists(select 1 from @t where 商品名称=t.商品名称 and 价格<t.价格)
/*
公司 部门 姓名 商品名称 价格 折扣
---- ---------- -------------------- ---------- ----------- ------------
PY 业务 小红 苹果 50 .50
PS 市场 小张 葡萄 50 .50*/
declare @t table
(
公司 char(2),
部门 varchar(10),
姓名 varchar(20),
商品名称 varchar(10),
价格 int,
折扣 decimal(10,2)
)insert @t select 'PS','市场','小张','苹果',60,0.8
union all select 'PY','业务','小红','苹果',50,0.5
insert @t select 'PS','市场','小张','葡萄',50,0.5
union all select 'PY','业务','小红','葡萄',60,0.8 select
*
from @t a
where not exists(
select
*
from @t
where 商品名称 = a.商品名称
and 价格 < a.价格)/**
PY 业务 小红 苹果 50 0.50
PS 市场 小张 葡萄 50 0.50
**/你要这个?
from table
group by 水果
(
公司 char(2),
部门 varchar(10),
姓名 varchar(20),
商品名称 varchar(10),
价格 int,
折扣 decimal(10,2)
)insert @t select 'PS','市场','小张','苹果',60,0.8
union all select 'PY','业务','小红','苹果',50,0.5
insert @t select 'PS','市场','小张','葡萄',50,0.5
union all select 'PY','业务','小红','葡萄',60,0.8
select *
from (
select *,rowid = row_number() over (partition by 公司,部门,姓名 order by 公司,部门,姓名,价格)
from @t) b
where rowid = 1/*公司 部门 姓名 商品名称 价格 折扣 rowid
---- ---------- -------------------- ---------- ----------- --------------------------------------- --------------------
PS 市场 小张 葡萄 50 0.50 1
PY 业务 小红 苹果 50 0.50 1(2 行受影响)
*/