PostgreSQL merge date, month, and date left complement 0

Source: Internet
Author: User
Tags postgresql

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.