When writing an SQL statement for a statistical query, you need to group by year, month, and day, but the fields that are required to be returned are date formats: yyyy mm month DD day. At first, my practice is to return to the year, month, day, and then manually splicing date, but also to determine whether the month and date is single digit, it is necessary to the left 0. After the project manager looked at my code, he told me that I could handle it directly in SQL. Here is the SQL statement:
SELECT CAST(stats_year AS character varying) || ‘年‘ || lpad(CAST(stats_month AS character varying), 2, ‘0‘) || ‘月‘ || lpad(CAST(stats_day AS character varying), 2, ‘0‘) || ‘日‘ AS statisticsTime,SUM(stats_value) AS count, ‘周‘ AS modeFROM t_cnitsec_attack_stats_hourWHERE stats_name=#{statsName} AND stats_date BETWEEN (now() - INTERVAL ‘12 months‘) AND now()GROUP BY statisticsTime
PostgreSQL merge date, month, and date left complement 0