日期:2014-05-17 浏览次数:20581 次
--drop table tb
create table tb(门店 varchar(10),商品 varchar(10),数量 int,销售额 numeric(10,2))
insert into tb
select 'A001','C001',1,1 union all
select 'A001','C002',2,2 union all
select 'A001','C003',3,3 union all
select 'A002','C001',1,1 union all
select 'A002','C003',3,3 union all
select 'A004','C004',4,4 union all
select 'A004','C002',2,2
go
;with t1
as
(
select *,
ROW_NUMBER() over(PARTITION by 门店 order by 销售额 desc) rownum
from tb
),
t2
as
(
select *,
门店+'
------解决方案--------------------
'+商品+'
------解决方案--------------------
'+cast(数量 as varchar)+'
------解决方案--------------------
'+cast(销售额 as varchar) as v
from t1
where rownum <= 5
)
select distinct
STUFF((select '
------解决方案--------------------
'+v from t2 b where a.门店 = b.门店 for xml path('')),
1,1,'') as v
from t2 a
/*
v
A001
------解决方案--------------------
C003
------解决方案--------------------
3
------解决方案--------------------
3.00
------解决方案--------------------
A001
------解决方案--------------------
C002
------解决方案--------------------
2
------解决方案--------------------
2.00
------解决方案--------------------
A001
------解决方案--------------------
C001
------解决方案--------------------
1
------解决方案--------------------