Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cBllS-0002YK-76 for pgsql-admin@arkaria.postgresql.org; Tue, 29 Nov 2016 17:00:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cBllR-0005nP-QE for pgsql-admin@arkaria.postgresql.org; Tue, 29 Nov 2016 17:00:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cBllR-0005nC-2G for pgsql-admin@postgresql.org; Tue, 29 Nov 2016 17:00:17 +0000 Received: from lists.commandprompt.com ([69.168.55.30]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cBllI-0005v7-80 for pgsql-admin@postgresql.org; Tue, 29 Nov 2016 17:00:15 +0000 Received: from [192.168.1.60] (spinlock.commandprompt.com [69.168.55.30]) by lists.commandprompt.com (Postfix) with ESMTPA id D32BD1A7; Tue, 29 Nov 2016 09:00:05 -0800 (PST) Received: from c-73-225-27-179.hsd1.wa.comcast.net ([73.225.27.179] helo=[192.168.1.60]) by assp.commandprompt.com with SMTPSA(TLSv1_2 ECDHE-RSA-AES128-GCM-SHA256) (2.5.1); 29 Nov 2016 09:00:05 -0800 Subject: Re: How to query by month? To: taizi , pgsql-admin@postgresql.org References: <35257d1c.b253.158b06aaae5.Coremail.taizi2006blog@163.com> From: "Joshua D. Drake" Organization: Command Prompt, Inc. Message-ID: <642f883e-7f57-bf82-6f36-753d0e0eeb0d@commandprompt.com> Date: Tue, 29 Nov 2016 09:00:05 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <35257d1c.b253.158b06aaae5.Coremail.taizi2006blog@163.com> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 7bit X-Assp-Version: 2.5.1(16177) on assp.commandprompt.com X-Assp-ID: assp.commandprompt.com m1-38805-09760 X-Assp-Session: 7F017940C888 (mail 1) X-Assp-Original-Subject: Re: [ADMIN] How to query by month? X-Assp-Client-SSL: yes X-Pg-Spam-Score: -4.8 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org On 11/29/2016 06:08 AM, taizi wrote: > Hi all, > 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. select sum(order_amount) from table_name where date_part('month', order_date) = '$month'; Note that the $month will be the month number, not name. So for November, it will = 11. JD > thanks for any help! > Leopold > > > > > -- Command Prompt, Inc. http://the.postgres.company/ +1-503-667-4564 PostgreSQL Centered full stack support, consulting and development. Everyone appreciates your honesty, until you are honest with them. Unless otherwise stated, opinions are my own. -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin