如何从与外部查询连接的 COUNT 子查询中设置列值?
How to I set a column value from a COUNT subquery that joins with an outer query?
我有一个查询(最初是为 SQL 服务器编写的),它有多个子查询作为计算计数的列。在 SQL 服务器中,我可以使用方程式将一列设置为计数子查询,例如:
Pass = (select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.RESULT = 'P' and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR)
我正在尝试在 Vertica 中使用查询 运行,但它不允许这种类型的 'equation'。所以我尝试做类似
的事情
(select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.RESULT = 'P' and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR) as Pass
但由于它与外部查询相关,我收到错误 Correlated subquery with aggregate function COUNT is not supported
这是我的查询:
select UserId = u.USER_ID,
Name = u.LNAME + ', ' + u.FNAME,
a.Month_Year,
(select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.RESULT = 'P' and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR) as Pass,
(select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.RESULT = 'F' and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR) as Fail,
(select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR) as Total
from report.sub_2018 a inner join pd_user_info u on a.ABST = u.USER_ID
where MONTH_YEAR like '2018-%' and u.USER_ID like 'MMN%'
group by u.LNAME, u.FNAME, a.MONTH_YEAR, a.ABST, u.USER_ID
order by u.LNAME, u.FNAME, a.MONTH_YEAR
我不太确定如何重新排列查询以使其与 table report.sub_2018 a
的外部查询一起使用
感谢任何帮助!
改写此查询
select
u.USER_ID as UserId,
u.LNAME || ', ' || u.FNAME as Name,
a.Month_Year,
count(case when a.RESULT = 'P' and a.STATUS_REASON = 'Pending' then 1 end) as Pass,
count(case when a.RESULT = 'F' and a.STATUS_REASON = 'Pending' then 1 end) as Fail,
count(case when a.STATUS_REASON = 'Pending' then 1 end) as Total
from report.sub_2018 a
inner join pd_user_info u on a.ABST = u.USER_ID
where MONTH_YEAR like '2018-%' and u.USER_ID like 'MMN%'
group by u.LNAME, u.FNAME, a.MONTH_YEAR, a.ABST, u.USER_ID
order by u.LNAME, u.FNAME, a.MONTH_YEAR
这样,您只需访问 sub_2018
table 一次。 I have blogged about this technique recently.
另请注意,在 SQL 服务器之外的大多数其他数据库中,使用此语法为列添加别名
u.USER_ID as UserId
...不是这个 SQL 服务器特定的一个:
UserId = u.USER_ID
我有一个查询(最初是为 SQL 服务器编写的),它有多个子查询作为计算计数的列。在 SQL 服务器中,我可以使用方程式将一列设置为计数子查询,例如:
Pass = (select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.RESULT = 'P' and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR)
我正在尝试在 Vertica 中使用查询 运行,但它不允许这种类型的 'equation'。所以我尝试做类似
的事情(select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.RESULT = 'P' and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR) as Pass
但由于它与外部查询相关,我收到错误 Correlated subquery with aggregate function COUNT is not supported
这是我的查询:
select UserId = u.USER_ID,
Name = u.LNAME + ', ' + u.FNAME,
a.Month_Year,
(select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.RESULT = 'P' and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR) as Pass,
(select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.RESULT = 'F' and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR) as Fail,
(select count(*) from report.sub_2018 p where p.ABST = a.ABST and p.STATUS_REASON = 'Pending' and p.MONTH_YEAR = a.MONTH_YEAR) as Total
from report.sub_2018 a inner join pd_user_info u on a.ABST = u.USER_ID
where MONTH_YEAR like '2018-%' and u.USER_ID like 'MMN%'
group by u.LNAME, u.FNAME, a.MONTH_YEAR, a.ABST, u.USER_ID
order by u.LNAME, u.FNAME, a.MONTH_YEAR
我不太确定如何重新排列查询以使其与 table report.sub_2018 a
感谢任何帮助!
改写此查询
select
u.USER_ID as UserId,
u.LNAME || ', ' || u.FNAME as Name,
a.Month_Year,
count(case when a.RESULT = 'P' and a.STATUS_REASON = 'Pending' then 1 end) as Pass,
count(case when a.RESULT = 'F' and a.STATUS_REASON = 'Pending' then 1 end) as Fail,
count(case when a.STATUS_REASON = 'Pending' then 1 end) as Total
from report.sub_2018 a
inner join pd_user_info u on a.ABST = u.USER_ID
where MONTH_YEAR like '2018-%' and u.USER_ID like 'MMN%'
group by u.LNAME, u.FNAME, a.MONTH_YEAR, a.ABST, u.USER_ID
order by u.LNAME, u.FNAME, a.MONTH_YEAR
这样,您只需访问 sub_2018
table 一次。 I have blogged about this technique recently.
另请注意,在 SQL 服务器之外的大多数其他数据库中,使用此语法为列添加别名
u.USER_ID as UserId
...不是这个 SQL 服务器特定的一个:
UserId = u.USER_ID