sql查询问题

xiaotupansy 2009-06-30 01:48:17
if object_id('ras_accountinfo') is not null
drop table ras_accountinfo;
create table ras_accountinfo(id int,companycode int,currentyear int);
insert into ras_accountinfo
select 1,1111001,2008
union select 2,1111001,2009
union select 3,1111002,2008
union select 4,1111002,2009

if object_id('ras_reportvalue') is not null
drop table ras_reportvalue;
create table ras_reportvalue(id int,datafactor varchar(100),datavalue decimal(10,2));
insert into ras_reportvalue
select 1,'aaa',1000 union
select 1,'bbb',2000 union
select 1,'ccc',3000 union
select 2,'aaa',4000 union
select 2,'bbb',5000 union
select 2,'ccc',6000 union
select 3,'aaa',1000 union
select 3,'bbb',2000 union
select 3,'ccc',3000 union
select 4,'aaa',4000 union
select 4,'bbb',5000 union
select 4,'ccc',6000


要出的结果是
1111001的2008 1111001的2009 相差数 百分比 1111002的2008 1111002的2009 相差数 百分比 (其他的单位,如果有的话)
aaa 1000 4000 3000 300% 1000 4000 3000 300%
bbb 2000 5000 3000 150% 2000 5000 3000 150%
ccc 3000 6000 3000 100% 3000 6000 3000 100%

companycode(单位)的数量可能有多家,年份是确定的,就是两年的比较
数据库是sql 2005
求sql语句
...全文
59 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaotupansy 2009-06-30
  • 打赏
  • 举报
回复
if object_id('tempdb.dbo.#temp') is not null
drop table #temp;
go
declare @currentyear int;
declare @sql varchar(max);
set @sql='select datafactor';
set @currentyear=2009;
create table #temp(companycode int,datafactor varchar(100),pre decimal(12,2),cur decimal(12,2));
with arg1 as(select a.id,a.companycode,a.currentyear,b.datafactor,b.datavalue
from ras_accountinfo a join ras_reportvalue b on a.id=b.id where currentyear=@currentyear)
,arg2 as (select a.id,a.companycode,a.currentyear,b.datafactor,b.datavalue
from ras_accountinfo a join ras_reportvalue b on a.id=b.id where currentyear=@currentyear-1)
insert into #temp select a.companycode,a.datafactor,b.datavalue as pre,a.datavalue as cur from arg1 a join arg2 b
on a.companycode=b.companycode and a.datafactor=b.datafactor
select @sql=@sql+',max(case when companycode='+cast(companycode as varchar(10))+' then pre end)
as pre'+cast(companycode as varchar(10))+',max(case when companycode='+cast(companycode as varchar(10))+' then cur end)
as cur'+cast(companycode as varchar(10))+',max(case when companycode='+cast(companycode as varchar(10))+' then cur-pre end)
as 相差'+cast(companycode as varchar(10))+',max(case when companycode='+cast(companycode as varchar(10))+'
then cast(cast(100*(cur-pre)/pre as decimal(10,2)) as varchar(100))+''%'' end) as percetange'+
cast(companycode as varchar(10)) from (select distinct companycode from #temp) as a
set @sql=@sql+' from #temp group by datafactor';
exec(@sql)


我真是自问自答的典范啊,残念啊
sharon8259 2009-06-30
  • 打赏
  • 举报
回复
确实负责,学习一下,join没怎么用
xiaotupansy 2009-06-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jiangshun 的回复:]
SQL codeselect[datafactor],[1111001 2008]=sum(casewhen[id]=1then[datavalue]else0end),[1111001 2009]=sum(casewhen[id]=2then[datavalue]else0end),[相差数]=sum(casewhen[id]=2then[datavalue]else0end)-sum(casewhen[id]=1then[datavalue]else0end),[百分比]=convert(varchar(6),(sum(casewhen[id]=2then[datavalue]else0end)-sum(casewhen[id]=1then[datavalue]else0end))/sum(casewhen[id]=1then[datavalue]else0end)*100)+'%',[1111002 2008]=sum(casewhen[id]=3then[datavalue]else0end),[1111002 2009]=sum(casewhen[id]=4then[datavalue]else0end),[相差数]=sum(casewhen[id]=4then[datavalue]else0end)-sum(casewhen[id]=3then[datavalue]else0end),[百分比]=convert(varchar(6),(sum(casewhen[id]=4then[datavalue]else0end)-sum(casewhen[id]=3then[datavalue]else0end))/sum(casewhen[id]=3then[datavalue]else0end)*100)+'%'from
ras_reportvaluegroupby[datafactor]/*
datafactor 1111001 2008 1111001 2009 相差数 百分比 1111002 2008 1111002 2009 相差数 百分比
---------------------------------------------------------------------------------------------------- ------------ ------------ ----------- ------- ------------ ------------ ----------- -------
aaa 1000 4000 3000 300% 1000 4000 3000 300%
bbb 2000 5000 3000 100% 2000 5000 3000 100%
ccc 3000 6000 3000 100% 3000 6000 3000 100%

(所影响的行数为 3 行)*/
[/Quote]

