Re: How to query by month?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



taizi wrote:
> im a beginner for SQL, and  trying to use it with my daily work.
> Now i catched this question(my title of mail).
> Suppose there is a table about orders, i want to sum the total amount of each month.
> There was a statement of mine to november.
> select  sum(order_amount) FROM table_name WHERE order_date between '2016-11-01' AND '2016-11-30';
> I know it comes a result what i want, but i hope someone can tell me another way to use.
> After searched, i found  there is a function named date_part, but it doesn't work.

You can use

  WHERE date_trunc('month', order_date) = '2016-11-01 00:00:00'

The downside is that this clause cannot use a simple index on
"order_date", you'd have to create an index on date_trunc('month', order_date).

Yours,
Laurenz Albe

-- 
Sent via pgsql-admin mailing list (pgsql-admin@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux