这个SQL语句怎么些
有一张表商品编码 进价 日期
001 10.2 2007-10-2
001 9.8 2007-10-3
001 10.5 2007-10-4
002 1.6 2007-10-5
002 2.1 2007-10-6
002 1.8 2007-10-8
得到结果商品编码 最近进价1 最近进价2 最近进价3
001 10.5 9.8 10.2
002 1.8 2.1 1.6
有一张表商品编码 进价 日期
001 10.2 2007-10-2
001 9.8 2007-10-3
001 10.5 2007-10-4
002 1.6 2007-10-5
002 2.1 2007-10-6
002 1.8 2007-10-8
得到结果商品编码 最近进价1 最近进价2 最近进价3
001 10.5 9.8 10.2
002 1.8 2.1 1.6
产品编号 产品名称 销售时间 数量
1501 A 2007-11-10 50
1501 A 2007-11-12 20
1502 B 2007-11-11 30
1502 B 2007-11-12 20
1502 B 2007-11-13 40
1503 C 2007-11-10 40
1524 C 2007-11-12 30 怎么用SQL语句得到下面的结果:
产品编号 产品名称 销售时间 数量 销售时间 数量 销售时间 数量
1501 A 2007-11-10 50 2007-11-12 20
1501 B 2007-11-11 30 2007-11-12 20 2007-11-13 40
1502 C 2007-11-10 40 2007-11-12 30
create table tb(产品编号 varchar(10) ,产品名称 varchar(10),销售时间 datetime,数量 int)
insert tb select '1501' , 'A' , '2007-11-10' , 50
insert tb select '1501' , 'A' , '2007-11-12' , 20
insert tb select '1502' , 'B' , '2007-11-11' , 30
insert tb select '1502' , 'B' , '2007-11-12' , 20
insert tb select '1502' , 'B' , '2007-11-13' , 40
insert tb select '1503' , 'C' , '2007-11-10' , 40
insert tb select '1503' , 'C' , '2007-11-12' , 30DECLARE @SQL VARCHAR(8000)
SET @SQL='SELECT 产品编号,产品名称'
SELECT @SQL=@SQL+',MAX(CASE WHEN PX='+rtrim(px)+' THEN CONVERT(CHAR(10),销售时间,120) ELSE '''' END ) [销售时间],
SUM(CASE WHEN PX='+rtrim(px)+' THEN 数量 ELSE 0 END ) [数量]'
FROM(SELECT DISTINCT PX=(SELECT COUNT(*) FROM tb where a.产品编号=产品编号 and 产品名称=a.产品名称 and 销售时间<a.销售时间)+1 FROM tb a) TSET @SQL=@SQL+' FROM (SELECT *,PX=(SELECT COUNT(*) FROM tb where a.产品编号=产品编号 and 产品名称=a.产品名称 and 销售时间<a.销售时间)+1 FROM tb a) T GROUP BY 产品编号,产品名称'EXEC (@SQL)DROP TABLE tb/*
产品编号 产品名称 销售时间 数量 销售时间 数量 销售时间 数量
---------- ---------- ---------- ----------- ---------- ----------- ---------- -----------
1501 A 2007-11-10 50 2007-11-12 20 0
1502 B 2007-11-11 30 2007-11-12 20 2007-11-13 40
1503 C 2007-11-10 40 2007-11-12 30 0(3 行受影响)
*/
insert ta select
'001',10.2,'2007-10-2' union select
'001', 9.8 ,'2007-10-3' union select
'001', 10.5 ,'2007-10-4' union select
'002', 1.6 ,'2007-10-5' union select
'002', 2.1 ,'2007-10-6' union select
'002', 1.8 ,'2007-10-8'godeclare @s varchar(8000)
select @s = isnull(@s+',','')+'[ 最近进价'+ltrim(px)+']=max(case when px = '+ltrim(px)+' then 进价 else 0 end)'
from
( select distinct px from
(
select *,px= (select count(distinct 进价)+1 from ta where 商品编码 = a.商品编码 and 进价 < a.进价)
from ta a) b) b--print @sexec('select 商品编码,'+@s+ ' from (
select *,px= (select count(distinct 进价)+1 from ta where 商品编码 = a.商品编码 and 进价 < a.进价)
from ta a) b group by 商品编码')drop table ta/*
商品编码 最近进价1 最近进价2 最近进价3
---------- -------------- -------------- --------------
001 9.80 10.20 10.50
002 1.60 1.80 2.10*/
create table ta(商品编码 varchar(10),进价 decimal(12,2),日期 datetime)
insert ta select
'001',10.2,'2007-10-2' union select
'001', 9.8 ,'2007-10-3' union select
'001', 10.5 ,'2007-10-4' union select
'002', 1.6 ,'2007-10-5' union select
'002', 2.1 ,'2007-10-6' union select
'002', 1.8 ,'2007-10-8'godeclare @s varchar(8000)
select @s = isnull(@s+',','')+'[ 最近进价'+ltrim(px)+']=max(case when px = '+ltrim(px)+' then 进价 else 0 end)'
from
( select distinct px from
(
select *,px= (select count(distinct 进价)+1 from ta where 商品编码 = a.商品编码 and 日期 < a.日期)
from ta a) b) b--print @sexec('select 商品编码,'+@s+ ' from (
select *,px= (select count(distinct 进价)+1 from ta where 商品编码 = a.商品编码 and 日期 < a.日期)
from ta a) b group by 商品编码')drop table ta/*
商品编码 最近进价1 最近进价2 最近进价3
---------- -------------- -------------- --------------
001 10.20 9.80 10.50
002 1.60 2.10 1.80*/
insert tb select
'001',10.2,'2007-10-2' union select
'001', 9.8 ,'2007-10-3' union select
'001', 10.5 ,'2007-10-4' union select
'002', 1.6 ,'2007-10-5' union select
'002', 2.1 ,'2007-10-6' union select
'002', 1.8 ,'2007-10-8'select 商品编码,
max(case px when 1 then 进价 else 0 end) 最近进价1,
max(case px when 1 then 进价 else 0 end) 最近进价2,
max(case px when 1 then 进价 else 0 end) 最近进价3
from
(
select * , px =(select count(*) from tb where 商品编码 = t.商品编码 and 日期 > t.日期) + 1 from tb t
) m
group by 商品编码drop table tb借用石头的测试数据.