我希望能够设置一个字段来回答“对于此记录中的值,该值是否满足另一个表中的某些条件?”我想尝试一下带有exists的case-when,但是Teradata (我的数据库管理系统)不喜欢它。有什么建议吗?
select foo,
(case when exists (select x.foo
from somedb x
where x.bar > 0)
then '1' else '0' end) as MyFlag
from mydb发布于 2010-07-29 04:15:58
我想不出一个简单易读的解决方案(当你像我一样愚蠢的时候是关键),所以我在一个临时表中做了一个联合:
create multiset table someDb.NiceFlags as
(
select t.foo,
'1' as myFlag
from someDb.pos_txn_mstr t
where exists(select x...)
union all
select t.foo,
'0' as myFlag
from someDb.pos_txn_mstr t
where not exists(select x...)
) with data primary index(foo)但现在我不觉得自己是个硬汉了:
发布于 2010-07-28 22:49:47
对此可能有不止一种解决方案。有时,这两个表之间存在关系。然后,我创建一个连接并在WHERE子句中处理它。我不知道Teradata,但在Oracle中我也可以这样做。
SELECT foo
FROM mydb
WHERE (select count(*) from somedb where x.bar > 0) > 0或者更像你的代码
select foo,
(case when (select count(*)
from somedb x
where x.bar > 0) > 0
then '1' else '0') as MyFlag
from mydb我知道只在WHERE子句中使用EXISTS:“我只想要下面的SELECT给我一些东西的行”。只有当一个表和另一个表之间存在某种连接时,这才有意义。
select id,foo from mydb y
where exists (select x.id from somedb x where x.id = y.id)发布于 2010-07-28 22:59:25
由于您只对1和0作为标志值感兴趣,因此请尝试以下操作:
select foo,
coalesce((select max(1)
from somedb x
where x.bar > 0), 0) as MyFlag
from mydbhttps://stackoverflow.com/questions/3353987
复制相似问题