在 Netezza/PDA 中加入事务表并填充空值
Joining Transaction Tables and Populating Null Values in Netezza / PDA
我在 Netezza 中设置了两个事务表,如下所示。当加入 ID 和 transactionCount 上的表并尝试 return Answer 时,第 9998、9996、9995 行等将有空值。我如何 return 所有 transactionCount 值的 Answer 列填充有最后结果?
因此,例如 9998 会 return U,9996 以后会 return Y,9988 会 return N,依此类推。
ID transactionCount ID transactionCount Answer
1 9999 1 9999 U
1 9998 1
1 9997 1 9997 Y
1 9996 1
2 9999 2 9999 Y
2 9998 2
2 9997 2
2 9996 2 9996 N
2 9995 2
3 9999 3 9999 Y
3 9998 3
3 9997 3 9997 N
3 9996 3
3 9995 3
感谢您的帮助。
您没有指定 RDBMS,所以我将只使用 MySQL。
select transactionCount1,
(select answer
from t2
where transactionCount2 >= t1.transactionCount1
order by transactionCount2
limit 1) as answer
from t1
order by 1 desc
这是一个 fiddle 来展示它的工作原理:http://sqlfiddle.com/#!2/dde35e/3
编辑...
为了响应对问题的更改,查询变为:
select id1, transactionCount1,
(select answer
from t2
where id2 = id1 and transactionCount2 >= t1.transactionCount1
order by transactionCount2
limit 1) as answer
from t1
order by 1 asc, 2 desc
而 sql fiddle 现在是:http://sqlfiddle.com/#!2/36aacc/1
我在 Netezza 中设置了两个事务表,如下所示。当加入 ID 和 transactionCount 上的表并尝试 return Answer 时,第 9998、9996、9995 行等将有空值。我如何 return 所有 transactionCount 值的 Answer 列填充有最后结果?
因此,例如 9998 会 return U,9996 以后会 return Y,9988 会 return N,依此类推。
ID transactionCount ID transactionCount Answer
1 9999 1 9999 U
1 9998 1
1 9997 1 9997 Y
1 9996 1
2 9999 2 9999 Y
2 9998 2
2 9997 2
2 9996 2 9996 N
2 9995 2
3 9999 3 9999 Y
3 9998 3
3 9997 3 9997 N
3 9996 3
3 9995 3
感谢您的帮助。
您没有指定 RDBMS,所以我将只使用 MySQL。
select transactionCount1,
(select answer
from t2
where transactionCount2 >= t1.transactionCount1
order by transactionCount2
limit 1) as answer
from t1
order by 1 desc
这是一个 fiddle 来展示它的工作原理:http://sqlfiddle.com/#!2/dde35e/3
编辑...
为了响应对问题的更改,查询变为:
select id1, transactionCount1,
(select answer
from t2
where id2 = id1 and transactionCount2 >= t1.transactionCount1
order by transactionCount2
limit 1) as answer
from t1
order by 1 asc, 2 desc
而 sql fiddle 现在是:http://sqlfiddle.com/#!2/36aacc/1