sql存储过程中合计的问题
存储过程如下:
1.这个是根据时间查询的
declare @sql varchar(4000)
set @sql='select hyh,LargeBodyColorID,ColorID,SizeID,sum(Quantity) as sumcol '
select @sql=@sql+',sum(case DateTate when '''+RTRIM(DateTate)+''' then Quantity else 0 end) as ['+RTRIM(DateTate)+'] ' from (select distinct DateTate from CuttingPlan where DateTate<='10-10-18' and DateTate>='10-10-12') as a
set @sql=@sql+' from CuttingPlan c where DateTate<=''10-10-18'' and DateTate>=''10-10-12'' group by hyh, LargeBodyColorID,ColorID,SizeID union select OrderID=''合计'',LargeBodyColorID='''',ColorID='''',SizeID='''',sumcol=sum(Quantity)'
select @sql=@sql+',sum(case DateTate when '''+RTRIM(DateTate)+''' then Quantity else 0 end) as ['+RTRIM(DateTate)+'] ' from (select distinct DateTate from CuttingPlan where DateTate<='10-10-18' and DateTate>='10-10-12') as b
set @sql=@sql+' from CuttingPlan as d where d.DateTate<=''10-10-18'' and d.DateTate>=''10-10-12'''
exec(@sql)
print(@sql)
2.这个是查询所有的没有条件的
declare @sql varchar(4000)
set @sql='select hyh,LargeBodyColorID,ColorID,SizeID,sum(Quantity) as sumcol '
select @sql=@sql+',sum(case DateTate when '''+RTRIM(DateTate)+''' then Quantity else 0 end) as ['+RTRIM(DateTate)+'] ' from (select distinct top 7 DateTate from CuttingPlan ) as a
set @sql=@sql+' from CuttingPlan c group by hyh, LargeBodyColorID,ColorID,SizeID union select hyh=''合计'',LargeBodyColorID='''',ColorID='''',SizeID='''',sumcol=sum(Quantity)'
select @sql=@sql+',sum(case DateTate when '''+RTRIM(DateTate)+''' then Quantity else 0 end) as ['+RTRIM(DateTate)+'] ' from (select distinct top 7 DateTate from CuttingPlan ) as b
set @sql=@sql+' from CuttingPlan as d '
exec(@sql)
print(@sql)
这个是第一个存储过程的结果
hyh LargeBodyColorID ColorID SizeID sumcol 10-10-12 10-10-13 10-10-14 10-10-15 10-10-16 10-10-17 10-10-18
1 浓芷青 A25EF1E3-2C3B-4F6D-B617-1901CCF75743 M 28 4 4 4 4 4 4 4
2 芷青 AE91D58F-2995-4309-A570-0BB5E947447C M 294 42 42 42 42 42 42 42
合计 322 46 46 46 46 46 46 46
这个是第二个存储过程的结果
hyh LargeBodyColorID ColorID SizeID sumcol 10-09-27 10-09-28 10-09-29 10-09-30 10-10-01 10-10-02 10-10-03
1 浓芷青 A25EF1E3-2C3B-4F6D-B617-1901CCF75743 M 156 4 4 4 4 4 4 4
2 芷青 AE91D58F-2995-4309-A570-0BB5E947447C M 1470 42 42 42 42 42 42 42
合计 1626 46 46 46 46 46 46 46
现在的问题两个存储过程查询出来的suncol是不一样的。
怎么能让第一个的存储过程的结果和第二个的结果是一样的。
1.这个是根据时间查询的
declare @sql varchar(4000)
set @sql='select hyh,LargeBodyColorID,ColorID,SizeID,sum(Quantity) as sumcol '
select @sql=@sql+',sum(case DateTate when '''+RTRIM(DateTate)+''' then Quantity else 0 end) as ['+RTRIM(DateTate)+'] ' from (select distinct DateTate from CuttingPlan where DateTate<='10-10-18' and DateTate>='10-10-12') as a
set @sql=@sql+' from CuttingPlan c where DateTate<=''10-10-18'' and DateTate>=''10-10-12'' group by hyh, LargeBodyColorID,ColorID,SizeID union select OrderID=''合计'',LargeBodyColorID='''',ColorID='''',SizeID='''',sumcol=sum(Quantity)'
select @sql=@sql+',sum(case DateTate when '''+RTRIM(DateTate)+''' then Quantity else 0 end) as ['+RTRIM(DateTate)+'] ' from (select distinct DateTate from CuttingPlan where DateTate<='10-10-18' and DateTate>='10-10-12') as b
set @sql=@sql+' from CuttingPlan as d where d.DateTate<=''10-10-18'' and d.DateTate>=''10-10-12'''
exec(@sql)
print(@sql)
2.这个是查询所有的没有条件的
declare @sql varchar(4000)
set @sql='select hyh,LargeBodyColorID,ColorID,SizeID,sum(Quantity) as sumcol '
select @sql=@sql+',sum(case DateTate when '''+RTRIM(DateTate)+''' then Quantity else 0 end) as ['+RTRIM(DateTate)+'] ' from (select distinct top 7 DateTate from CuttingPlan ) as a
set @sql=@sql+' from CuttingPlan c group by hyh, LargeBodyColorID,ColorID,SizeID union select hyh=''合计'',LargeBodyColorID='''',ColorID='''',SizeID='''',sumcol=sum(Quantity)'
select @sql=@sql+',sum(case DateTate when '''+RTRIM(DateTate)+''' then Quantity else 0 end) as ['+RTRIM(DateTate)+'] ' from (select distinct top 7 DateTate from CuttingPlan ) as b
set @sql=@sql+' from CuttingPlan as d '
exec(@sql)
print(@sql)
这个是第一个存储过程的结果
hyh LargeBodyColorID ColorID SizeID sumcol 10-10-12 10-10-13 10-10-14 10-10-15 10-10-16 10-10-17 10-10-18
1 浓芷青 A25EF1E3-2C3B-4F6D-B617-1901CCF75743 M 28 4 4 4 4 4 4 4
2 芷青 AE91D58F-2995-4309-A570-0BB5E947447C M 294 42 42 42 42 42 42 42
合计 322 46 46 46 46 46 46 46
这个是第二个存储过程的结果
hyh LargeBodyColorID ColorID SizeID sumcol 10-09-27 10-09-28 10-09-29 10-09-30 10-10-01 10-10-02 10-10-03
1 浓芷青 A25EF1E3-2C3B-4F6D-B617-1901CCF75743 M 156 4 4 4 4 4 4 4
2 芷青 AE91D58F-2995-4309-A570-0BB5E947447C M 1470 42 42 42 42 42 42 42
合计 1626 46 46 46 46 46 46 46
现在的问题两个存储过程查询出来的suncol是不一样的。
怎么能让第一个的存储过程的结果和第二个的结果是一样的。
...全文
相关推荐
用友U8自定义报表使用存储过程时,使用系统的分组和小计、累计、合计
在用友U8中,自定义报表可以使用存储过程来生成报表,而不需要编写复杂的SQL语句。存储过程可以根据用户的输入参数来生成报表,而用户可以根据需要来设置报表的条件和格式。
SAP HANA SQL 增加合计行
🏆本文收录于专栏,主要记录项目实战过程中所遇到的Bug或因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家!持续更新中,up!up!up!!
易语言利用SQL语句实现超级列表框 带合计行
此外,如果你的程序需要频繁进行此类计算,可以考虑优化数据库设计,例如添加预计算的合计字段,或者使用数据库的存储过程来进一步减少应用程序的负担。
用SQL实现统计报表中的小计与合计的方法详解
在SQL中,生成统计报表,特别是包含小计与合计的部分,是常见的需求。
使用SQL实现小计,合计以及排序
在SQL(Structured Query Language)中,小计、合计与排序是数据分析和报表生成的重要操作。本文将详细解析如何使用SQL来实现这些功能。


疑难问题
