pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SQL Dummy Needs Help
5+ messages / 4 participants
[nested] [flat]

* SQL Dummy Needs Help
@ 2001-03-09 19:05  Alder <Terrence.Branscombe@gems8.gov.bc.ca>
  0 siblings, 3 replies; 5+ messages in thread

From: Alder @ 2001-03-09 19:05 UTC (permalink / raw)
  To: pgsql-sql

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

Thanks,
Terry





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: SQL Dummy Needs Help
@ 2001-03-13 01:34  Andrew Perrin <aperrin@socrates.berkeley.edu>
  parent: Alder <Terrence.Branscombe@gems8.gov.bc.ca>
  2 siblings, 0 replies; 5+ messages in thread

From: Andrew Perrin @ 2001-03-13 01:34 UTC (permalink / raw)
  To: Alder <Terrence.Branscombe@gems8.gov.bc.ca>; +Cc: pgsql-sql

Well, you should probably get yourself a good SQL book :) but here's a try
(untested).  How about something like:

SELECT DISTINCT title_no, paidto_date
FROM            table1, table2
WHERE           table1.title_no = table2.title_no
AND             table1.effect_date <> table2.paidto_date;

Again, untested - try it and see.

----------------------------------------------------------------------
Andrew J Perrin - Ph.D. Candidate, UC Berkeley, Dept. of Sociology  
(Soon: Asst Professor of Sociology, U of North Carolina, Chapel Hill)
andrew_perrin@unc.edu - http://www.unc.edu/~aperrin

On Fri, 9 Mar 2001, Alder wrote:

> 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.
> 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.
> 
> Thanks,
> Terry
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
> 
> http://www.postgresql.org/users-lounge/docs/faq.html
> 




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: SQL Dummy Needs Help
@ 2001-03-13 02:14  Cliff Crawford <cjc26@cornell.edu>
  parent: Alder <Terrence.Branscombe@gems8.gov.bc.ca>
  2 siblings, 1 reply; 5+ messages in thread

From: Cliff Crawford @ 2001-03-13 02:14 UTC (permalink / raw)
  To: Alder <Terrence.Branscombe@gems8.gov.bc.ca>; +Cc: pgsql-sql

* 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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: SQL Dummy Needs Help
@ 2001-03-13 03:49  Cliff Crawford <cjc26@cornell.edu>
  parent: Cliff Crawford <cjc26@cornell.edu>
  0 siblings, 0 replies; 5+ messages in thread

From: Cliff Crawford @ 2001-03-13 03:49 UTC (permalink / raw)
  To: Alder <Terrence.Branscombe@gems8.gov.bc.ca>; +Cc: pgsql-sql

* Cliff Crawford <cjc26@cornell.edu> menulis:
> 
> 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));

Ooops, I keep confusing C and SQL operators...the "!=" above should be "<>".


-- 
Cliff Crawford

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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: SQL Dummy Needs Help
@ 2001-03-13 15:57  Ross J. Reedstrom <reedstrm@rice.edu>
  parent: Alder <Terrence.Branscombe@gems8.gov.bc.ca>
  2 siblings, 0 replies; 5+ messages in thread

From: Ross J. Reedstrom @ 2001-03-13 15:57 UTC (permalink / raw)
  To: Alder <Terrence.Branscombe@gems8.gov.bc.ca>; +Cc: pgsql-sql

On Fri, Mar 09, 2001 at 11:05:38AM -0800, Alder wrote:
> 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.
> 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.

I've seen a couple answers to this, but I think people aren't getting
the question right. I'm presuming that the desired return values are the
EFFECT_DATEs without matching PAIDTO_DATEs, rather than returning 
PAIDTO_DATEs, as stated, but that's just tweaking the return set.


It seems to me you're looking for something like:

select * from table1 t where not exists (select * from table2 where
t.title_no = title_no and t.effect_date = paidto_date)


Ross



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2001-03-13 15:57 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-03-09 19:05 SQL Dummy Needs Help Alder <Terrence.Branscombe@gems8.gov.bc.ca>
2001-03-13 01:34 ` Andrew Perrin <aperrin@socrates.berkeley.edu>
2001-03-13 02:14 ` Cliff Crawford <cjc26@cornell.edu>
2001-03-13 03:49   ` Cliff Crawford <cjc26@cornell.edu>
2001-03-13 15:57 ` Ross J. Reedstrom <reedstrm@rice.edu>

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