现有一个表数据如下:分店名称 日期 数量
大隗门市部 2005-12-18 00:00:00.000 11
大隗门市部 2005-12-19 00:00:00.000 12
大隗门市部 2005-12-20 00:00:00.000 16
牛庄门市部 2005-12-18 00:00:00.000 26
牛庄门市部 2005-12-19 00:00:00.000 21
牛庄门市部 2005-12-20 00:00:00.000 22
第n个门市部(数量不知道)想用一SQL语句实现如下表格:日期 合计 大隗门市部 牛庄门市部
2005-12-18 37 11 26
2005-12-19 33 12 21
2005-12-20 38 16 22想了半天不知该怎么做,分不多了,请兄弟们谅解一下,多帮帮忙吧!
大隗门市部 2005-12-18 00:00:00.000 11
大隗门市部 2005-12-19 00:00:00.000 12
大隗门市部 2005-12-20 00:00:00.000 16
牛庄门市部 2005-12-18 00:00:00.000 26
牛庄门市部 2005-12-19 00:00:00.000 21
牛庄门市部 2005-12-20 00:00:00.000 22
第n个门市部(数量不知道)想用一SQL语句实现如下表格:日期 合计 大隗门市部 牛庄门市部
2005-12-18 37 11 26
2005-12-19 33 12 21
2005-12-20 38 16 22想了半天不知该怎么做,分不多了,请兄弟们谅解一下,多帮帮忙吧!
解决方案 »
- 内存猛占用
- 我要使用LeftStr等函数 所以在uses中增加了StrUtils 为什么还是报错啊?我把USES和出错贴了出来,请大家帮我看下,谢谢。
- 有关 TModuleEntry32 的问题请教..
- 求数据库用的date类控件
- 爱情:有谁知道这个答案吗?
- 对treeview控件如何使用代码添加第一个节点??望各位大大帮忙解决
- 初学者的一个问题(关于更新DBGrid数据...)
- 大家是怎么备份重要数据的(比如源代码)
- 在treeview中,右键不能得到当前右键所选择selected的属性,高分
- 如何在TPANEL中填充背景?
- 关于数据连接,和局域网中搜索服务器的问题,急
- 求助:哪位前辈有高程考试的资料,请给小弟传点,小弟在这里谢过了:)
先select distinct 分店名称 from tb
再利用分店名称合成sql
select 日期,sum(decode(分店名称,'大隗门市部'))+sum(decode(分店名称,'牛庄门市部))…… as 合计,sum(decode(分店名称,'大隗门市部')) as 大隗门市部,sum(decode(分店名称,'牛庄门市部')) as 牛庄门市部 ……
group by ...
CREATE PROCEDURE [dbo].[Sp_JB_GL_ItemBanlance_AR]
ASDeclare @ncurrYear Int
Declare @ncurrMon Int
Declare @k Int
Declare @J Int
Declare @Cr nVarChar(80)
Declare @Dr nVarChar(80)
Declare @SQL nVarChar(4000)
Declare @SQL_a nVarChar(200)
Declare @myStr nVarChar(2000)Set NoCount onSelect @ncurrYear = ncurryear From sys_subsysinstalled where csubsyscode = 'GL'
Select @ncurrMon = ncurrmon From sys_subsysinstalled where csubsyscode = 'GL'CREATE TABLE #temp_a ([客户代码] nVarChar(20) NOT NULL, [客户名称] nVarChar(100) NOT NULL)
Set @myStr = '[客户代码], [客户名称]'IF @ncurrMon >= 1
Begin
Set @k = 1
While @k <= @ncurrMon
Begin
Set @Cr = Convert(Char(2), @k)+'月贷方'
Set @myStr = @myStr + ', IsNull(Sum([' + @Cr + ']), 0) as [' + @Cr + ']'
Set @SQL = 'ALTER TABLE #temp_a ADD [' + @Cr + '] numeric(20,2) Not NULL Default 0'
exec sp_executesql @SQL--, N'@Cr nVarChar(80) out', @Cr out Set @Dr = Convert(Char(2), @k)+'月借方'
Set @myStr = @myStr + ', IsNull(Sum([' + @Dr + ']), 0) as [' + @Dr + ']'
Set @SQL = 'ALTER TABLE #temp_a ADD [' + @Dr + '] numeric(20,2) Not NULL Default 0'
exec sp_executesql @SQL--, N'@Dr nVarChar(80) out', @Dr out
Set @SQL_a = ' '
Set @SQL = 'Insert Into #temp_a Select b.cCusCode, b.cCusName '
Set @J = @k
While @J -1 >0
Begin
Set @SQL_a = @SQL_a + ' , 0, 0 '
Set @J = @J - 1
End
Set @SQL = @SQL + @SQL_a
Set @SQL = @SQL + ', Sum(a.nCrAmt) , Sum(a.nDrAmt) '
-- IF @k = @ncurrMon
Set @SQL = @SQL + ' From GL_ItemBanlance a, AR_Customer b '
-- Else
-- Set @SQL = @SQL + ' From GL_ItemBanlance_H a, AR_Customer b '
Set @SQL = @SQL + ' Where a.nCusId = b.id '
Set @SQL = @SQL + ' and a.nCusId <> 0 and (a.nCrAmt <> 0 or a.nDrAmt <> 0) '
Set @SQL = @SQL + ' and a.nAccY = ' + Str(@ncurrYear) + ' and a.nAccM = ' + Str(@k)
Set @SQL = @SQL + ' and a.nAccId in (Select Id From GL_Account Where Code like ''1131%'') '
Set @SQL = @SQL + ' Group by b.cCusCode, b.cCusName '
Exec sp_executesql @SQL Set @k = @k + 1
End
End--余额
ALTER TABLE #temp_a ADD [本月余额] numeric(20,2) Not NULL Default 0
Set @myStr = @myStr + ', IsNull(Sum([本月余额]), 0) as [本月余额]'Set @SQL = 'Insert Into #temp_a Select b.cCusCode, b.cCusName '
Set @SQL = @SQL + @SQL_a + ', 0, 0, Sum(a.nCloseBalan) From GL_ItemBanlance a, AR_Customer b '
Set @SQL = @SQL + ' Where a.nCusId = b.id and a.nCusId <> 0 and a.nCloseBalan <> 0 '
Set @SQL = @SQL + ' and a.nAccY = ' + Str(@ncurrYear) + ' and a.nAccM = ' + Str(@ncurrMon)
Set @SQL = @SQL + ' and a.nAccId in (Select Id From GL_Account Where Code like ''1131%'')'
Set @SQL = @SQL + ' Group by b.cCusCode, b.cCusName'
Exec sp_executesql @SQLSet @SQL = 'Select ' + @myStr + ' From #temp_a Group by [客户代码], [客户名称]'
Exec sp_executesql @SQL
GO
select distinct 分店名称 from 表
来动态生成字段个数,然后做统计就很简单了吧呵呵
--目标
/*
日期 合计 大隗门市部 牛庄门市部
2005-12-18 37 11 26
2005-12-19 33 12 21
2005-12-20 38 16 22
*/
Create Table 表(分店名称 varchar(10),日期 varchar(50),数量 varchar(10))
--插入数据
insert into 表
select '大隗门市部','2005-12-18 00:00:00.000','11' union
select '大隗门市部','2005-12-19 00:00:00.000','12' union
select '大隗门市部','2005-12-20 00:00:00.000','16' union
select '牛庄门市部','2005-12-18 00:00:00.000','26' union
select '牛庄门市部','2005-12-19 00:00:00.000','21' union
select '牛庄门市部','2005-12-20 00:00:00.000','22'
--select * from 表
--测试语句
select left(日期,10),合计=count(*),sum(case 分店名称 when '大隗门市部' then 1 else 0 end) [大隗门市部],
sum(case 分店名称 when '牛庄门市部' then 1 else 0 end) [牛庄门市部]from 表
group by left(日期,10)
--删除测试环境
Drop Table 表
declare @str varchar(8000)
declare @sql varchar(8000)
delcare @a varchar(20)
delcare @b varchar(20)
delcare @c varchar(20)
delcare @d float
--生成表结构
Create table #temp1(intID int identity(1,1))
declare mycursor for select 分店名称 from tablename group by 分店名称
open mycursor
fecth next from mycursor into @a
while @@fetch_status=0
begin
set @sql='alter table #temp1 add ['+@a+'] varchar(20) null'
exec(@sql)
fetch next from mycursor into @a
end
close mycursor
deallocate mycursor
/*写入数据*/
/*声明游标(行数)*/
declare cur1 cursor for select distinct 分店名称,日期,sum(数量) from tablename group by 分店名称,日期
/*打开游标*/
open cur1
/*读取数据*/
fetch next from cur1 into @b,@c,@d
while @@Fetch_status=0
begin
insert into #temp1 select 分店名称,日期,sum(数量) as 数量 from tablename group by 分店名称,日期
/*声明游标(列数)*/
declare cur2 cursor for select 分店名称 from tablename group by 分店名称
open cur2
fetch next from cur2 into @a
while @@fetch_status=0
begin
set @sql='update #temp1 set ['+@a+']=(select MpspFact from
Mpsp where 分店名称='''+@b+'''and 日期 ='''
+@c+'''and 数量='''+@d+''')
print(@sql)
exec(@sql)
fetch next from cur2 into @a
end
/*关闭游标cur2*/
close cur2
/*释放游标cur1*/
deallocate cur2
fetch next from cur1 into @b,@c,@d
end
/*关闭游标cur1*/
close cur1
/*释放游标cur1*/
deallocate cur1
select * from #temp1