动态啊动态
残念~~~~~
jiangshun 2009-06-30
  • 打赏
  • 举报
回复

select
[datafactor],
[1111001 2008]=sum(case when [id]=1 then [datavalue] else 0 end),
[1111001 2009]=sum(case when [id]=2 then [datavalue] else 0 end),
[相差数]=sum(case when [id]=2 then [datavalue] else 0 end)-sum(case when [id]=1 then [datavalue] else 0 end),
[百分比]=convert(varchar(6),(sum(case when [id]=2 then [datavalue] else 0 end)-sum(case when [id]=1 then [datavalue] else 0 end))/sum(case when [id]=1 then [datavalue] else 0 end)*100)+'%',
[1111002 2008]=sum(case when [id]=3 then [datavalue] else 0 end),
[1111002 2009]=sum(case when [id]=4 then [datavalue] else 0 end),
[相差数]=sum(case when [id]=4 then [datavalue] else 0 end)-sum(case when [id]=3 then [datavalue] else 0 end),
[百分比]=convert(varchar(6),(sum(case when [id]=4 then [datavalue] else 0 end)-sum(case when [id]=3 then [datavalue] else 0 end))/sum(case when [id]=3 then [datavalue] else 0 end)*100)+'%'
from
ras_reportvalue
group by [datafactor]

/*
datafactor 1111001 2008 1111001 2009 相差数 百分比 1111002 2008 1111002 2009 相差数 百分比
---------------------------------------------------------------------------------------------------- ------------ ------------ ----------- ------- ------------ ------------ ----------- -------
aaa 1000 4000 3000 300% 1000 4000 3000 300%
bbb 2000 5000 3000 100% 2000 5000 3000 100%
ccc 3000 6000 3000 100% 3000 6000 3000 100%

(所影响的行数为 3 行)


*/
xiaotupansy 2009-06-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fredrickhu 的回复:]
引用 5 楼 xiaotupansy 的回复:
to 2楼
对你那个表来说,我要的结果就是

        张三08年 张三09年 比较  李四08年 李四09年 比较
语文
数学
物理


都有例子了 自己参照着写一下试试?
[/Quote]

就是写不出来啊,感觉有点混乱了
xiaotupansy 2009-06-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 drysea 的回复:]
SQL codeSELECT*FROM
(SELECT a.datafactor,a.datavalue,b.datavalueAS e,b.datavalue-a.datavalueAS a,LEFT(CAST((b.datavalue-a.datavalue)/a.datavalue*100ASVARCHAR(50)),CHARINDEX('.',CAST((b.datavalue-a.datavalue)/a.datavalue*100ASVARCHAR(50)),0)+2)+'%'AS bFROM ras_reportvalue aJOIN ras_reportvalue bON a.datafactor= b.datafactorWHERE a.id=1AND b.id=2) tLEFTJOIN
(SELECT a.datafactor,a.datavalue,b.datavalueAS f,b.datavalue-a.datavalueAS c,LEFT(CAST((b.datavalue-a.datavalue)/a.datavalue*100ASVARCHAR(50)),CHARINDEX('.',CAST((b.datavalue-a.datavalue)/a.datavalue*100ASVARCHAR(50)),0)+2)+'%'AS dFROM ras_reportvalue aJOIN ras_reportvalue bON a.datafactor= b.datafactorWHERE a.id=3AND b.id=4) sON t.datafactor= s.datafactor

学习中。。。
[/Quote]

