Re: Solving my query needs with Rank and may be CrossTab - Mailing list pgsql-sql

From Rob Sargent
Subject Re: Solving my query needs with Rank and may be CrossTab
Date
Msg-id C4F63576-4927-4AB9-95C3-0D215E14DDA0@gmail.com
Whole thread Raw
In response to Re: Solving my query needs with Rank and may be CrossTab  (Iaam Onkara <iamonkara@gmail.com>)
List pgsql-sql


On Dec 2, 2019, at 9:55 AM, Iaam Onkara <iamonkara@gmail.com> wrote:

@Rob. What your referring to sounds like Materialized views, isn't it? An example query would be helpful in understand your recommendation/approach better.

On Mon, Dec 2, 2019 at 7:42 AM Rob Sargent <robjsargent@gmail.com> wrote:


Using the update-fixed-table style:
-- Get all possible people, null their values
create table report as
select distinct patient, null::float as bmi, null::float as sysbp, null::float as diabp, null::int as height
from source_table;
create index on report(patient);
-- get the height code (8302-2 using tilde operator because the import included leading blanks)
update report r set height = last_value 
from (select distinct patient, last_value(measurement) over
       (partition by patient, code
        order by sampletime)
from source_table
where code ~ '8302-2') as m where r.patient = m.patient
;
-- then similar for other codes. You may want to format the results, as in combining sys/dia bp readings after the update operations
-- the time drag of course is forever finding max(measurement time).  A composite index might help; indeed the unique key on the source is patient,code,timestamp I think.



pgsql-sql by date:

Previous
From: Iaam Onkara
Date:
Subject: Re: Solving my query needs with Rank and may be CrossTab
Next
From: "Mark Williams"
Date:
Subject: Whether to use "IN" clause