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自定义报表使用存储过程时,使用系统的分组和小计、累计、合计
SAP HANA SQL 增加合计行
🏆本文收录于专栏,主要记录项目实战过程中所遇到的Bug或因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家!持续更新中,up!up!up!!
易语言利用SQL语句实现超级列表框 带合计行
超级列表框的合计行,一直令人比较头痛,得写好几行代码,加上循环相加才能计算出合计,实在是有些费劲。经研究,发现可以利用SQL语句,轻松实现 超级列表框合计行。@谢健。
用SQL实现统计报表中的小计与合计的方法详解
客户提出需求,针对某一列分组加上小计,合计汇总。网上找了一些有关SQL加合计的语句。都不是很理想。决定自己动手写。思路有三个:1.很多用GROUPPING和ROLLUP来实现。 优点:实现代码简洁,要求对GROUPPING和ROLLUP很深的理解。 缺点:低版本的Sql Server不支持。2.游标实现。 优点:思路逻辑简洁。 缺点:复杂和低效。3.利用临时表。 优点:思路逻辑简洁,执行效率高。SQL实现简单。 缺点:数据量大时耗用内存. 综合三种情况,决定“利用临时表”实现。实现效果原始表TB 加上小计,合计后效果 SQL语句 代码如下:select * into #TB f
使用SQL实现小计,合计以及排序
本篇文章是对SQL实现小计,合计以及排序进行了详细的分析介绍,需要的朋友参考下


疑难问题
