我现在再做一个动态表头的报表,大概需求是这样子的。
图1是我现在查询出来的结果
图2是我希望将这个结果展现在前台的效果
月份有值的,取结果里边的值,没有值的默认为0,最后一列为动态列的合计,请问各位有没有什么好的实现方式啊
动态列的日期最好能是按照makeDate0,makeDate1,以此类推命名,因为需要前后台对应
http://www.cnblogs.com/binye-typing/p/5934202.html
可以参考这种写法 用with as
https://ask.csdn.net/questions/709877
with a as (select 姓名 ,成绩 from table where date的年=substr(sysdate,8,2) and date的月=substr(sysdate,4,2)),
b as (select 姓名 ,成绩 from table where date的年=substr(sysdate,8,2) and date的月=substr(sysdate,4,2)-1),
c as (select 姓名 ,成绩 from table where date的年=substr(sysdate,8,2)-1 and date的月=substr(sysdate,4,2))
select 姓名,a.成绩 今年本月,b.成绩 今年上月 ,c.成绩 去年本月 from a,b,c
sql server
declare @temp table(
makeDate varchar(50),
store_name varchar(50),
ven_name varchar(50),
pay_money decimal(18,4)
)
insert into @temp
select '2018-05','采购中心房库','1公司',10000
union all
select '2018-06','采购中心房库','1公司',10000
union all
select '2018-07','采购中心房库','1公司',10000
union all
select '2018-05','采购中心房库','2公司',10000
SELECT B.ven_name,'{'+stuff(strList,1,1,'')+'}' as result FROM (
SELECT ven_name,
(SELECT ',"'+makeDate+'":'+RTRIM(pay_money) FROM @temp
WHERE ven_name=A.ven_name
FOR XML PATH('')) AS strList
FROM @temp A
GROUP BY ven_name
) B
ven_name result
1公司 {"2018-05":10000.0000,"2018-06":10000.0000,"2018-07":10000.0000}
2公司 {"2018-05":10000.0000}
mysql
SET SESSION group_concat_max_len=2147483647;
select ven_name,
concat('{',group_concat(concat('"',makeDate,'"',':',pay_money) order by makeDate separator ','),'}') timedo
from @temp
group by ven_name
统计在页面进行,遍历json总会吧
declare @i as nvarchar(max)
select @i=isnull(@i,'')+'sum(case makeDate when '''+makeDate+''' then pay_money else 0 end) '''+replace(makeDate,'-','年')+'月'',' from [tb] group by makeDate order by makeDate
set @i=replace(@i+',',',,','')
exec ('select ven_name,'+@i+',sum(pay_money) ''合计'' from [tb] group by ven_name')