SQL Hadoop Hive 与 Toad for DB2 - 多个不同的计数
SQL Hadoop Hive vs Toad for DB2 - Multiple Distinct Counts
我正在尝试为 Toad 构建查询,但是,以下内容不起作用。
select count (distinct t.column1, t.column2)
from schema.table
;
但是,上述查询在 Hadoop Hive 中运行良好。关于优化查询以使其适用于 Toad 的任何建议?
尝试将它们连接起来:
select count(distinct concat(t.column1, t.column2))
from schema.table t
您可以使用子查询:
select count(1)
from (select distinct t.column1, t.column2 from schema.table) as t1
;
模拟行为有点棘手。最安全的方法可能是:
select sum(case when seqnum = 1 and column1 is not null and column2 is not null then 1 else 0 end)
from (select t.*,
row_number() over (partition by column1, column2 order by column1) as seqnum
from t
) t
(order by
列无关紧要。许多数据库都需要一个,所以我经常包含它。)
此版本适用于任何数据库,而不仅仅是 DB2。
问题是,如果 任何 的值是 NULL
,Hive 不会计算一行,这考虑到了这一点。
在子查询中使用 select distinct
已接近尾声,但它计算 NULL
个值 -- 这种更改可能不适用于查询中的其他列。
将列串联在一起更接近。但是,当存在重叠值(例如“12”/“3”和“1”/“23”)时,您会遇到问题。
我正在尝试为 Toad 构建查询,但是,以下内容不起作用。
select count (distinct t.column1, t.column2)
from schema.table
;
但是,上述查询在 Hadoop Hive 中运行良好。关于优化查询以使其适用于 Toad 的任何建议?
尝试将它们连接起来:
select count(distinct concat(t.column1, t.column2))
from schema.table t
您可以使用子查询:
select count(1)
from (select distinct t.column1, t.column2 from schema.table) as t1
;
模拟行为有点棘手。最安全的方法可能是:
select sum(case when seqnum = 1 and column1 is not null and column2 is not null then 1 else 0 end)
from (select t.*,
row_number() over (partition by column1, column2 order by column1) as seqnum
from t
) t
(order by
列无关紧要。许多数据库都需要一个,所以我经常包含它。)
此版本适用于任何数据库,而不仅仅是 DB2。
问题是,如果 任何 的值是 NULL
,Hive 不会计算一行,这考虑到了这一点。
在子查询中使用 select distinct
已接近尾声,但它计算 NULL
个值 -- 这种更改可能不适用于查询中的其他列。
将列串联在一起更接近。但是,当存在重叠值(例如“12”/“3”和“1”/“23”)时,您会遇到问题。