Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nywU3-0001yR-VW for pgsql-sql@arkaria.postgresql.org; Wed, 08 Jun 2022 14:16:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nywU2-0007YR-UO for pgsql-sql@arkaria.postgresql.org; Wed, 08 Jun 2022 14:16:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nywTg-0003Mu-Ok for pgsql-sql@lists.postgresql.org; Wed, 08 Jun 2022 14:16:09 +0000 Received: from mail-vs1-xe32.google.com ([2607:f8b0:4864:20::e32]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nywTa-0005Or-Dc for pgsql-sql@lists.postgresql.org; Wed, 08 Jun 2022 14:16:07 +0000 Received: by mail-vs1-xe32.google.com with SMTP id e11so5286605vsh.13 for ; Wed, 08 Jun 2022 07:16:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joeconway.com; s=google; h=message-id:date:mime-version:user-agent:content-language:to:cc :references:from:subject:in-reply-to:content-transfer-encoding; bh=dUbhaBlQSogYEB7rNTjxoB0cKltXRz7l5EDfXIVjlKk=; b=sHFM62XEWRGRNIEWzn38wEDVdjzyiZraohstbw8I3g70WYxGGX9NXP5eZjlPpVbok+ JhzjTsvflUzAfw2zqv+djz1/MuZ7c2effK8MymbnpMUsbX8nNES8bEB1qsaYoYlCxRdY dhs3jOA1PIUtJ5QT1aetn0WixaomDFiGgDy83D27XTcLhFZIvT7poVfvWSmfSXlYmlwH UZ0LgQ9UWFYE9Ekjl6OGvfp/PbcTC45RCNxn9/V53vPK4zBsreFUq1fZF+2agTEwNpW+ pzKoFyI5bcPxoLQmxb3hlBPAapAleIFyepskdg3qjkZgRDjCfBvDacvgGOMZzQKv9JNy dArg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:references:from:subject:in-reply-to :content-transfer-encoding; bh=dUbhaBlQSogYEB7rNTjxoB0cKltXRz7l5EDfXIVjlKk=; b=J0A+jpDrWWYfmDKSH//PYBQIR+NQC7GyZERKNh1ehtu5nW8BcwqXBb4E/n5u/gHF2k kzLpUWIsNO7Kc/CtFes73emntykwXFGXJWt4qsKridefK4s065jPP8BwzIcXRJ8OKfaV 4CFYbtW+yykS9/ro1qEY9RzbsXRZmma0/ghiFDRwmOiEZRLQSXAEXGdmAWGPHOfXtyCC /Mspu+vQJOlOlUHuJiLVFfcpLhTbElIbLJ2AvwH0IMYGvf7K8lPduVwP/Peii2tXUZc+ DXiay45T/qg2SWS+2O7kR/hqXJZHkMI2XaKFh/tHsa8kb6Ou6NE+FL6K4OIEc6NnDWbm xWgA== X-Gm-Message-State: AOAM530rg+0LVezRh6zBrrOPyUuya+aPciAOVvttep/7Fu4U0Rcdo5bP 7pvG0iKhp0to1oeP+jnynMfSTg== X-Google-Smtp-Source: ABdhPJy6/Dg1lrl3+pgzZ2BAk1hEHu+25fyNWatmp+YVcYaqhOgUBTMsBpJyZCrSY8bJvf8lo9yN/w== X-Received: by 2002:a67:b40b:0:b0:349:f01b:fe83 with SMTP id x11-20020a67b40b000000b00349f01bfe83mr15765825vsl.7.1654697760861; Wed, 08 Jun 2022 07:16:00 -0700 (PDT) Received: from [192.168.4.41] (072-017-018-098.res.spectrum.com. [72.17.18.98]) by smtp.gmail.com with ESMTPSA id k63-20020a1f5642000000b0034e6f1fd054sm2692028vkb.30.2022.06.08.07.16.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 08 Jun 2022 07:16:00 -0700 (PDT) Message-ID: <0dd4a9d2-1b88-cb9b-c84a-6124d658a82b@joeconway.com> Date: Wed, 8 Jun 2022 10:15:58 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Content-Language: en-US To: "David G. Johnston" , stercor@gmail.com Cc: pgsql-sql References: From: Joe Conway Subject: Re: Electricity bill 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 6/7/22 21:47, David G. Johnston wrote: > On Tue, Jun 7, 2022 at 6:39 PM Theodore M Rolle, Jr. > wrote: > > I'm putting in YYYY-MM-DD dates of electricity bills and would like > to not have starting and ending dates in the same row. Only ending date. > Will someone show me the SQL to compute the months’ usage? This > requires retrieving two rows to compute the number of days... > > You can use a window function called lead (or lag) to retrieve a value > from the next (previous) row and associate it with the current row. Another way that might work for you is interval math and ranges, e.g.: select enddt, month, year from bill; enddt | month | year -------------------------------+-------+------ 2021-12-31 23:59:59.999999-05 | dec | 2021 2022-01-31 23:59:59.999999-05 | jan | 2022 2022-02-28 23:59:59.999999-05 | feb | 2022 2022-03-31 23:59:59.999999-04 | mar | 2022 2022-04-30 23:59:59.999999-04 | apr | 2022 2022-05-31 23:59:59.999999-04 | may | 2022 2022-06-30 23:59:59.999999-04 | jun | 2022 2022-07-31 23:59:59.999999-04 | jul | 2022 2022-08-31 23:59:59.999999-04 | aug | 2022 2022-09-30 23:59:59.999999-04 | sep | 2022 2022-10-31 23:59:59.999999-04 | oct | 2022 2022-11-30 23:59:59.999999-05 | nov | 2022 2022-12-31 23:59:59.999999-05 | dec | 2022 (13 rows) WITH tsr (tr, month, year) AS ( SELECT tstzrange(b.enddt - '1 month'::interval, b.enddt, '(]') AS tr, b.month, b.year FROM bill b ) SELECT tr, month, year FROM tsr WHERE now() <@ tr; -[ RECORD 1 ]---------------------------------------------- tr | ["2022-05-30 00:00:00-04","2022-06-30 00:00:00-04") month | jun year | 2022 Adjust the open/closed bounds to suit. HTH, -- Joe Conway RDS Open Source Databases Amazon Web Services: https://aws.amazon.com