SQL语句报错

我想做个同比的数据集,但是总是报错。有没有大佬给我看看是啥情况呢

SELECT name,month,total_revenue ,

CONCAT(year, '-',IF(LENGTH(month) = 2, month, CONCAT('0', month)))as ym,concat(ifnull(round((a.total_revenue-b.total_revenue/b.total_revenue*100,2),0),"%") as '同比' 

FROM(

SELECT name,year,month,total_revenue from `tourist_scenic_data` 

WHERE year BETWEEN left('${date1}',4) and left('${date2}',4),

${if(len(jq)==0,"","and name = '" +jq+ "'")}

order by month asc) a

LEFT JOIN

( SELECT name,year,month,total_revenue from `tourist_scenic_data`) b

ON b.month = a.month and b.year = a.year-1

ORDER BY a.month asc

WHERE a.ym BETWEEN '${date1}' and '${date2}

FineReport yzmbTpn61525629 发布于 2023-4-17 12:53
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
0
CD20160914Lv8专家互助
发布于2023-4-17 12:58(编辑于 2023-4-17 13:02)

这样?如果有多个一样的字段名称,你要在前面把别名都带上的。不然它不知道你是哪个表的字段名称,还有排序要放在最后。。。where的后面

SELECT 

a.name,

a.month,

a.total_revenue ,

CONCAT(year, '-',IF(LENGTH(a.month) = 2, month, CONCAT('0', a.month))) as ym,

------应该要加括号吧猜测这里要改一下-----------

 concat(ifnull(round((a.total_revenue-b.total_revenue)/b.total_revenue*100,2),0),"%") as '同比' 

FROM(

SELECT name,year,month,total_revenue from `tourist_scenic_data` 

WHERE year BETWEEN left('${date1}',4) 

and left('${date2}',4),

${if(len(jq)==0,"","and name = '" +jq+ "'")}

order by month asc) a

LEFT JOIN

( SELECT name,year,month,total_revenue from `tourist_scenic_data`) b

ON b.month = a.month and b.year = a.year-1

WHERE a.ym BETWEEN '${date1}' and '${date2}

ORDER BY a.month asc

  • yzmbTpn61525629 yzmbTpn61525629(提问者) 还是不行啊,我放到sql里去查询显示1241 - Operand should contain 1 column(s)
    2023-04-17 13:59 
  • CD20160914 CD20160914 回复 yzmbTpn61525629(提问者) 我没有调试环境,也不知道你的数据格式。。。我只是大概看的你上面写的错误,其它的没办法调试,你可以先把ym更与同比列删除看
    2023-04-17 14:01 
  • 2关注人数
  • 181浏览人数
  • 最后回答于:2023-4-17 13:02
    请选择关闭问题的原因
    确定 取消
    返回顶部