免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5137 | 回复: 7
打印 上一主题 下一主题

mysql环比怎么写? [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-07-14 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-07-10 17:13 |只看该作者 |倒序浏览
这是我写的sql语句:
select DATE_FORMAT(b.ACTUAL_TIME,'%Y') as 年度,
b.PAYMENT_DAYS as 月份,
b.PAY_MONEY as 本月缴费金额,
(select a.pay_money from tc_power_pay a where DATE_FORMAT(a.ACTUAL_TIME,'%Y')=2015
and a.payment_days=02 and a.BASE_STATION_ID=b.BASE_STATION_ID) as 上月份缴费金额,
(?) as 环比
from tc_power_pay b
where DATE_FORMAT(b.ACTUAL_TIME,'%Y')=2015 and  b.PAYMENT_DAYS=03 ORDER BY b.POWER_PAY_ID LIMIT 10;
在as环比前面的括号里面怎么写sql语句,使得环比的结果为:(这个月/上个月)/上个月*100/%,这样的结果

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
2 [报告]
发表于 2016-07-11 09:38 |只看该作者
回复 1# lf361196


    如果是用的英文做 as 名字,可以直接在语句里使用,但是用中文我没试过,你可以试一下看看效果怎样,求分享哦。
比如是这样:
  1. b.PAY_MONEY as this_month,
  2. (select a.pay_money from tc_power_pay a where DATE_FORMAT(a.ACTUAL_TIME,'%Y')=2015
  3. and a.payment_days=02 and a.BASE_STATION_ID=b.BASE_STATION_ID) as last_month,
  4. concat(format((this_month / last_month) / last_month * 100, 2), "%") as 环比
复制代码

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-07-14 06:20:00
3 [报告]
发表于 2016-07-11 16:00 |只看该作者
select DATE_FORMAT(b.ACTUAL_TIME,'%Y') as 年度,
b.PAYMENT_DAYS as 月份,
b.PAY_MONEY as this_month,
(select a.pay_money from tc_power_pay a where DATE_FORMAT(a.ACTUAL_TIME,'%Y')=2015 and
a.payment_days=02 and a.BASE_STATION_ID=b.BASE_STATION_ID) as last_month,
CONCAT((FORMAT(this_month /last_month) /last_month * 100,2), "%") as 环比率
from tc_power_pay b
where DATE_FORMAT(b.ACTUAL_TIME,'%Y')=2015 and  b.PAYMENT_DAYS=03 ORDER BY b.POWER_PAY_ID

用了你写的这行环比的sql语句,数据库执行的时候还是出现了报错,如下:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') /last_month * 100,2), "%") as 环比率
from tc_power_pay b
where DATE_FORMA' at line 6
请问这是什么报错?


   

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-07-14 06:20:00
4 [报告]
发表于 2016-07-11 16:11 |只看该作者
CONCAT((FORMAT(this_month-last_month) /last_month * 100,2), "%") as 环比率

应该是这个月减去上个月:this_month - last_month而不是this_month/last_month


   

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-07-14 06:20:00
5 [报告]
发表于 2016-07-11 16:23 |只看该作者
CONCAT((FORMAT(this_month-last_month)/last_month * 100,2), "%") as 环比率
我想问一下,你这里用format转换,format不是对日期进行格式化吗,而这里的this_month和last_month代表的是这个月的缴费金额跟上个月的缴费金额,是金额而不是日期格式,是不是有点问题呢?

   

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
6 [报告]
发表于 2016-07-11 18:07 |只看该作者
回复 5# lf361196


    报错的话,你那抄的抄错了啦,括号不对 当然我确实没有测试,出错很正常,你就参考一下用法而已。
你说的是 data_format 啦,这个用来操作时间的…… format 则不是。

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-07-14 06:20:00
7 [报告]
发表于 2016-07-12 11:30 |只看该作者
今天终于做出来了,我在你写的这个(b.PAY_MONEY as this_month,
(select a.pay_money from tc_power_pay a where DATE_FORMAT(a.ACTUAL_TIME,'%Y')=2015
and a.payment_days=02 and a.BASE_STATION_ID=b.BASE_STATION_ID) as last_month,
concat(format((this_month / last_month) / last_month * 100, 2), "%") as 环比 )  
基础上改动了一下:
b.PAY_MONEY as this_month,
(select a.pay_money from tc_power_pay a where DATE_FORMAT(a.ACTUAL_TIME,'%Y')=2015
and a.payment_days=02 and a.BASE_STATION_ID=b.BASE_STATION_ID) as last_month,
concat(format(((b.PAY_MONEY / (select a.pay_money from tc_power_pay a where DATE_FORMAT(a.ACTUAL_TIME,'%Y')=2015
and a.payment_days=02 and a.BASE_STATION_ID=b.BASE_STATION_ID))-1) * 100, 2), "%") as 环比
这就对了。

谢谢你提供的用法语句,非常感谢!


   

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
8 [报告]
发表于 2016-07-12 16:48 |只看该作者
回复 7# lf361196


    客气客气,恭喜~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP