SELECT
SUBSTR(i.fdate,1,10) fdate
,Ftype
,SUM(i.Fdate_interest/100-i.Fpetty_date_ticket/1000000) fbase_interest
,SUM(Fpetty_date_ticket)/1000000 fticket_interest
FROM jz_snap.finance_db_t_finance_up_income i
join dp_fk_tmp.juzi_up_plan p on i.fplan_id = p.fplan_id
WHERE
1=1
AND i.Ftype = 1
AND i.Fdate >= '2018-04-01'
AND i.fdate <= '2018-12-31'
GROUP BY SUBSTR(i.fdate,1,10)
报错:
expression ‘i.Ftype
’ is neither present in the group by, nor is it an aggregate function. Add to group by or wrap in first() (or first_value) if you don’t care which value you get.
原因:
使用group by时
select的列,聚合函数前的所有列,都要放在group by之后。这些列条数相同即可!
更改后: (重点在最后一行)
SELECT
SUBSTR(i.fdate,1,10) fdate
,Ftype
,SUM(i.Fdate_interest/100-i.Fpetty_date_ticket/1000000) fbase_interest
,SUM(Fpetty_date_ticket)/1000000 fticket_interest
FROM jz_snap.finance_db_t_finance_up_income i
join dp_fk_tmp.juzi_up_plan p on i.fplan_id = p.fplan_id
WHERE
1=1
AND i.Ftype = 1
AND i.Fdate >= '2018-04-01'
AND i.fdate <= '2018-12-31'
GROUP BY SUBSTR(i.fdate,1,10),Ftype