From: Johnson, Shaunn <SJohnson6@bcbsm.com>
To: pgsql-general@postgresql.org
Subject: SQL question
Date: Thu, 1 Nov 2001 20:17:16 -0500
Message-ID: <73309C2FDD95D11192E60008C7B1D5BB03FFFF5C@snt452.corp.bcbsm.com> (raw)
Howdy:
Not sure if this is the correct group - let me now
if I should post elsewhere ...
Anyhow, I'm new with SQL. I wrote a script that I *thought*
should work. What I'm trying to do is query 5 tables from 5
different databases that have the same columns and collect all
of the information and append it into one file. This is the script:
[script]
SELECT
C_CONTRACT_NUM AS CONTRACT,
C_MBR_NUM AS MBR_NUM,
C_CLASS AS CLASS,
MAX(D_END_DT) AS DENDDT
FROM database1.table1_CN2PCLAS V
GROUP BY C_CLASS, C_CONTRACT_NUM, C_MBR_NUM
HAVING MAX(D_END_DT)=(
SELECT MAX(D_END_DT)
FROM database1.table1_CN2PCLAS W
WHERE V.C_CONTRACT_NUM = W.C_CONTRACT_NUM
AND V.C_MBR_NUM = W.C_MBR_NUM
AND D_END_DT >= '2000-01-01')
UNION
SELECT
C_CONTRACT_NUM AS CONTRACT,
C_MBR_NUM AS MBR_NUM,
C_CLASS AS CLASS,
MAX(D_END_DT) AS DENDDT
FROM database2.table1_CN2PCLAS V
GROUP BY C_CLASS, C_CONTRACT_NUM, C_MBR_NUM
HAVING MAX(D_END_DT)=(
SELECT MAX(D_END_DT)
FROM database2.table1_CN2PCLAS W
WHERE V.C_CONTRACT_NUM = W.C_CONTRACT_NUM
AND V.C_MBR_NUM = W.C_MBR_NUM
AND D_END_DT >= '2000-01-01')
UNION
SELECT
C_CONTRACT_NUM AS CONTRACT,
C_MBR_NUM AS MBR_NUM,
C_CLASS AS CLASS,
MAX(D_END_DT) AS DENDDT
FROM database3.table1_CN2PCLAS V
GROUP BY C_CLASS, C_CONTRACT_NUM, C_MBR_NUM
HAVING MAX(D_END_DT)=(
SELECT MAX(D_END_DT)
FROM database3.table1_CN2PCLAS W
WHERE D_END_DT >= '2000-01-01')
WHERE V.C_CONTRACT_NUM = W.C_CONTRACT_NUM
AND V.C_MBR_NUM = W.C_MBR_NUM
AND D_END_DT >= '2000-01-01')
UNION
SELECT
C_CONTRACT_NUM AS CONTRACT,
C_MBR_NUM AS MBR_NUM,
C_CLASS AS CLASS,
MAX(D_END_DT) AS DENDDT
FROM database4.table1_CN2PCLAS V
GROUP BY C_CLASS, C_CONTRACT_NUM, C_MBR_NUM
HAVING MAX(D_END_DT)=(
SELECT MAX(D_END_DT)
FROM database4.table1_CN2PCLAS W
WHERE V.C_CONTRACT_NUM = W.C_CONTRACT_NUM
AND V.C_MBR_NUM = W.C_MBR_NUM
AND D_END_DT >= '2000-01-01')
UNION
SELECT
C_CONTRACT_NUM AS CONTRACT,
C_MBR_NUM AS MBR_NUM,
C_CLASS AS CLASS,
MAX(D_END_DT) AS DENDDT
FROM database5.table1_CN2PCLAS V
GROUP BY C_CLASS, C_CONTRACT_NUM, C_MBR_NUM
HAVING MAX(D_END_DT)=(
SELECT MAX(D_END_DT)
FROM database5.table1_CN2PCLAS W
WHERE V.C_CONTRACT_NUM = W.C_CONTRACT_NUM
AND V.C_MBR_NUM = W.C_MBR_NUM
AND D_END_DT >= '2000-01-01');
[/script]
But the error I'm getting is:
[error]
The use of the reserved word "WHERE" following "" is not valid.
Expected tokens may include: "FOR WITH ORDER UNION EXCEPT QUERYNO
OPTIMIZE".
[/error]
I'm not clear on the meaning of that error. Can someone re-explain
what it's trying to say and why the query isn't working?
Thanks!
-X
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: SJohnson6@bcbsm.com
Subject: Re: SQL question
In-Reply-To: <73309C2FDD95D11192E60008C7B1D5BB03FFFF5C@snt452.corp.bcbsm.com>
References: <73309C2FDD95D11192E60008C7B1D5BB03FFFF5C@snt452.corp.bcbsm.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