pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Chris Stephens <cstephens16@gmail.com>
Cc: " <pgsql-general@postgresql.org>
Subject: Re: SQL group by help
Date: Sat, 12 Dec 2020 12:27:59 +0100
Message-ID: <8965BA82-FE52-4A77-9612-DACEB4CC66A6@gmail.com> (raw)
In-Reply-To: <CAEFL0syy1foM75DGUgfTAoouAaFO5pyPZcXrLharPYjW70DAOA@mail.gmail.com>
References: <CAEFL0syy1foM75DGUgfTAoouAaFO5pyPZcXrLharPYjW70DAOA@mail.gmail.com>


> On 11 Dec 2020, at 18:24, Chris Stephens <cstephens16@gmail.com> wrote:
> 
> I'm trying to create a visual representation of a 6x8 grid of samples on a rack using the following SQL format:
> 
> with rack_display as (
>     select sr.ts rack_ts
>         , sr.rack_id
>         , r.rack_barcode
>         , 1 as row_pos
>         , max(case when rack_well = 0 then 'A1: '||sample_barcode end) as col1
>         , max(case when rack_well = 1 then 'A2: '||sample_barcode end) as col2
>         , max(case when rack_well = 2 then 'A3: '||sample_barcode end) as col3
>         , max(case when rack_well = 3 then 'A4: '||sample_barcode end) as col4
>         , max(case when rack_well = 4 then 'A5: '||sample_barcode end) as col5
>         , max(case when rack_well = 5 then 'A6: '||sample_barcode end) as col6
>         from rack r
>                         , sample_rack sr
>             , sample s
>         where r.rack_id = sr.rack_id
>            and sr.sample_id = 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 = 6 then 'B1: '||sample_barcode end) as col1
>         , max(case when rack_well = 7 then 'B2: '||sample_barcode end) as col2
>         , max(case when rack_well = 8 then 'B3: '||sample_barcode end) as col3
>         , max(case when rack_well = 9 then 'B4: '||sample_barcode end) as col4
>         , max(case when rack_well = 10 then 'B5: '||sample_barcode end) as col5
>         , max(case when rack_well = 11 then 'B6: '||sample_barcode end) as col6
>         from rack r
>                         , sample_rack sr
>             , sample s
>         where r.rack_id = sr.rack_id
>            and sr.sample_id = s.sample_id
>            and sr.rack_well >= 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;
> 
> the "union all"s continue for another 6 blocks. reports would filter on rack_id and timestamp. 

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. 
> 
> 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. 

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’s a common requirement in reporting.


Alban Hertroys
--
There is always an exception to always.









view thread (3+ messages)

Message-ID: <8965BA82-FE52-4A77-9612-DACEB4CC66A6@gmail.com>
Permalink:  ../8965BA82-FE52-4A77-9612-DACEB4CC66A6@gmail.com/
Also on:    postgresql.org/message-id/8965BA82-FE52-4A77-9612-DACEB4CC66A6@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: haramrae@gmail.com, cstephens16@gmail.com
  Subject: Re: SQL group by help
  In-Reply-To: <8965BA82-FE52-4A77-9612-DACEB4CC66A6@gmail.com>
  References: <CAEFL0syy1foM75DGUgfTAoouAaFO5pyPZcXrLharPYjW70DAOA@mail.gmail.com> <8965BA82-FE52-4A77-9612-DACEB4CC66A6@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox