Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sTk6Y-001xdG-3f for pgsql-general@arkaria.postgresql.org; Tue, 16 Jul 2024 15:28:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sTk6W-001nPr-Pg for pgsql-general@arkaria.postgresql.org; Tue, 16 Jul 2024 15:28:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sTk6W-001nPj-DT for pgsql-general@lists.postgresql.org; Tue, 16 Jul 2024 15:28:36 +0000 Received: from fhigh6-smtp.messagingengine.com ([103.168.172.157]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sTk6T-002QtP-Dk for pgsql-general@postgresql.org; Tue, 16 Jul 2024 15:28:35 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 5C7631148076; Tue, 16 Jul 2024 11:28:32 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 16 Jul 2024 11:28:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1721143712; x=1721230112; bh=OsSEApF2AuHkC4IdnLG5ZlxHv0R9Cia3HIJlymciFiA=; b= D4XYpNYY8V/Je90ytZcyA+uvughcNx4UWK9QUYCcQLFywrtBCU2RS2i2ZblHb1Jf r1KyJhB56S7PUVr2gA9w7BfW98s195W+veEvQ9oubCx37uHyvnAsvrikyJ1oBOll sB62HT1RW3fEmUNCAk4bb2tJy39Hbolm/BjVF0oC+/KdQ1ULEDfKda0Oe+ANuHE1 ubDIaEswqwcjbjCVMoisNf/rL3j7XwrUwo1DUl8ZEwRkY137b1XOmk+0oS9YRCKL 3ZD3643bXnchuZUHa/PeZZblR4HTix0SZnitPxY1kS2tKHed4lAyQ0qLYGgAF6Lz p9PrP72KPedpbnxSafqKPA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1721143712; x= 1721230112; bh=OsSEApF2AuHkC4IdnLG5ZlxHv0R9Cia3HIJlymciFiA=; b=e u9cHqsHpsoslQDZ38ZtlCG6RUJahJwPeaT04Dnou73N41zfjStwySjvw6asJhNdM GG7JrWViOvp0U1rxqSjfPJkbGLlyvThsPUt4DWzGgdbELOZS7xNOUiSoIb1DfPZV 3eDWdg1NCicd4sZvrEbX7QIXJhVv5DSgUgN4WkZhKsNvRFUVwe9hz36dKrQla1ji rMdzDvno60mTFxuPhsWakTa4ZksYqu3o5zx50NBojwdlp35dJRSize4EagPsceF+ JSOPJPKlwc60VinbGwzIBkCnfUkDukUQY37+7JgFagWhqw1pS0AM2J4WDZG4EgrT HwXlciAZiMq1w/WKK6N0A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrgeeggdekjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefkffggfgfuvfhfvefhjggtgfesthekredttddvjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeffveehgffgtdfgfedvtdejteetgedttdeujefhleei jeehiedtjeeggfegiedugfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 16 Jul 2024 11:28:30 -0400 (EDT) Message-ID: Date: Tue, 16 Jul 2024 08:28:30 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Code does Not Read in FY 2025 Data To: Anthony Apollis References: <1fd63ee5-a8a3-492e-a38d-82ff21da26b8@aklaver.com> <82ff4dac-d8b0-4b8b-aa88-e5d74f624a4d@aklaver.com> Content-Language: en-US Cc: pgsql-general From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/16/24 08:26, Anthony Apollis wrote: Again reply to list also Ccing list > I included a bigger data set. This code only pics up data up until 2024 > in new table which has data for 2025. Something with the Maxdate > calculation is wrong. > -- Step 1: Define the Fiscal Calendar > WITH FiscalCalendar AS ( >     SELECT 'FY25' AS FY, 'P1' AS Period, '2024-07-01'::date AS > PeriodStart, '2024-08-03'::date AS PeriodEnd >     UNION ALL >     SELECT 'FY25', 'P2', '2024-08-04'::date, '2024-08-31'::date >     UNION ALL >     SELECT 'FY25', 'P3', '2024-09-01'::date, '2024-09-28'::date >     UNION ALL >     SELECT 'FY25', 'P4', '2024-09-29'::date, '2024-11-02'::date >     UNION ALL >     SELECT 'FY25', 'P5', '2024-11-03'::date, '2024-11-30'::date >     UNION ALL >     SELECT 'FY25', 'P6', '2024-12-01'::date, '2024-12-31'::date >     UNION ALL >     SELECT 'FY25', 'P7', '2025-01-01'::date, '2025-02-01'::date >     UNION ALL >     SELECT 'FY25', 'P8', '2025-02-02'::date, '2025-03-01'::date >     UNION ALL >     SELECT 'FY25', 'P9', '2025-03-02'::date, '2025-03-29'::date >     UNION ALL >     SELECT 'FY25', 'P10', '2025-03-30'::date, '2025-05-03'::date >     UNION ALL >     SELECT 'FY25', 'P11', '2025-05-04'::date, '2025-05-31'::date >     UNION ALL >     SELECT 'FY25', 'P12', '2025-06-01'::date, '2025-06-30'::date > ), > > -- Step 2: Get Calendar Data with full week details > CalendarData AS ( >     SELECT DISTINCT >         c."Date", >         EXTRACT(WEEK FROM c."Date") AS "WeekNumber", >         f.PeriodEnd, >         COUNT(c."Date") OVER (PARTITION BY EXTRACT(WEEK FROM c."Date") > ORDER BY c."Date") AS "NumberOfDays", >         ROW_NUMBER() OVER (PARTITION BY EXTRACT(WEEK FROM c."Date") > ORDER BY c."Date") AS "DayNumberInWeek" >     FROM >         "Prod"."IMETA - Calendar Days Data_Table_Temp_Copy" c >     INNER JOIN >         FiscalCalendar f ON c."Date" BETWEEN f.PeriodStart AND > f.PeriodEnd AND c."FY" = f.FY >     WHERE >         c."FY" = 'FY25' > ), > > -- Step 3: Filter to get full weeks only > FullWeeks AS ( >     SELECT >         "Date", >         "WeekNumber", >         "PeriodEnd", >         "NumberOfDays", >         "DayNumberInWeek" >     FROM >         CalendarData >     WHERE >         "NumberOfDays" = 7 > ), > > -- Step 4: Get the maximum date from full weeks that is before today > MaxDate AS ( >     SELECT >         MAX("Date") AS "MaxDate" >     FROM >         FullWeeks >     WHERE >         "Date" < CURRENT_DATE > ) > > -- Step 5: Select the maximum date > SELECT >     "MaxDate" > FROM >     MaxDate; > > On Tue, 16 Jul 2024 at 17:13, Adrian Klaver > wrote: > > On 7/16/24 07:59, Anthony Apollis wrote: > > I am using Postgres and SQL Server. > > Does the query 'fail' on both platforms? > > > Can you test the data pls. > > Don't know what I am supposed to see? > > You will need to provide an explanation of what you want the query to > return vs what is currently being returned. Probably easiest to provide > a sample of data and what you expect the query to return from that > data. > Do this inline in your post. > > FYI, further investigation of the zipped data shows the 2025 data > has FY > values of NULL which would fail "FY" IS NOT NULL. > > > > > On Tue, 16 Jul 2024 at 16:45, Adrian Klaver > > > >> wrote: > > > >     On 7/16/24 05:53, Anthony Apollis wrote: > >      >  1. The problem is the code below reads only data up until > 2024, > >      >     although the table has been updated with latest data that > >     contains > >      >     2025, for some odd reason it is not pulling in or showing > >     data when > >      >     filtering for 2025 or even 2024 and later, which should > >     contain all > >      >     the latest data. > >      >  2.   SQL: "SET DATEFIRST 7;-- Holding Period Query > > > >     I don't recognize SET DATEFIRST 7 as Postgres command. > > > >     What database are you running this on? > > > >     Have not gone through the below fully, but this: > > > >     "FY" IS NOT NULL AND "FY" >= 'FY24' > > > >     and this > > > >     WEEK_NUMBER."Date" < CURRENT_DATE > > > >     would seem to be at odds if you want to reach into 2025. > > > >      > > >      >     SELECT > >      > > >      >  3. > >      > > >      >     -- Holding Period Query > >      >     SELECT > >      >          WEEK_NUMBER."Week Number", > >      >          WM_GH."Calendar day", > >      >          WM_GH."Month/Week", > >      >          WM_GH."Day name", > >      >          WM_GH."Company", > >      >          WM_GH."Material Code", > >      >          WM_GH."Metric", > >      >          WM_GH."Metric Value" > >      >     FROM > >      >          ( > >      >              SELECT > >      >                  "Calendar day", > >      >                  "Month/Week", > >      >                  "Day name", > >      >                  "Company", > >      >                  "Material Code", > >      >                  "Metric", > >      >                  "Metric Value" > >      >              FROM > >      > > >      > > > >  "Prod"."IMETA_Weekly_Metrics_in_Focus_Global_Stock_View_SAP_BW" AS > >     WM_GH > >      >              WHERE > >      >                  WM_GH."Metric Focus" LIKE 'Weekly' > >      >                  AND WM_GH."Calendar day" <= ( > >      >                      SELECT MAX(WEEK_NUMBER."Date") AS > "MAX DATE" > >      >                      FROM ( > >      >                          SELECT > >      >                              "Date", > >      >                              "Week number", > >      >                              COUNT("Date") OVER (PARTITION > BY "Week > >      >     number" ORDER BY "Week number") AS "Number of days", > >      >                              COUNT("Date") OVER (PARTITION > BY "Week > >      >     number" ORDER BY "Date") AS "Day number in weeks" > >      >                          FROM ( > >      >                              SELECT DISTINCT > >      >                                  "Date", > >      >                                  EXTRACT(WEEK FROM > >     WEEK_NUMBER."Date") > >      >     AS "Week number" > >      >                              FROM > >      > > >      >     "dbo"."IMETA_Calendar_Days_Data_Table_Copy10" AS > WEEK_NUMBER > >      >                              WHERE > >      >                                  "FY" IS NOT NULL AND "FY" > >= 'FY24' > >      >                          ) AS W_MAX > >      >                      ) AS WEEK_NUMBER > >      >                      WHERE > >      >                          WEEK_NUMBER."Date" < CURRENT_DATE > >      >                          AND "Number of days" = "Day > number in weeks" > >      >                  ) > >      >          ) AS WM_GH > >      >     LEFT OUTER JOIN > >      >          ( > >      >              SELECT > >      >                  * > >      >              FROM ( > >      >                  SELECT > >      >                      "Date", > >      >                      "Week number", > >      >                      COUNT("Date") OVER (PARTITION BY > "Week number" > >      >     ORDER BY "Week number") AS "Number of days", > >      >                      COUNT("Date") OVER (PARTITION BY > "Week number" > >      >     ORDER BY "Date") AS "Day number in weeks" > >      >                  FROM ( > >      >                      SELECT DISTINCT > >      >                          "Date", > >      >                          EXTRACT(WEEK FROM > WEEK_NUMBER."Date") AS > >     "Week > >      >     number" > >      >                      FROM > >      > > >     "dbo"."IMETA_Calendar_Days_Data_Table_Copy10" > >      >     AS WEEK_NUMBER > >      >                  ) AS W_MAX > >      >              ) AS WEEK_NUMBER > >      >              WHERE > >      >                  WEEK_NUMBER."Date" < CURRENT_DATE > >      >          ) AS WEEK_NUMBER > >      >     ON > >      >          WM_GH."Calendar day" = WEEK_NUMBER."Date" > >      >     ORDER BY > >      >          WM_GH."Calendar day" DESC; > >      > > > > >     -- > >     Adrian Klaver > > adrian.klaver@aklaver.com > > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com