你的代码是写死的,不和要求啊,我想要动态的,也就是id是不定的,是我通过其他的条件查询出来的,这里的1,2,3,4
只是我举例说明而已
比如说我要看08,09年的数据,我是根据年份09去数据库中查询出id来的
--小F-- 2009-06-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xiaotupansy 的回复:]
to 2楼
对你那个表来说,我要的结果就是

        张三08年 张三09年 比较  李四08年 李四09年 比较
语文
数学
物理
[/Quote]

都有例子了 自己参照着写一下试试?
xiaotupansy 2009-06-30
  • 打赏
  • 举报
回复
to 2楼
对你那个表来说,我要的结果就是

张三08年 张三09年 比较 李四08年 李四09年 比较
语文
数学
物理
drysea 2009-06-30
  • 打赏
  • 举报
回复

SELECT * FROM
(SELECT a.datafactor,a.datavalue,b.datavalue AS e,b.datavalue-a.datavalue AS a,
LEFT(CAST((b.datavalue-a.datavalue)/a.datavalue*100 AS VARCHAR(50)),CHARINDEX('.',CAST((b.datavalue-a.datavalue)/a.datavalue*100 AS VARCHAR(50)),0)+2)+'%' AS b
FROM ras_reportvalue a JOIN ras_reportvalue b ON a.datafactor = b.datafactor
WHERE a.id = 1 AND b.id = 2) t
LEFT JOIN
(SELECT a.datafactor,a.datavalue,b.datavalue AS f,b.datavalue-a.datavalue AS c,
LEFT(CAST((b.datavalue-a.datavalue)/a.datavalue*100 AS VARCHAR(50)),CHARINDEX('.',CAST((b.datavalue-a.datavalue)/a.datavalue*100 AS VARCHAR(50)),0)+2)+'%' AS d
FROM ras_reportvalue a JOIN ras_reportvalue b ON a.datafactor = b.datafactor
WHERE a.id = 3 AND b.id = 4) s
ON t.datafactor = s.datafactor


学习中。。。
xiaotupansy 2009-06-30
  • 打赏
  • 举报
回复
ras_accountinfo
id companycode currentyear
1 1111001 2008
2 1111001 2009
3 1111002 2008
4 1111002 2009
这张表中的一个id其实就是代表一张表,具体数据保存在另外一张表中
比如说id=1的数据,他代表的数据如下,所有单位的数据的结构都是一样的
datafactor datavalue
aaa 1000
bbb 2000
ccc 3000

我想做的起始就是对同一家单位的两年数据做个对比,然后单位数量可以有多家,并列列出来
也就是说
id为1,2是同一家单位的不同年份的数据
放在一起做个比较,结果的列的格式如下
id=1的数据 id=2的数据 做个比较 id=3的数据 id=4的数据 做个比较
--小F-- 2009-06-30
  • 打赏
  • 举报
回复
*
标题:普通行列转换(version 2.0)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-03-09
地点:广东深圳
说明:普通行列转换(version 1.0)仅针对sql server 2000提供静态和动态写法,version 2.0增加sql server 2005的有关写法。

问题:假设有张学生成绩表(tb)如下:
姓名 课程 分数
张三 语文 74
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94
想变成(得到如下结果):
姓名 语文 数学 物理
---- ---- ---- ----
李四 74 84 94
张三 74 83 93
-------------------
*/

create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go

--SQL SERVER 2000 静态SQL,指课程只有语文、数学、物理这三门课程。(以下同)
select 姓名 as 姓名 ,
max(case 课程 when '语文' then 分数 else 0 end) 语文,
max(case 课程 when '数学' then 分数 else 0 end) 数学,
max(case 课程 when '物理' then 分数 else 0 end) 物理
from tb
group by 姓名

--SQL SERVER 2000 动态SQL,指课程不止语文、数学、物理这三门课程。(以下同)
declare @sql varchar(8000)
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb) as a
set @sql = @sql + ' from tb group by 姓名'
exec(@sql)

--SQL SERVER 2005 静态SQL。
select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b

--SQL SERVER 2005 动态SQL。
declare @sql varchar(8000)
select @sql = isnull(@sql + '],[' , '') + 课程 from tb group by 课程
set @sql = '[' + @sql + ']'
exec ('select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b')

---------------------------------


两个表都行转列 然后left join一下OK了
claro 2009-06-30
  • 打赏
  • 举报
回复
没看懂

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