Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i3h00-00074K-P5 for pgsql-general@arkaria.postgresql.org; Fri, 30 Aug 2019 13:31:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i3gzy-0006Yu-P6 for pgsql-general@arkaria.postgresql.org; Fri, 30 Aug 2019 13:31:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i3gzy-0006Yi-FG for pgsql-general@lists.postgresql.org; Fri, 30 Aug 2019 13:31:30 +0000 Received: from mta1.recol.net ([64.207.103.6]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i3gzu-0008DC-PL for pgsql-general@lists.postgresql.org; Fri, 30 Aug 2019 13:31:29 +0000 Received: from wrkhors.com (unknown [75.37.52.9]) by mta1.recol.net (Postfix) with ESMTPSA id 909AA398A73; Fri, 30 Aug 2019 09:31:23 -0400 (EDT) Date: Fri, 30 Aug 2019 08:26:31 -0500 From: Steven Lembark To: pgsql-general@lists.postgresql.org Cc: lembark@wrkhors.com Subject: Re: Work hours? Message-ID: <20190830082631.6a039efc.lembark@wrkhors.com> In-Reply-To: <499b1af5-5be9-2639-2236-a7329ee5689d@aklaver.com> References: <20190827222741.GB19306@panix.com> <499b1af5-5be9-2639-2236-a7329ee5689d@aklaver.com> Organization: Workhorse Computing X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) X-Workhorse: Quite MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > > Any thoughts as to the best way to approach this? > > Use generate_series: > > https://www.postgresql.org/docs/11/functions-srf.html > > to generate all the days in the month. > > Loop over the days and use EXTRACT: > > https://www.postgresql.org/docs/11/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT > > to find the dates with a dow(The day of the week as Sunday (0) to > Saturday (6)) or isodow(The day of the week as Monday (1) to Sunday > (7)) that falls in Mon-Fri and add to counter. Assumes all weekdays are work days and that all weeks are uniform for work. Any holiday, emergency, outage leaves you with a skewed result. First workaround is table of non-work days: generate a temp table of the series not intersecting the non-workdays (materialized views are nice for this). Then you get into issues of different people having different non- work days, leaving your subtraction table keyed by person+date. Frequently the non-work days are by employee class, which allows a table of days off by employee grade + employees w/ grade => days off by empoloyee. Then individual employees will have their own time off due to paid vacation, medical or family leave, and sick days. Depending on your number of employees a non_working_days w/ date + employee works or you get into the pay grade + generic days and employee + pay grade for the generic days off merged with a separate table of individual days off. Subtract that from a temp table generated by the sequences and you'll have a complete schedule. -- Steven Lembark 3920 10th Ave South Workhorse Computing Birmingham, AL 35222 lembark@wrkhors.com +1 888 359 3508