我对这段代码感到麻烦:
SELECT
s.codcli,
s.consfin consistenza_iniziale,
s.periodo,
ss.consfin consistenza_finale,
ss.periodo,
(SELECT sum(quanti) somma_bonifici_ingresso
FROM mov WHERE tipope IN ('VE1', 'VE5', 'VE9') AND (datope BETWEEN to_date('01-01-2016', 'DD-MM-YYYY') AND to_date('30-07-2017', 'DD-MM-YYYY')) AND mov.codcli = s.codcli
GROUP BY codcli, tipope) as somma_bonifici_ingresso,
(SELECT sum(quanti) somma_bonifici_ingresso
FROM mov WHERE tipope IN ('PR1', 'PR5', 'PR9') AND (datope BETWEEN to_date('01-01-2016', 'DD-MM-YYYY') AND to_date('30-07-2017', 'DD-MM-YYYY')) AND mov.codcli = s.codcli
GROUP BY codcli, tipope) as somma_bonifici_uscita,
(ss.consfin - (somma_bonifici_ingresso - somma_bonifici_uscita) - s.consfin)/s.consfin as variazione
FROM sre s
LEFT JOIN sre ss on s.codcli = ss.codcli
WHERE s.periodo=to_date('01-01-2016', 'DD-MM-YYYY')
AND ss.periodo=to_date('30-06-2017', 'DD-MM-YYYY')
AND s.consfin>0
ORDER BY s.codcli
我收到的00904错误不是有效的标识符。我需要插入一个新列,安排列somma_bonifici_ingresso和somma_bonifici_uscita。我必须做一些计算但看起来它似乎无法找到那些我认为是别名的列...
答案 0 :(得分:0)
您无法在同一select
中重复使用列别名,因此请使用子查询:
SELECT s.*,
(consistenza_iniziale - (somma_bonifici_ingresso - somma_bonifici_uscita) - consistenza_iniziale) / consistenza_iniziale as variazione
FROM (SELECT s.codcli, s.consfin as consistenza_iniziale, s.periodo,
ss.consfin as consistenza_finale, ss.periodo,
(SELECT sum(quanti)
FROM mov
WHERE tipope IN ('VE1', 'VE5', 'VE9') AND
(datope BETWEEN DATE '2016-01-01' AND DATE '2017-07-30') AND
mov.codcli = s.codcli
) as somma_bonifici_ingresso,
(SELECT sum(quanti)
FROM mov
WHERE tipope IN ('PR1', 'PR5', 'PR9') AND
(datope BETWEEN '2016-01-01' AND DATE '2017-07-30') AND
mov.codcli = s.codcli
) as somma_bonifici_uscita,
FROM sre s LEFT JOIN
sre ss
ON s.codcli = ss.codcli
WHERE s.periodo = DATE '2016-01-01' AND
ss.periodo = DATE '2017-07-30' AND
s.consfin > 0
) s
ORDER BY codcli;
其他说明:
DATE
表示日期,TIMESTAMPS
表示有时间。WHERE
之间存在差异。GROUP BY
是不必要的 - 而且不好 - 。你最终会得到一个"返回太多行"错误。