如何在IBM Informix 10.0中使用Sub Query

时间:2017-08-16 17:04:44

标签: sql informix

RDBMS是INFORMIX 10.0

2008-03-09T03:30-04:00

返回9.50C1(相当于版本10.x)

Sub Query工作正常:

SELECT owner FROM systables WHERE TABNAME= ' VERSION';

我想将此查询用作子查询。

  

所以我的问题是:

     

如何在IBM INFORMIX v10.0上完成此操作?
  应该简单直接吗?

任何将该查询用作子查询的尝试都会生成这条有意义的消息:

“查询失败=> 2”

INFORMIX - 没有“WITH”子句,所以这不起作用:

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;

尝试按照此示例(IBM documentation):

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;

尝试按照此示例(specific to INFORMIX v10.0)

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;

2 个答案:

答案 0 :(得分:1)

我认为你只需要一个别名:

REM INSERTING into dual
SET DEFINE OFF;
Insert into "dual" (DUMMY) values ('X');

但是,根据对数据的大多数合理假设,您也可以将其称为:

dual

答案 1 :(得分:0)

因为Informix v10的早期版本不支持“Sub Query”/“Inline Query”/“Inner Query”,所以传统的做法是使用TEMP表来支持复杂的sql语句。因此,这是IBM Informix的这个版本v10的工作答案:

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;

临时表仅在会话期间存活 - 因此请确保您的连接在执行多个语句时保持打开状态。

此处的相关参考:

INTO TEMP clause

Using the WITH NO LOG option

Explicit inserts with SELECT...INTO TEMP statements

Duration of temporary tables