Reputation: 750
RDBMS is INFORMIX 10.0
SELECT owner FROM systables WHERE TABNAME= ' VERSION';
Returns 9.50C1 (which equates to version 10.x)
The Sub Query works just fine:
SELECT acct.fund_acct_nbr, acct.bin, prod.issuer_id, COUNT(*)
FROM fund_acct AS acct
JOIN products AS prod ON acct.cusip = prod.cusip
WHERE prod.issuer_id = 'xxxx'
AND SUBSTR(acct.bin, 1, 1) = 'x'
GROUP BY acct.fund_acct_nbr, acct.bin, prod.issuer_id;
I want to use this Query as a Sub Query.
So my question is:
How do I get this done on IBM INFORMIX v10.0?
Should be simple and straight forward right?
Any attempt to use that query as a Sub Query generates this meaningful message:
"Failed queries => 2"
INFORMIX - No "WITH" clause so this will not work:
WITH issuer_accts AS
(
SELECT acct.fund_acct_nbr, acct.bin, prod.issuer_id, COUNT(*)
FROM fund_acct AS acct
JOIN products AS prod ON acct.cusip = prod.cusip
WHERE prod.issuer_id = 'xxxx'
AND SUBSTR(acct.bin, 1, 1) = 'x'
GROUP BY acct.fund_acct_nbr, acct.bin, prod.issuer_id
)
SELECT issuer_accts.issuer_id, COUNT(*)
FROM issuer_accts
GROUP BY issuer_accts.issuer_id;
Attempt to follow this example (IBM documentation):
SELECT issuer_id, COUNT(*)
FROM
(
SELECT acct.fund_acct_nbr, acct.bin, prod.issuer_id, COUNT(*)
FROM fund_acct AS acct
JOIN products AS prod ON acct.cusip = prod.cusip
WHERE prod.issuer_id = 'xxxx'
AND SUBSTR(acct.bin, 1, 1) = 'x'
GROUP BY acct.fund_acct_nbr, acct.bin, prod.issuer_id
) issuer_accts
GROUP BY issuer_id;
Attempt to follow this example (specific to INFORMIX v10.0)
SELECT issuer_id, COUNT(*)
FROM
TABLE(MULTISET(
SELECT acct.fund_acct_nbr, acct.bin, prod.issuer_id, COUNT(*)
FROM fund_acct AS acct
JOIN products AS prod ON acct.cusip = prod.cusip
WHERE prod.issuer_id = 'xxxx'
AND SUBSTR(acct.bin, 1, 1) = 'x'
GROUP BY acct.fund_acct_nbr, acct.bin, prod.issuer_id
))
GROUP BY issuer_id;
Upvotes: 0
Views: 1193
Reputation: 750
Because this early version of Informix v10 does not support "Sub Query"/"Inline Query"/"Inner Query" then the conventional practice is to use TEMP tables to support complex sql statements. Therefore this is the working answer for this version v10 of IBM Informix:
SELECT acct.fund_acct_nbr, acct.bin, prod.issuer_id, COUNT(*)
FROM fund_acct AS acct
JOIN products AS prod ON acct.cusip = prod.cusip
WHERE prod.issuer_id = 'xxxx'
AND SUBSTR(acct.bin, 1, 1) = 'x'
GROUP BY acct.fund_acct_nbr, acct.bin, prod.issuer_id
INTO TEMP temp_issuer_accts WITH NO LOG;
SELECT issuer_id, COUNT(*) account_count
FROM temp_issuer_accts
GROUP BY issuer_id;
DROP TABLE temp_issuer_accts;
Temp tables survive only for the duration of the session - so make sure your connection remains open over the execution of multiple statements.
Relevant reference here:
Explicit inserts with SELECT...INTO TEMP statements
Upvotes: 0
Reputation: 1270391
I think you just need an alias:
SELECT issuer_id, COUNT(*)
FROM (SELECT acct.fund_acct_nbr, acct.bin, prod.issuer_id, COUNT(*) as cnt
FROM fund_acct AS acct JOIN
products AS prod
ON acct.cusip = prod.cusip
WHERE prod.issuer_id = 'xxxx' AND SUBSTR(acct.bin, 1, 1) = 'x'
GROUP BY acct.fund_acct_nbr, acct.bin, prod.issuer_id
) x
GROUP BY issuer_id;
But, under most reasonable assumptions about the data, you could also phrase this as:
SELECT issuer_id, COUNT(DISTINCT acct.bin || ' ' || prod.issuer_id)
FROM fund_acct AS acct JOIN
products AS prod
ON acct.cusip = prod.cusip
WHERE prod.issuer_id = 'xxxx' AND SUBSTR(acct.bin, 1, 1) = 'x'
GROUP BY issuer_id;
Upvotes: 1