如何使用列表中的变量创建DB2 UNION查询

icomxhvb  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(134)

所以我有一个联合查询,如下所示:

select count(id)
from table 1
where membernumber = 'x'
and castnumber = 'y'
union
select count(id)
from table 1
where membernumber = 'x'
and castnumber = 'y'
union

等等。
这将有超过200个联合来自一个列表2x 200表,每行都有x和y的值。所以每个联合查询都必须从相应的行中获得x和y的值(没有任何特定的顺序)。我如何才能实现这一点?谢谢

nszi6y05

nszi6y051#

试试看:

DECLARE GLOBAL TEMPORARY TABLE 
SESSION.PARAMETERS
(
  MEMBERNUMBER INT
, CASTNUMBER INT
) DEFINITION ONLY WITH REPLACE
ON COMMIT PRESERVE ROWS NOT LOGGED;

-- Insert all the the constants in your application with
INSERT INTO SESSION.PARAMETERS 
(MEMBERNUMBER, CASTNUMBER)
VALUES (?, ?);

-- I don't know the meaning of the result you want to get 
-- but it's equivalent
select distinct count(t.id)
from table1 t
join session.parameters p
on p.membernumber = t.membernumber
and p.castnumber = t.castnumber
group by t.membernumber, t.castnumber;

相关问题