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 1ko34v-00047F-W5 for pgsql-general@arkaria.postgresql.org; Sat, 12 Dec 2020 11:28:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ko34O-0001KD-3L for pgsql-general@arkaria.postgresql.org; Sat, 12 Dec 2020 11:28:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ko34N-0001K5-QI for pgsql-general@lists.postgresql.org; Sat, 12 Dec 2020 11:28:11 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ko34H-0008ME-BB for pgsql-general@postgresql.org; Sat, 12 Dec 2020 11:28:11 +0000 Received: by mail-ej1-x632.google.com with SMTP id x16so15959910ejj.7 for ; Sat, 12 Dec 2020 03:28:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=3NhMNnVKACCK7SJtjTz25O6hMdv4ZdUIpb/3tcUCAwg=; b=DLnq7x64pU9lCkAQ+nbbi2g2D6Zmc6WJ9/k13W+KyZ3ZQUL1/EkJvHEm8VxAWkBxwc R/bdGf+AcaSIa9/K20VmGzRxqjklR+X6rIkLYgJe8WkvkW3St9BaPLxf44NZlLeEv+kF jA1NEKjf7bQLSiBU7/WMGQSVJrH2yXF1VgG57vKvTcBNflt/RAf+EGDvAMgzSDkH0PMn Tt+66mtwnN2GKKsA+uVZuhin10J9t6z6q9dkojN/yA8bquvQflr1NdLNgY4eR5/eMZGs RnVXaYlXixJWWFtaM7RyEUDZfjJvd9VPkMfRLQpQJe5jJFSyONzai1bbAVSevqQCh9fX OXdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=3NhMNnVKACCK7SJtjTz25O6hMdv4ZdUIpb/3tcUCAwg=; b=G1YFHM9gaCJ3Io8gJO42GJGNVWWhHR3agtq7wmah2azfZQvRvlUvk6gekQPDxVe84V Ss/9OkOGhijx4KxNrukm0gxvj+CCE/tZqn6A1YanRC7WB8cShP+/wyZ2cMYrGPc8MWdd XdtBsVgA6UDRtwSocttCGl87xAXuXP9DI01KsUJcwBRxjYoSGyYBNq32qdxjG/JfnJH9 C3gs8Ug/koeLkxcITra3HrNLoDo1Z7PUN+tkT8pVso89H2UUtiTYEeZ/jvXHRDxNGzJW RoHeVIu9Siw+KdO7JgOEvZVhExzA9hnuFkKcewRpCOn5SNdzFdc/TxpBuGOz7yOmalTc Kr0A== X-Gm-Message-State: AOAM531GOmRxpnaya3QctXhdv+hWI6j8Tg41DFbCIGBiCj/GVzyqNVRS mBedJuchL0L1/bKrgyvXS68= X-Google-Smtp-Source: ABdhPJzt5YlGpXWiEZx1kYKn9amY+jWLiPOJHU8fu79bIT2XuxW4959DD6R2mcIxv1D9uu1UsMpCpg== X-Received: by 2002:a17:906:5fc9:: with SMTP id k9mr14358015ejv.70.1607772482078; Sat, 12 Dec 2020 03:28:02 -0800 (PST) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id v9sm9181345ejk.48.2020.12.12.03.28.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 12 Dec 2020 03:28:01 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Subject: Re: SQL group by help From: Alban Hertroys In-Reply-To: Date: Sat, 12 Dec 2020 12:27:59 +0100 Cc: "pgsql-general@postgresql.org >> PG-General Mailing List" Content-Transfer-Encoding: quoted-printable Message-Id: <8965BA82-FE52-4A77-9612-DACEB4CC66A6@gmail.com> References: To: Chris Stephens X-Mailer: Apple Mail (2.3445.104.17) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 11 Dec 2020, at 18:24, Chris Stephens = wrote: >=20 > I'm trying to create a visual representation of a 6x8 grid of samples = on a rack using the following SQL format: >=20 > with rack_display as ( > select sr.ts rack_ts > , sr.rack_id > , r.rack_barcode > , 1 as row_pos > , max(case when rack_well =3D 0 then 'A1: '||sample_barcode = end) as col1 > , max(case when rack_well =3D 1 then 'A2: '||sample_barcode = end) as col2 > , max(case when rack_well =3D 2 then 'A3: '||sample_barcode = end) as col3 > , max(case when rack_well =3D 3 then 'A4: '||sample_barcode = end) as col4 > , max(case when rack_well =3D 4 then 'A5: '||sample_barcode = end) as col5 > , max(case when rack_well =3D 5 then 'A6: '||sample_barcode = end) as col6 > from rack r > , sample_rack sr > , sample s > where r.rack_id =3D sr.rack_id > and sr.sample_id =3D s.sample_id > and sr.rack_well < 6 > group by sr.ts, sr.rack_id, r.rack_barcode, row_pos > union all > select sr.ts rack_ts > , sr.rack_id > , r.rack_barcode > , 2 as row_pos > , max(case when rack_well =3D 6 then 'B1: '||sample_barcode = end) as col1 > , max(case when rack_well =3D 7 then 'B2: '||sample_barcode = end) as col2 > , max(case when rack_well =3D 8 then 'B3: '||sample_barcode = end) as col3 > , max(case when rack_well =3D 9 then 'B4: '||sample_barcode = end) as col4 > , max(case when rack_well =3D 10 then 'B5: '||sample_barcode = end) as col5 > , max(case when rack_well =3D 11 then 'B6: '||sample_barcode = end) as col6 > from rack r > , sample_rack sr > , sample s > where r.rack_id =3D sr.rack_id > and sr.sample_id =3D s.sample_id > and sr.rack_well >=3D 6 > and sr.rack_well < 12 > group by sr.ts, sr.rack_id, r.rack_barcode, row_pos > union all > ... > ) > select * from rack_display order by rack_ts, rack_id, row_pos; >=20 > the "union all"s continue for another 6 blocks. reports would filter = on rack_id and timestamp.=20 Is time really what groups these batches? I would double-check whether = you may be omitting to store some data relevant to this process. > if timestamps for each load of a rack were guaranteed to be the same, = this would work. however, the "sr.ts" values may vary by a few seconds = so there is potential for the "group by" to break. ts differences will = be a minimum of 5 minutes for each distinct load of a rack.=20 >=20 > what i think i need is to manufacture a group by column based off rows = in "sample_rack" that have "ts" values that are < 1 minute from each = other and rack_id is the same. i'm coming up blank on how to accomplish = that though. my first thought was to create an interval of +/- 1 min = then find all rows that overlap and assign a group number but i'm not = sure how to accomplish that.=20 You could date_trunc those timestamps to the minute and group on that. > there's also no guarantee an entire rack is full of samples so some = "cells" of display might be null. i think that makes the use of = tablefunc crosstab a little harder. if i remember correctly, it does not = handle missing values well. i'm open to any pivoting strategy. Many reporting tools have features to support just that. We use = WebFOCUS, which calls those ACROSS columns. It=E2=80=99s a common = requirement in reporting. Alban Hertroys -- There is always an exception to always.