SQL question

From: "Johnson, Shaunn" <SJohnson6(at)bcbsm(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: SQL question
Date: 2001-11-02 01:17:16
Message-ID: 73309C2FDD95D11192E60008C7B1D5BB03FFFF5C@snt452.corp.bcbsm.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

Browse pgsql-general by date

  From Date Subject
Next Message Dinesh Parikh 2001-11-02 06:25:48 GoTo statement problem
Previous Message Tom Innes 2001-11-02 00:28:18 Use of Serial Datatype and Sequence Issue