Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YTzxJ-0002xh-9F for pgsql-sql@arkaria.postgresql.org; Fri, 06 Mar 2015 21:38:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YTzxI-000267-Eu for pgsql-sql@arkaria.postgresql.org; Fri, 06 Mar 2015 21:38:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YTzxG-00023V-Uf for pgsql-sql@postgresql.org; Fri, 06 Mar 2015 21:38:47 +0000 Received: from mail-oi0-x230.google.com ([2607:f8b0:4003:c06::230]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YTzxD-0001vM-Lq for pgsql-sql@postgresql.org; Fri, 06 Mar 2015 21:38:45 +0000 Received: by oifz81 with SMTP id z81so19959940oif.2 for ; Fri, 06 Mar 2015 13:38:43 -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; bh=8u9QaYUxxJwFTmgzzaxkekmbJ34xWHwPS1y+A24APk8=; b=LGm23QtCcwJODg4bUk28WkopbqGybc+Hlb8pG4fAnWYzrGkl8d5nBu2CBga3iwSPDA T+SY1SpQH0jRCwnmZqQuq+JI5qSS19MH/qqzTrJxWRo9492Wo/qcYYJdLgI9wDQEvn/W yVotYpVB7WtZvD1MCFn33qXV9q9P++xKMRjfemzimfHWAZRi1gSBaCrRuuUteSAKjWxa KeODqR6VRLnw38GowMWjTafgS3wLruCmfWab5fgP0sVlD1oW/B0WxpiMuB0NWYuHFnOq BmVX9nwGznvmsfiq4fAJzS2W/GrmUp8VC5lqWiQXA8JXObYf1JlYSaHOtvu/BOQBeXiz taJQ== X-Received: by 10.202.216.68 with SMTP id p65mr11950683oig.4.1425677923080; Fri, 06 Mar 2015 13:38:43 -0800 (PST) Received: from [127.0.0.1] (mail.jonesborocwl.org. [64.233.145.118]) by mx.google.com with ESMTPSA id f133sm6879077oia.8.2015.03.06.13.38.42 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Mar 2015 13:38:42 -0800 (PST) Message-ID: <54FA1E61.9000201@gmail.com> Date: Fri, 06 Mar 2015 15:38:41 -0600 From: Jason Aleski User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Find inconsistencies in data with date range Content-Type: multipart/alternative; boundary="------------010503010901070701070607" 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 This is a multi-part message in MIME format. --------------010503010901070701070607 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit I know I can do this Java, but I'd rather have this running as a Stored Procedure. What I am wanting to do is identify and potentially correct the summary data for date inconsistencies. We have policies/red flag reports in place to keep this from happening, but we are now cleaning up history. The query below works on a per store basis, but I'd like to be able to run this for all stores in the location table. I've looked at some procedure codes regarding looping, but everything I try to create seems to give me problems. THe code I'm trying is also below. Does anyone have any suggestions on how to accomplish this? _Working Tables_ locations - table contains store information, startup date, address, etc daily_salessummary - table holds daily sales summary by store (summary should be updated nightly). eod_ts is End of Day Timestamp. _Query_ WITH datelist AS( SELECT t1.GenDate as gendate, extract(dow from GenDate) AS dayofweek FROM (SELECT date as GenDate FROM generate_series('1985-01-01'::date,CURRENT_TIMESTAMP::date,'1 day'::interval) date ) AS t1 ) SELECT gendate FROM datelist AS t1 WHERE gendate NOT IN (SELECT t1.eod_ts FROM daily_salessummary AS t1 JOIN locations AS t2 ON t1.location_id = t2.row_id WHERE t2.locationCode = 'US_FL_TAMPA_141') AND gendate > (SELECT start_date FROM locations WHERE locationCode = 'US_FL_TAMPA_141') _Desired Output_ - could output to an exceptions table StoreCode 'US_FL_TAMA_141' missing daily summary for 1998-01-01 StoreCode 'MX_OAXACA_SALINA_8344' missing daily summary for 2011-06-05 _ProcedureSQL_ (contains unknown errors) DECLARE CURSOR location_table IS SELECT locationCode FROM locations; BEGIN FOR thisSymbol IN ticker_tables LOOP EXECUTE IMMEDIATE 'WITH datelist AS( SELECT t1.GenDate as gendate, extract(dow from GenDate) AS dayofweek FROM (SELECT date as GenDate FROM generate_series('1985-01-01'::date,CURRENT_TIMESTAMP::date,'1 day'::interval) date ) AS t1 ) SELECT gendate FROM datelist AS t1 WHERE gendate NOT IN (SELECT t1.eod_ts FROM daily_salessummary AS t1 JOIN locations AS t2 ON t1.location_id = t2.row_id WHERE t2.locationCode = '' || location_table.locationCode || '') AND gendate > (SELECT start_date FROM locations WHERE locationCode = '' || location_table.locationCode || '')'; END LOOP; END; -- Jason Aleski / IT Specialist --------------010503010901070701070607 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit I know I can do this Java, but I'd rather have this running as a Stored Procedure.  What I am wanting to do is identify and potentially correct the summary data for date inconsistencies.  We have policies/red flag reports in place to keep this from happening, but we are now cleaning up history.  The query below works on a per store basis, but I'd like to be able to run this for all stores in the location table.

I've looked at some procedure codes regarding looping, but everything I try to create seems to give me problems.  THe code I'm trying is also below.  Does anyone have any suggestions on how to accomplish this?



Working Tables
  locations - table contains store information, startup date, address, etc
  daily_salessummary - table holds daily sales summary by store (summary should be updated nightly).  eod_ts is End of Day Timestamp.

Query
WITH datelist AS(
  SELECT t1.GenDate as gendate, extract(dow from GenDate) AS dayofweek
  FROM (SELECT date as GenDate
        FROM generate_series('1985-01-01'::date,CURRENT_TIMESTAMP::date,'1 day'::interval) date
       ) AS t1 
)
SELECT gendate FROM datelist AS t1
WHERE gendate NOT IN (SELECT t1.eod_ts FROM daily_salessummary AS t1
                      JOIN locations AS t2 ON t1.location_id = t2.row_id
                      WHERE t2.locationCode = 'US_FL_TAMPA_141')
                     
  AND gendate > (SELECT start_date FROM locations WHERE locationCode = 'US_FL_TAMPA_141')


Desired Output - could output to an exceptions table
StoreCode 'US_FL_TAMA_141' missing daily summary for 1998-01-01
StoreCode 'MX_OAXACA_SALINA_8344' missing daily summary for 2011-06-05


ProcedureSQL (contains unknown errors)
DECLARE
  CURSOR location_table IS
    SELECT locationCode FROM locations;
  BEGIN
    FOR thisSymbol IN ticker_tables LOOP
      EXECUTE IMMEDIATE 'WITH datelist AS(
                                              SELECT t1.GenDate as gendate, extract(dow from GenDate) AS dayofweek
                                                FROM (SELECT date as GenDate
                                                      FROM generate_series('1985-01-01'::date,CURRENT_TIMESTAMP::date,'1 day'::interval) date
                                                     ) AS t1 
                                              )
                                              SELECT gendate FROM datelist AS t1
                                              WHERE gendate NOT IN (SELECT t1.eod_ts FROM daily_salessummary AS t1
                                                                    JOIN locations AS t2 ON t1.location_id = t2.row_id
                                                                    WHERE t2.locationCode = '' || location_table.locationCode  || '')                   
                                             AND gendate > (SELECT start_date FROM locations WHERE locationCode = '' || location_table.locationCode  || '')';
    END LOOP;
  END;



-- 
Jason Aleski / IT Specialist
--------------010503010901070701070607--