Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YEnBx-0002rm-BW for pgsql-sql@arkaria.postgresql.org; Fri, 23 Jan 2015 22:59:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YEnBw-0007rF-4U for pgsql-sql@arkaria.postgresql.org; Fri, 23 Jan 2015 22:59:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YEnBv-0007r7-B2 for pgsql-sql@postgresql.org; Fri, 23 Jan 2015 22:59:03 +0000 Received: from mail-ob0-x22f.google.com ([2607:f8b0:4003:c01::22f]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YEnBn-0008Hu-5v for pgsql-sql@postgresql.org; Fri, 23 Jan 2015 22:59:01 +0000 Received: by mail-ob0-f175.google.com with SMTP id wp4so127018obc.6 for ; Fri, 23 Jan 2015 14:58:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=d3cNYj4f1yfqjYhhuvsgI1hBv04SUfGWL/MHNJljGLQ=; b=Cf2IwpnirwV1mBzec8SmEZIRlYG6KTQQH5yy5/JVUUSpocmr1WWEWNZmQKLJCG99yO r56l/YE+dfX2tRP/XxnbgKe5vPq5NOv4IfarWE6eKb74J8f7JXe4FWb6wlKD6vO8CBrj xJKgaAi0MNyW13yCcBA+eOAo6r1EWPXVlZevhAQ5xI4WV5IUR32IMvn0l/yo+dLx/lPd tzhPR9ZSN41GxGJ64wqHzJEByd5TOXZ4gE6iU++cNoojnE6PQTEHnz3yliYqvPqVw+XU D0suw1yAhtbz7jPvVeM12PrmUdp9foyf0fdFHOja9h+njcbIZcIrWxyEMXo3H8UsRHWh FoIQ== X-Received: by 10.202.50.136 with SMTP id y130mr5637352oiy.91.1422053933192; Fri, 23 Jan 2015 14:58:53 -0800 (PST) Received: from [127.0.0.1] (mail.jonesborocwl.org. [64.233.145.118]) by mx.google.com with ESMTPSA id uv10sm1493687obc.27.2015.01.23.14.58.52 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Jan 2015 14:58:52 -0800 (PST) Message-ID: <54C2D22B.8050605@gmail.com> Date: Fri, 23 Jan 2015 16:58:51 -0600 From: Jason Aleski User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Better way to compute moving averages? Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org I've been asked compute various moving averages of end of day sales by store. I can do this for all rows with no problem (same query without the WHERE clause towards the end). That query took 10-15 minutes to run over approx 3.4 million rows. I'm sure they will want this information to be added to the daily end of day reports. I can run the query below (excluding the WHERE clause) but it takes almost as long to run one day as it does the entire dataset. It looks like when I do the inner select, it is still running over the entire dataset. I have added a "WHERE eod_ts > CURRENT_TIMESTAMP - INTERVAL '365 days'" (as below) to the inner query, which allows the query to run between 1-2 minutes. Question 1) This seems to work, but was curious if there is a better way. Question 2) Is there a way to specify a date, instead of using current date and current_timestamp, as a variable and use that in the query? I know I can do that in my Java program using variables, but wasn't sure if there was a way to do this with a function or stored procedure? INSERT INTO historical_data_avg (store_id, date, avg7sales, avg14sales, avg30sales, avg60sales, avg90sales, avg180sales) ( SELECT t1.store_id, t1.eod_ts, t1.avg5sales, t1.avg10sales, t1.avg20sales, t1.avg50sales, t1.avg100sales, t1.avg180sales FROM ( SELECT store_id, eod_ts, avg(eod_sales) OVER (PARTITION BY store_id ORDER BY eod_ts DESC ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) AS avg5sales, avg(eod_sales) OVER (PARTITION BY store_id ORDER BY eod_ts DESC ROWS BETWEEN CURRENT ROW AND 9 FOLLOWING) AS avg10sales, avg(eod_sales) OVER (PARTITION BY store_id ORDER BY eod_ts DESC ROWS BETWEEN CURRENT ROW AND 19 FOLLOWING) AS avg20sales, avg(eod_sales) OVER (PARTITION BY store_id ORDER BY eod_ts DESC ROWS BETWEEN CURRENT ROW AND 49 FOLLOWING) AS avg50sales, avg(eod_sales) OVER (PARTITION BY store_id ORDER BY eod_ts DESC ROWS BETWEEN CURRENT ROW AND 99 FOLLOWING) AS avg100sales, avg(eod_sales) OVER (PARTITION BY store_id ORDER BY eod_ts DESC ROWS BETWEEN CURRENT ROW AND 179 FOLLOWING) AS avg200sales FROM end_of_day_data WHERE eod_ts > CURRENT_TIMESTAMP - INTERVAL '260 days' GROUP BY store_id, eod_ts, eod_sales ORDER BY ticker_id, eod_ts ) as t1 WHERE t1.eod_ts = current_date ); -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql