有人可以帮助我解决我的问题,我有2个表
查询表一:
SELECT (REPLACE(A .coano, '0', '')) coano_for_like FROM glfcoa_dump A WHERE posum = 'K' ORDER BY A .coano ASC;
表一的结果:
coano_for_like
----------------
1
11
12
13
131
132
2
21
211
212
22
221
2211
292
2921
2922
2923
2924
293
查询表二:
SELECT
coano,
SUM (CAST(endbal AS NUMERIC)) endbal
FROM
vw_fx_glfmas
WHERE
OFCID = '450'
AND TGL = '2017-08-11' and coano like '11%'
GROUP BY
coano
ORDER BY
SUBSTRING (coano ,- 2, 7) ASC
结果对于表二:
coano endbal
--------------
111000 0
112000 234342846
114000 0
表2中的 coano like '11%'
11是表一的结果
我的问题,如何从结果表一中按表2中的所有coano分组?
我想要这样的结果:
coano_for_like coano endbal
----------------------------------
1 111000 0
112000 234342846
114000 0
152101 813307251
152102 49259791
162110 -3456957
163010 0
169100 -36826000
169200 -32649000
11 111000 0
112000 234342846
114000 0
12 ... ...
答案 0 :(得分:0)
我认为您需要在第二个查询中更改GROUP BY和SELECT,以便通过您在查询1中使用的REPLACE函数进行分组:
SELECT
a.coano_for_like,
b.coano,
SUM (CAST(b.endbal AS NUMERIC)) AS endbal
FROM
(SELECT REPLACE(coano, '0', '') AS coano_for_like
FROM glfcoa_dump) AS a
INNER JOIN vw_fx_glfmas b ON a.coano_for_like = b.coano
WHERE b.ofcid = '450'
AND b.tgl = '2017-08-11'
AND b.posum = 'K'
GROUP BY a.coano_for_like,
b.coano
ORDER BY A.coano ASC;
这能为您提供所需的结果吗?
答案 1 :(得分:0)
您可以在联接中使用like。
SELECT coano_for_like, coano, SUM (CAST(endbal AS NUMERIC)) endbal
FROM vw_fx_glfmas
JOIN (SELECT (REPLACE(A .coano, '0', '')) coano_for_like FROM glfcoa_dump A WHERE posum = 'K') r
WHERE OFCID = '450' AND TGL = '2017-08-11' and coano like coano_for_like || '%'
GROUP BY coano
ORDER BY coano ASC
略微纠正
SELECT
r.coano_for_like,
coano,
SUM (CAST(endbal AS NUMERIC)) endbal
FROM
vw_fx_glfmas a,
(
SELECT
(REPLACE(A .coano, '0', '')) coano_for_like
FROM
glfcoa_dump A
WHERE
posum = 'K'
) r
WHERE
ofcid = '450'
AND TGL = '2017-08-11'
AND coano LIKE coano_for_like || '%'
GROUP BY
coano_for_like,
a.coano
ORDER BY
coano_for_like ASC