我想计算此子查询中的行数:
select t.*
FROM(
select 1 as first, 2 as second
union
select 1 as first, 3 as second
) t
所以预期的结果是:
+-------+--------+-------+
| first | second | count |
+-------+--------+-------+
| 1 | 2 | 2 |
| 1 | 3 | 2 |
+-------+--------+-------+
我已经尝试过将count(*)
包含在查询中,但它不起作用。对这个问题有任何想法吗?
答案 0 :(得分:4)
您可以使用窗口功能。
窗口功能非常宝贵,值得您花些时间来熟悉它们。
示例强>
select t.*
,count = sum(1) over ()
FROM(
select 1 as first, 2 as second
union
select 1 as first, 3 as second
) t
<强>返回强>
first second count
1 2 2
1 3 2
答案 1 :(得分:2)
另一种选择是使用CTE。
with cte as
(
select 1 as first, 2 as second
union
select 1 as first, 3 as second
)
select
cte.*,
(select count(*) from cte) as [count]
from cte
答案 2 :(得分:1)
你可以尝试
presto> select (map(array[], array[]) = map()) as is_empty;
is_empty
----------
true
(1 row)
或
select t.*, count(*) over (order by first ) count
FROM(
select 1 as first, 1 as second
union
select 1 as first, 2 as second
union
select 1 as first, 3 as second
union
select 2 as first, 4 as second
) t