求救。报表查询的SQL语句

LuckyZerg 2009-03-17 06:02:54
我有一张视图:test_view
字段有 : id(int),企业编号(varchar),名称(varchar),金额(float),时间(datetime),
内容有:
id 企业编号 名称 金额 时间
1 a123 哈哈企业 50 2007-11-04
2 a123 哈哈企业 100 2008-11-04
3 a123 哈哈企业 300 2008-11-04
4 a123 哈哈企业 200 2009-11-04
5 b321 呵呵企业 100 2007-11-04
6 b321 呵呵企业 50 2008-11-04
7 b321 呵呵企业 30 2008-11-04
8 b321 呵呵企业 20 2009-11-04
我需要做出这样一张报表。\
按照时间段1=“2007-11-04 到 2008-11-04之间” 时间段2=“2008-11-04 到 2009-11-04 之间查询”
id 企业编号 名称 时间段1的金额汇总 时间段2的金额汇总 时间段2比时间段1的增长额度 时间段2比时间段1的增长率
1 a123 哈哈企业 150 500 350 %250
2 b321 呵呵企业 150 100 -50 -%35
这个意思。请问SQL语句应该怎么写??
...全文
134 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
LuckyZerg 2009-03-18
  • 打赏
  • 举报
回复
早上起来顶一下。希望大大们帮帮忙。谢谢拉
阿非 2009-03-18
  • 打赏
  • 举报
回复

declare @table table([id] int,[pId] varchar(20),[name] varchar(20),[money] float,[date] datetime)

insert into @table select 1,'a123','哈哈企业',50,'2007-11-04'
union all select 2,'a123','哈哈企业',100,'2008-11-04'
union all select 3,'a123','哈哈企业',300,'2008-11-04'
union all select 4,'a123','哈哈企业',200,'2009-11-04'
union all select 5,'b321','呵呵企业',100,'2007-11-04'
union all select 6,'b321','呵呵企业',50,'2008-11-04'
union all select 7,'b321','呵呵企业',30,'2008-11-04'
union all select 8,'b321','呵呵企业',20,'2009-11-04'


select * from @table

select min(a.[id]) [id],a.[pId],a.[name],
(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2007-11-04' and '2008-11-04' ) sum1,
(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2008-11-04' and '2009-11-04' ) sum2,
(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2008-11-04' and '2009-11-04' )
-(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2007-11-04' and '2008-11-04') increase,
cast(cast(
((select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2008-11-04' and '2009-11-04' )
-(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2007-11-04' and '2008-11-04'))
/(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2008-11-04' and '2009-11-04' )*100 as int) as varchar(20))+'%' increase1
from @table a group by a.[pId],a.[name]

wuyq11 2009-03-17
  • 打赏
  • 举报
回复
通过LINQ实现与数据库连接,赋值给实体类,再查询。
wojiaochenglong 2009-03-17
  • 打赏
  • 举报
回复
三层的实体类?
LuckyZerg 2009-03-17
  • 打赏
  • 举报
回复
谢谢jiangshun 给的回复。还要请问上面所说的问题。请问如何动态的给三层的实体类赋值呢。因为有时候不想查询那么多的列。但是实体类已经写了 。或者有什么办法让实体类也动态起来呢??
浪子-无悔 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jiangshun 的回复:]
SQL code
----先看看这个有没报错
select id,企业编号,case when 时间 in(2007-11-04 ,2008-11-04 ) then sum(金额) else 0 as 时间段1的金额汇总,
case when 时间 in(2008-11-04 ,2009-11-04 ) then sum(金额) else 0 as 时间段2的金额汇总,from tab order by 企业编号
[/Quote]
LuckyZerg 2009-03-17
  • 打赏
  • 举报
回复
因为那个金额会有很多。比如
字段有 : id(int),企业编号(varchar),名称(varchar),金额_税款(float),金额_消费,时间(datetime)
并且我还得把汇总的合赋值给三层下的实体类。而且查询的时候可能需要按照某一项查询。或者全都要查询。请问怎么做呢?如果把所有的列全都写上查询。速度有会慢下来。
不知道我说的是否清楚。
jiangshun 2009-03-17
  • 打赏
  • 举报
回复

----先看看这个有没报错
select id,企业编号,case when 时间 in(2007-11-04 ,2008-11-04 ) then sum(金额) else 0 as 时间段1的金额汇总,
case when 时间 in(2008-11-04 ,2009-11-04 ) then sum(金额) else 0 as 时间段2的金额汇总,from tab order by 企业编号

zzxap 2009-03-17
  • 打赏
  • 举报
回复
去SQL版提问吧,哪里是又快又好。。。哇就一大堆了。。。

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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