求大佬帮帮忙

andyberg 2018-03-02 11:25:02

数据库中是这样
前端页面要这样展示

要求:
查询出每种商品今年每个月有多少条数据,总销量查询为每月销量总数(不限商品)
返回数据格式为JSON[["水果","apple",2,2,2,2,2,2,2,2,2,2,2,2],["水果","apple2",2,2,2,2,2,2,2,2,2,2,2,2],["总销量",333,333,333,333,333,333,333,333,333,333,333,333]]
...全文
558 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Miracle1216 2018-03-08
  • 打赏
  • 举报
回复
1、先截取reportTime字段到月份 2、再按照水果名称和新的月份字段分组 这样就可以了
无聊的上午 2018-03-05
  • 打赏
  • 举报
回复
select name,COUNT(id) cnt,sum(saleCount) sum,DATE_FORMAT(report,'%Y%m') m from tt group by m 如何接收查询结果不需要写吧,假设接收到的数据存在dt里面的 json数据封装:Map<String,Object> json = new HashMap<String,Object>(); json.put("name",dt.name); json.put("cnt",dt.cnt); json.put("sum",dt.sum); json.put("m",dt.m); 这边可以用json的工具转一下就可以了 其他代码不写了 写出来太多了
liaohao0101 2018-03-05
  • 打赏
  • 举报
回复
这个是Sql server的,供参考,通过使用case when 实现了行变列。
select belong,[name],sum(case when DATE_FORMAT(report,'%m')=1 then 1 else 0 end) as cnt1,
sum(case when DATE_FORMAT(report,'%m')=2 then 1 else 0 end) as cnt2,
sum(case when DATE_FORMAT(report,'%m')=3 then 1 else 0 end) as cnt3,
sum(case when DATE_FORMAT(report,'%m')=4 then 1 else 0 end) as cnt4,
sum(case when DATE_FORMAT(report,'%m')=5 then 1 else 0 end) as cnt5,
sum(case when DATE_FORMAT(report,'%m')=6 then 1 else 0 end) as cnt6
from tt group by belong,[name],cnt1 

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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