select 编码 as 编码,名称 as 名称 max(case 日期 when '0天' then 金额 else 0 end) as 即期,max(case 日期 when '30天' then 金额 else 0 end) as 三十天, max(case 日期 when '60天' then 金额 else 0 end) as 六十天,max(case 日期 when '90天' then 金额 else 0 end) as 九十天,max(case 日期 when '120天' then 金额 else 0 end) as 一百二十天 from LastResultTempTable group by 编码,名称
这个是行转列的,问题是,这个表是这样的
编码 名称 日期 金额
A 01 0天 2
A 01 30天 3
B 02 0天 4
B 02 60天 5
就是A和B的日期不是0天,30天,60天,90天,120天都存在数据的我先把数据根据A和B自己算 然后再用行转列得出来的表再算一次,数据不一样,这里原因是不是因为 就是A和B的日期不是0天,30天,60天,90天,120天都存在数据的 导致的呢,应该怎么改好呢??
这个是行转列的,问题是,这个表是这样的
编码 名称 日期 金额
A 01 0天 2
A 01 30天 3
B 02 0天 4
B 02 60天 5
就是A和B的日期不是0天,30天,60天,90天,120天都存在数据的我先把数据根据A和B自己算 然后再用行转列得出来的表再算一次,数据不一样,这里原因是不是因为 就是A和B的日期不是0天,30天,60天,90天,120天都存在数据的 导致的呢,应该怎么改好呢??
A 01 0天 2
A 01 30天 3
B 02 0天 4
B 02 60天 5转成
编码 名称 0天 30天 60天
A 01 2 3 0
B 02 4 0 5
sql语句:
select 编码 as 编码,名称 as 名称 max(case 日期 when '0天' then 金额 else 0 end) as 即期,max(case 日期 when '30天' then 金额 else 0 end) as 三十天, max(case 日期 when '60天' then 金额 else 0 end) as 六十天 from LastResultTempTable group by 编码,名称
这样好像不行
--行列互换/*--有表
indust 200301 200302 200303
---------- ---------- ---------- ----------
a 111 222 333
b 444 555 666
c 777 888 999
d 789 910 012
--要求得到结果
日期 a b c d
------ ---- ---- ---- ----
200301 111 444 777 789
200302 222 555 888 910
200303 333 666 999 012
--*/--创建测试表
create table test(indust varchar(10)
,[200301] varchar(10)
,[200302] varchar(10)
,[200303] varchar(10))
insert test select 'a','111','222','333'
union all select 'b','444','555','666'
union all select 'c','777','888','999'
union all select 'd','789','910','012'
go--数据处理
declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000)
select @f1='',@f2='',@f3=''
select @f1=@f1+',['+indust+']='''+[200301]+''''
,@f2=@f2+','''+[200302]+''''
,@f3=@f3+','''+[200303]+''''
from test
exec('select 日期=''200301'''+@f1
+' union all select ''200302'''+@f2
+' union all select ''200303'''+@f3)
go
--删除测试表
select * from test
drop table test/*--测试结果
日期 a b c d
------ ---- ---- ---- ----
200301 111 444 777 789
200302 222 555 888 910
200303 333 666 999 012
--*/
into #sum3 from #sum21 as ShipNonTable full join #sum22 as RMANonTable on ShipNonTable.客户名称=RMANonTable.客户名称 and ShipNonTable.客户编码=RMANonTable.客户编码 and ShipNonTable.日期=RMANonTable.日期 where (ISNULL(ShipNonTable.金额,0)+ISNULL(RMANonTable.金额,0))!=0
select 组织编码 as 组织编码,组织名称 as 组织名称,客户编码 as 客户编码,客户名称 as 客户名称,结算期 as 结算期, max(case 日期 when '0天' then 金额 else 0 end) as 即期,max(case 日期 when '30天' then 金额 else 0 end) as 三天, max(case 日期 when '60天' then 金额 else 0 end) as 六天,max(case 日期 when '90天' then 金额 else 0 end) as 九天,max(case 日期 when '120天' then 金额 else 0 end) as 十天
into #sum4 from #sum3 group by 组织编码,组织名称,客户编码,客户名称,结算期1.select 组织编码,组织名称,sum(即期),sum(三天),sum(六天),sum(九天),sum(十天) from #sum4 group by 组织编码,组织名称2.select 组织编码,组织名称,日期,SUM(金额) from #sum3 group by 组织编码,组织名称,日期
正常的话,1和2查出来的信息对比起来应该不会错的,但是,这里查出来的数据就对不上了