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 1gnXP6-0005xC-HA for pgsql-general@arkaria.postgresql.org; Sat, 26 Jan 2019 23:30:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gnXP3-0003KZ-Qj for pgsql-general@arkaria.postgresql.org; Sat, 26 Jan 2019 23:30:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gnXP3-0003Im-AY for pgsql-general@lists.postgresql.org; Sat, 26 Jan 2019 23:30:21 +0000 Received: from mail-ot1-x32f.google.com ([2607:f8b0:4864:20::32f]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gnXP0-0004Wp-R4 for pgsql-general@lists.postgresql.org; Sat, 26 Jan 2019 23:30:20 +0000 Received: by mail-ot1-x32f.google.com with SMTP id v23so11746959otk.9 for ; Sat, 26 Jan 2019 15:30:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=x/PF+EJqCLVYCTZL+obOIiV+Zxp1E6jAhNM/kZiZoao=; b=i+4qsV1x57qhIP7idhu79w7XynjZXq3Pov2LN/3aDi9zMZIAvDcIaIzZVoWBJaE0gs HJev+cBIXt5kcqr/QM4vKjc2OVut9pTqSIe6t+oKNSs59zLpF9l3CyaFmt5EKczn12pg PbtfjlhZeGijPHhdbBETZ1291gXRPBYBk+/awy/VCu7Nxikx8hcXeIcALJjnmlUrDSBf otT37NW3yA69UlpXRKP6+8jw+ba6U/OUnFV1P0fLwQ8z8kAykRudl2R+SmoZqT94s4kD EILB3e4AU+S2sbfq1NZ/suBD09MAyTWUTmOPpK9NKowetW60gkArlFoOs8ewJRPlX+t1 zqfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=x/PF+EJqCLVYCTZL+obOIiV+Zxp1E6jAhNM/kZiZoao=; b=F1BYKzr9sGCn563plXaGRDl2xntwQZHrn3Krer27OiQ4B/rjESDoqdzwQVtuOizq8/ ePJS7l3Q3a3vjn1IU78gvhRwNz9/5VsO7ow0GkP+O/LggzTeWbh1yoOhE5w1IeuJ3mpQ FD/6BKTnsbPzHUZE4WZ0RQ3gnvacn6b+fSHlRlLOO+gE6bBusSpBNCgJsKOkyZwhbG14 1yKp3sPhvtTVPUhvUIKZ0zqqpp1rIz1iLyE+QAPn96EM2ZA/yR4QE6xQC6nbFm4Lgqfk 3gd97E/nPyliyc09qc+hyTBhjeJ3m5+sQ68id+JoPIM2Eb0Ejs0DaGQC9LJo1F2Y+yR2 yJNQ== X-Gm-Message-State: AJcUukfhswzLOOM1CS495984CLu/3UBmq1kbvM8EGbynwLHnImLUV3cJ dxiNobVhtiVqmc1L7HVUq+dbeaC6 X-Google-Smtp-Source: ALg8bN4nOoG57T9zBMeuW1S7EWbEZaHdx3N0OHFIAIYBewfNBtH/0vJGle4cZwUuN9qx8GThbInwkQ== X-Received: by 2002:a05:6830:134d:: with SMTP id r13mr12463365otq.337.1548545417564; Sat, 26 Jan 2019 15:30:17 -0800 (PST) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id v1sm2730114otq.80.2019.01.26.15.30.16 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 26 Jan 2019 15:30:16 -0800 (PST) Subject: Re: Query help To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <4742d501-9cff-fd59-b752-c7409354d39b@gmail.com> Date: Sat, 26 Jan 2019 17:30:15 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------EDB5EC1FD0315DF65965AA78" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------EDB5EC1FD0315DF65965AA78 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 1/26/19 5:04 PM, Chuck Martin wrote: > I'm having trouble formulating a query. This is a simplified version of > the tables: > > ombcase > ------------ > case_pkey integer, primary key > casename varchar > insdatetime timestamp w/o time zone > status_fkey integer, foreign key > > status > -------- > status_pkey integer, primary key > statusid varchar > > statuschange > -------- > statuschange_pkey integer, primary key > insdatetime timestamp w/o time zone > ombcase_fkey integer, foreign key > oldstatus_fkey integer, foreign key > newstatus_fkey integer, foreign key > active integer, not nullable > > The idea should be obvious, but to explain, insdatetime is set when a new > record is created in any table. All records in ombcase have a foreign key > to status that can't be null. When status changes, a record is created in > statuschange recording the old and new status keys, and the time (etc). > > The goal is to find records in ombcase that have not had a status change > in xx days. If the status has not changed, there will be no statuschange > record. Does statuschange.*ins*datetime record when an ombcase record was first inserted, or when the status_fkey associated with ombcase.case_pkey was updated? And why not add upddatetime to ombcase?  That would solve all your problems. -- Angular momentum makes the world go 'round. --------------EDB5EC1FD0315DF65965AA78 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 1/26/19 5:04 PM, Chuck Martin wrote:
I'm having trouble formulating a query. This is a simplified version of the tables:

ombcase
------------
case_pkey integer, primary key
casename varchar
insdatetime timestamp w/o time zone
status_fkey integer, foreign key

status
--------
status_pkey integer, primary key
statusid varchar

statuschange
--------
statuschange_pkey integer, primary key
insdatetime timestamp w/o time zone
ombcase_fkey integer, foreign key
oldstatus_fkey integer, foreign key
newstatus_fkey integer, foreign key
active integer, not nullable

The idea should be obvious, but to explain, insdatetime is set when a new record is created in any table. All records in ombcase have a foreign key to status that can't be null. When status changes, a record is created in statuschange recording the old and new status keys, and the time (etc). 

The goal is to find records in ombcase that have not had a status change in xx days. If the status has not changed, there will be no statuschange record.

Does statuschange.insdatetime record when an ombcase record was first inserted, or when the status_fkey associated with ombcase.case_pkey was updated?

And why not add upddatetime to ombcase?  That would solve all your problems.

--
Angular momentum makes the world go 'round.
--------------EDB5EC1FD0315DF65965AA78--