数据库引擎是称为Paprika的财务软件。目的是在2018年12月为特定部门提取收入。
(2018年12月跨3个数据库(英国,美国,BR)的项目价值工作概率/ FX率)(基于小时数/跨数据库的总工作小时的部门工作时间)
零值来自最后一部分(最后3个分度)-每个项目的等级抽出小时数,其中许多时间为零。
以下是服务台的评论:
“对视图进行编码的人可以在计算中添加IF语句,以使Paprika在遇到被零除的情况时知道该怎么办,这将阻止视图崩溃。”
带有错误的代码:
(((COALESCE((SELECT SUM(JF_AMOUNT)
FROM MAV.UK.JOB_BUD_FORECAST, MAV.UK.NOMINAL_PERIOD
WHERE JF_JO_MN=JO_MN AND JF_FORECAST_TYPE=1 AND JF_NP_MN=NP_MN AND NP_PERIOD_KEY = '201812'),
(SELECT SUM(JF_AMOUNT)
FROM MAV.USA.JOB_BUD_FORECAST, MAV.USA.NOMINAL_PERIOD
WHERE JF_JO_MN=(JO_MN) AND JF_FORECAST_TYPE=1 AND JF_NP_MN=NP_MN AND NP_PERIOD_KEY = '201812'),
(SELECT SUM(JF_AMOUNT)
FROM MAV.BR.JOB_BUD_FORECAST, MAV.BR.NOMINAL_PERIOD
WHERE JF_JO_MN=(JO_MN) AND JF_FORECAST_TYPE=1 AND JF_NP_MN=NP_MN AND NP_PERIOD_KEY = '201812'),
0))*JO_PROBABILITY/100)/CUR_RATE)*
COALESCE(((SELECT SUM(JB_CHARGE)
FROM MAV.UK.JOB_BUDFORM
WHERE JB_JO_MN=JO_MN AND JB_GRADE IN('AUT', 'EDT', 'FMAN', 'FPRN', 'FDIR', 'SAUT', 'FDIR') AND JB_REV=JO_BUD_REVISION)/
(SELECT SUM(JB_CHARGE)
FROM MAV.UK.JOB_BUDFORM
WHERE JB_JO_MN=JO_MN AND JB_GRADE NOT IN(' ') AND JB_REV=JO_BUD_REVISION)),
((SELECT SUM(JB_CHARGE)
FROM MAV.USA.JOB_BUDFORM
WHERE JB_JO_MN=(JO_MN) AND JB_GRADE IN('AUT', 'EDT', 'FMAN', 'FPRN', 'FDIR', 'SAUT', 'FDIR') AND JB_REV=JO_BUD_REVISION)/
(SELECT SUM(JB_CHARGE)
FROM MAV.USA.JOB_BUDFORM
WHERE JB_JO_MN=(JO_MN) AND JB_GRADE NOT IN(' ') AND JB_REV=JO_BUD_REVISION)),
((SELECT SUM(JB_CHARGE)
FROM MAV.BR.JOB_BUDFORM
WHERE JB_JO_MN=(JO_MN) AND JB_GRADE IN('AUT', 'EDT', 'FMAN', 'FPRN', 'FDIR', 'SAUT', 'FDIR') AND JB_REV=JO_BUD_REVISION)/
(SELECT SUM(JB_CHARGE) FROM MAV.BR.JOB_BUDFORM WHERE JB_JO_MN=(JO_MN) AND JB_GRADE NOT IN(' ') AND JB_REV=JO_BUD_REVISION)),
0)
答案 0 :(得分:3)
我的天哪:在良好的SQL中,select中没有这样的选择,我们必须使用子查询和/或联接。
错误的SQL
(select A from toto where A=T.AA), T.*
from mytable T
正确的SQL(ANSI 92)
select toto.A, T.*
from toto
inner join T
on toto.A =T.AA
正确的SQL(旧SQL或由漫游器生成)
select toto.A, T.*
from toto,T
where toto.A =T.AA
要回答A / B的问题,如果B可以等于零而没有错误:
select
case
when coalesce(T.B,0)<> 0
then T.A/T.B
end
from my_table as T
或
select
case
when coalesce(T.B,0)<> 0
then T.A/T.B
else 0
end
from my_table as T
答案 1 :(得分:1)
与该查询相关的第一件事是对IT进行格式化,以便仅人类即可读取它,例如我把那个怪物带到an online T-SQL formatting tool上,添加了单词select
然后粘贴,然后点击格式按钮以显示以下内容:
SELECT
(((COALESCE( (
SELECT
SUM( JF_AMOUNT )
FROM MAV.UK.JOB_BUD_FORECAST
, MAV.UK.NOMINAL_PERIOD
WHERE JF_JO_MN = JO_MN
AND JF_FORECAST_TYPE = 1
AND JF_NP_MN = NP_MN
AND NP_PERIOD_KEY = '201812'
)
, (
SELECT
SUM( JF_AMOUNT )
FROM MAV.USA.JOB_BUD_FORECAST
, MAV.USA.NOMINAL_PERIOD
WHERE JF_JO_MN = (JO_MN)
AND JF_FORECAST_TYPE = 1
AND JF_NP_MN = NP_MN
AND NP_PERIOD_KEY = '201812'
)
, (
SELECT
SUM( JF_AMOUNT )
FROM MAV.BR.JOB_BUD_FORECAST
, MAV.BR.NOMINAL_PERIOD
WHERE JF_JO_MN = (JO_MN)
AND JF_FORECAST_TYPE = 1
AND JF_NP_MN = NP_MN
AND NP_PERIOD_KEY = '201812'
)
, 0 ))
* JO_PROBABILITY / 100)
/ CUR_RATE
) * COALESCE( ((
SELECT
SUM( JB_CHARGE )
FROM MAV.UK.JOB_BUDFORM
WHERE JB_JO_MN = JO_MN
AND JB_GRADE IN ('AUT', 'EDT', 'FMAN', 'FPRN', 'FDIR', 'SAUT', 'FDIR')
AND JB_REV = JO_BUD_REVISION
)
/ (
SELECT
SUM( JB_CHARGE )
FROM MAV.UK.JOB_BUDFORM
WHERE JB_JO_MN = JO_MN
AND JB_GRADE NOT IN (' ')
AND JB_REV = JO_BUD_REVISION
)
), ((
SELECT
SUM( JB_CHARGE )
FROM MAV.USA.JOB_BUDFORM
WHERE JB_JO_MN = (JO_MN)
AND JB_GRADE IN ('AUT', 'EDT', 'FMAN', 'FPRN', 'FDIR', 'SAUT', 'FDIR')
AND JB_REV = JO_BUD_REVISION
)
/ (
SELECT
SUM( JB_CHARGE )
FROM MAV.USA.JOB_BUDFORM
WHERE JB_JO_MN = (JO_MN)
AND JB_GRADE NOT IN (' ')
AND JB_REV = JO_BUD_REVISION
)
), ((
SELECT
SUM( JB_CHARGE )
FROM MAV.BR.JOB_BUDFORM
WHERE JB_JO_MN = (JO_MN)
AND JB_GRADE IN ('AUT', 'EDT', 'FMAN', 'FPRN', 'FDIR', 'SAUT', 'FDIR')
AND JB_REV = JO_BUD_REVISION
)
/ (
SELECT
SUM( JB_CHARGE )
FROM MAV.BR.JOB_BUDFORM
WHERE JB_JO_MN = (JO_MN)
AND JB_GRADE NOT IN (' ')
AND JB_REV = JO_BUD_REVISION
)
), 0 )
我认为大多数数据可以来自2个连接的子查询,如下所示:
SELECT
, jo.JO_MN , jo.JO_BUD_REVISION
, jo.JO_PROBABILITY , jo.CUR_RATE
, s1.SUM_JF_AMOUNT , s2.SUM_JB_CHARGE_IN , s2.SUM_JB_CHARGE_NOT_IN
FROM unknown_table jo
LEFT JOIN (
SELECT
JF_JO_MN
, SUM( JF_AMOUNT ) AS SUM_JF_AMOUNT
FROM MAV.UK.JOB_BUD_FORECAST
INNER JOIN MAV.UK.NOMINAL_PERIOD ON JF_NP_MN = NP_MN
WHERE JF_FORECAST_TYPE = 1
AND NP_PERIOD_KEY = '201812'
GROUP BY
JF_JO_MN
) s1 ON JF_JO_MN = JO_MN
LEFT JOIN (
SELECT
JB_REV
, SUM( CASE
WHEN JB_GRADE IN ('AUT', 'EDT', 'FMAN', 'FPRN', 'FDIR', 'SAUT', 'FDIR')
THEN JB_CHARGE
END ) AS SUM_JB_CHARGE_IN
, SUM( CASE
WHEN JB_GRADE NOT IN (' ')
THEN JB_CHARGE
END ) AS SUM_JB_CHARGE_NOT_IN
FROM MAV.UK.JOB_BUDFORM
WHERE (
JB_GRADE IN ('AUT', 'EDT', 'FMAN', 'FPRN', 'FDIR', 'SAUT', 'FDIR')
OR JB_GRADE NOT IN (' ')
)
GROUP BY
JB_REV
) s2 ON JB_REV = JO_BUD_REVISION
如果使用这样的查询结构,则可以更轻松地访问所需的数字,从而也可以轻松解决NULL问题。
注意,您需要改进每个子查询的联接语法。帮自己一个忙,不要在表名之间使用逗号,这将帮助您采用“较新的”语法(已在25年前发布)。