我有这样的查询:
driver.find_element(:link_text, "Login").click
另一个像这样:
SELECT bgdepartemen.c_kodedept AS c_kodedept,
bgdepartemen.vc_namadept AS vc_namadept,
bgdivisi.c_kodediv AS c_kodediv,
bgdivisi.vc_namadiv AS vc_namadiv,
bggroup0.c_kodegrp0 AS c_kodegrp0,
bggroup0.vc_namagrp0 AS vc_namagrp0,
(SELECT COALESCE(SUM(bgbudget0.n_nilai))
FROM (bgbudget0 JOIN bggroup1 ON bgbudget0.c_kodegrp1 = bggroup1.c_kodegrp1)
WHERE bgbudget0.n_tahun = 2016 AND
bgbudget0.n_bulan >= 2 AND
bgbudget0.n_bulan <= 3 AND
bgbudget0.c_kodediv = bgdivisi.c_kodediv AND
bggroup1.c_kodegrp0 = bggroup0.c_kodegrp0 AND
bggroup1.c_kodegrp1 LIKE '%') AS nilai
FROM bgdivisi JOIN bggroup0 ON 1 = 1
JOIN bgdepartemen on bgdivisi.c_kodedept = bgdepartemen.c_kodedept
WHERE bgdivisi.c_kodediv LIKE '%' AND
bgdepartemen.c_kodedept LIKE '%' AND
bggroup0.c_kodegrp0 LIKE '%'
我想从这些表中选择条件
SELECT bgdepartemen.c_kodedept AS c_kodedept,
bgdepartemen.vc_namadept AS vc_namadept,
bgdivisi.c_kodediv AS c_kodediv,
bgdivisi.vc_namadiv AS vc_namadiv,
bggroup0.c_kodegrp0 AS c_kodegrp0,
bggroup0.vc_namagrp0 AS vc_namagrp0,
(SELECT COALESCE(SUM(bgrealisasi0.n_nilai))
FROM (bgrealisasi0 JOIN bggroup1 ON bgrealisasi0.c_kodegrp1 = bggroup1.c_kodegrp1)
WHERE bgrealisasi0.n_tahun = 2016 AND
bgrealisasi0.n_bulan >= 2 AND
bgrealisasi0.n_bulan <= 3 AND
bgrealisasi0.c_kodediv = bgdivisi.c_kodediv AND
bggroup1.c_kodegrp0=bggroup0.c_kodegrp0 AND
bggroup1.c_kodegrp1 LIKE '%') AS nilai
FROM bgdivisi JOIN bggroup0 ON 1 = 1
JOIN bgdepartemen on bgdivisi.c_kodedept = bgdepartemen.c_kodedept
WHERE bgdivisi.c_kodediv LIKE '%' AND
bgdepartemen.c_kodedept LIKE '%' AND
bggroup0.c_kodegrp0 LIKE '%'
但我也想更改出现0的空记录,我已尝试 WHERE a.c_kodedept = b.c_kodedept AND
a.c_kodediv = b.c_kodediv AND
a.c_kodegrp0 = b.c_kodegrp0 AND
(a.nilai is not null or b.nilai is not null)
但它仍然没有给我正确的结果。
答案 0 :(得分:6)
通常,您首先会将NULL
值设置为0
,然后将SUM
设置为:
SELECT SUM(COALESCE(bgrealisasi0.n_nilai, 0)) ...
否则,您的查询会有一些缺陷,例如field LIKE '%'
,这是一种写作field IS NOT NULL
的效率非常低的方法。使用标量子查询会使整个查询真的难以阅读,如果重写为子查询,则可能会优化连接。
答案 1 :(得分:2)
你应该使用
SELECT COALESCE(SUM(bgbudget0.n_nilai), 0)
FROM .....
SELECT COALESCE(SUM(bgrealisasi0.n_nilai), 0)
FROM ....
答案 2 :(得分:0)
在 Postgres 从 9.4 版开始,SUM
忽略空值:
所有非空输入值的表达式总和
https://www.postgresql.org/docs/9.4/functions-aggregate.html
请注意,如果所有输入均为空,则聚合值将为空。
答案 3 :(得分:-1)
不是很明白你的问题是什么,但如果你想将null值更改为0可以参考下面的查询:
ISNULL(SUM(column_name), 0)