select 手机号码,维修方式,维修费用=(select sum(price) from 维修费用表 where charindex('|'+convert(varchar,维修标志)+'|',a.维修方式)>0) from 手机维修表 a --每种维修方式的费用取出最大值:
select max(维修费用) from
select 手机号码,维修方式,维修费用=(select sum(price) from 维修费用表 where charindex('|'+convert(varchar,维修标志)+'|',a.维修方式)>0) from 手机维修表 a )aselect max(price) from 维修费用表 where convert(varchar,维修标志) in (select replace(维修方式,'|',',') from 手机维修表)
select max(维修费用) from
select 手机号码,维修方式,维修费用=(select sum(price) from 维修费用表 where charindex('|'+convert(varchar,维修标志)+'|',a.维修方式)>0) from 手机维修表 a )aselect max(price) from 维修费用表 where convert(varchar,维修标志) in (select replace(维修方式,'|',',') from 手机维修表)
select max(维修费用) from
select 手机号码,维修方式,维修费用=(select sum(price) from 维修费用表 where charindex('|'+convert(varchar,维修标志)+'|','|'+a.维修方式+'|')>0) from 手机维修表 a )a
是分出来,选出价格最大的那种 ,
是指徐震的这句:select 手机号码,维修方式,维修费用=(select max(price) from 维修费用表 where charindex('|'+convert(varchar,维修标志)+'|','|'+a.维修方式+'|')>0) from 手机维修表 a
sum改成max就好了。
from 手机维修表 a left join 维修费用表 b
on charindex('|'+cast(b.维修标志 as varchar)+'|','|'+a.维修方式+'|')>0
group by a.维修方式
declare @维修费用表 table(维修标志 int,维修方法 varchar(10),价格 int)
insert @维修费用表 select 1,'换主板' ,30
union all select 2,'换电池' ,20
union all select 3,'换充电器',20
union all select 4,'换键盘' ,30
union all select 5,'焊接' ,40
union all select 6,'换机' ,3declare @手机维修表 table(手机号码 int,维修方式 varchar(10))
insert @手机维修表 select 1,'3|4|1|2|'
union all select 2,'1|2'
union all select 3,'2|4|5'
union all select 4,'5|7|8'--查询
select 维修方式,最大价格=max(b.价格)
from @手机维修表 a left join @维修费用表 b
on charindex('|'+cast(b.维修标志 as varchar)+'|','|'+a.维修方式+'|')>0
group by a.维修方式/*--测试结果维修方式 最大价格
---------- -----------
1|2 30
2|4|5 40
3|4|1|2| 30
5|7|8 40(所影响的行数为 4 行)
--*/
declare @维修费用表 table(维修标志 int,维修方法 varchar(10),价格 int)
insert @维修费用表 select 1,'换主板' ,30
union all select 2,'换电池' ,20
union all select 3,'换充电器',20
union all select 4,'换键盘' ,30
union all select 5,'焊接' ,40
union all select 6,'换机' ,3declare @手机维修表 table(手机号码 int,维修方式 varchar(10))
insert @手机维修表 select 1,'3|4|1|2|'
union all select 2,'1|2'
union all select 3,'2|4|5'
union all select 4,'5|7|8'--查询
select 维修方式,最大价格=max(b.价格)
from @手机维修表 a left join @维修费用表 b
on charindex('|'+cast(b.维修标志 as varchar)+'|','|'+a.维修方式+'|')>0
group by a.维修方式/*--测试结果维修方式 最大价格
---------- -----------
1|2 30
2|4|5 40
3|4|1|2| 30
5|7|8 40(所影响的行数为 4 行)
--*/
from(
select 维修方式,最大价格=max(b.价格)
from 手机维修表 a left join 维修费用表 b
on charindex('|'+cast(b.维修标志 as varchar)+'|','|'+a.维修方式+'|')>0
group by a.维修方式
)a left join 维修费用表 b on a.最大价格=b.价格
from(
select 维修方式,最大价格=max(b.价格)
from 手机维修表 a left join 维修费用表 b
on charindex('|'+cast(b.维修标志 as varchar)+'|','|'+a.维修方式+'|')>0
group by a.维修方式
)a left join 维修费用表 b on a.最大价格=b.价格最大价格是由分隔出来的维修方式决定的,也就是说a.最大价格其实是由a中的维修方式分隔取出,其中价格最大的那个维修方式.
其中
select 手机号码,维修方式,维修费用=(select max(price) from 维修费用表 where charindex('|'+convert(varchar,维修标志)+'|','|'+a.维修方式+'|')>0) from 手机维修表 a已经基本满足了我要求,现在得到的是手机号码 维修方式 维修费用
1381381000 8|3|6|10 30
1381381001 4|5|9 20
1381381002 6|3 15
我要求:
手机号码 维修方式 维修费用
1381381000 8 30
1381381001 4 20
1381381002 6 15
.......................
象这样的效果
麻烦您了!
,维修方式=(select top 1 维修标志 from 维修费用表 where charindex('|'+convert(varchar,维修标志)+'|','|'+a.维修方式+'|')>0 order by price desc)
,维修费用=(select max(price) from 维修费用表 where charindex('|'+convert(varchar,维修标志)+'|','|'+a.维修方式+'|')>0)
from 手机维修表 a