pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Cliff Crawford <cjc26@cornell.edu>
To: Alder <Terrence.Branscombe@gems8.gov.bc.ca>
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL Dummy Needs Help
Date: Mon, 12 Mar 2001 21:14:21 -0500
Message-ID: <20010312211421.A17214@cornell.edu> (raw)
In-Reply-To: <3aa9287b@obsidian.gov.bc.ca>
References: <3aa9287b@obsidian.gov.bc.ca>

* Alder <Terrence.Branscombe@gems8.gov.bc.ca> menulis:
> I'm pretty new to SQL and can't figure out how to write what should be a
> simple query of two tables.  Could someone here possibly help me out?
> 
> Table 1 has two fields I'm interested in: TITLE_NO and EFFECT_DATE
> Table 2 also has two fields of interest:    TITLE_NO and PAIDTO_DATE
> 
> TITLE_NO is unique in Table 1, meaning each TITLE will have a unique
> EFFECT_DATE.  Table 2 represents an accounting history of each TITLE, so for
> each TITLE_NO there may be one or more PAIDTO_DATE values.  In both Tables
> the _DATE fields are stored as 9-character strings in the fomat YYYYMMDD.

It's probably better to store them as type date instead.  That way you
can use functions like date_part() to extract the month and day.

You could use string functions to extract, say, the last 4 characters to
get the MMDD value, but that can get messy if someone accidentally
stores a date in YYMMDD format.


> In all cases, the MM and DD values in Table 2 should be identical with those
> in Table 1.
> 
> My intention is to produce a list that will contain the TITLE_NO of each
> TITLE where the MMDD value for EFFECT_DATE differ from any of the
> PAIDTO_DATE values for that TITLE_NO.  The list must contain ONLY the
> PAIDTO_DATE values that differ, and the corresponding TITLE_NO.
> 
> Sorry I can't explain this a little more technically, but if anyone can
> advise me, that would be fabulous.

If you change your tables to store the dates as type date instead of
type string, then you can do something like the following:

SELECT table2.title_no, table2.paidto_date
    FROM table1, table2
    WHERE table1.title_no=table2.title_no
    AND (date_part('month', table1.effect_date) !=
            date_part('month', table2.paidto_date)
         OR date_part('day', table1.effect_date) !=
            date_part('day', table2.paidto_date));

(At least, I think that will work, but I never trust any SQL I write
without thoroughly testing it first..;)


-- 
Cliff Crawford

He who sacrifices his conscience to ambition
         burns a picture to obtain the ashes.  - Chinese proverb



view thread (5+ messages)  latest in thread

Message-ID: <20010312211421.A17214@cornell.edu>
Permalink:  ../20010312211421.A17214@cornell.edu/
Also on:    postgresql.org/message-id/20010312211421.A17214@cornell.edu

 · 

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-sql@postgresql.org
  Cc: cjc26@cornell.edu, Terrence.Branscombe@gems8.gov.bc.ca
  Subject: Re: SQL Dummy Needs Help
  In-Reply-To: <20010312211421.A17214@cornell.edu>
  References: <3aa9287b@obsidian.gov.bc.ca> <20010312211421.A17214@cornell.edu>

* 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