我需要使用CTE使用已编译的数据,然后在下一个select语句中使用crosstab(将其转换为其他想法)将列转换为行。下面是查询。
with checked_adgroup AS (
SELECT
ua.new_adgroup,
ua.account,
ua.campaign,
ua.ad_group,
ua."position",
cp.category,
pt.full_value,
FROM unnest_adgroup ua
LEFT JOIN taxonomy_category cp ON ua."position" = cp."position"
LEFT JOIN taxonomy pt ON ua.short_val = pt.short_value AND cp.category = pt.category AND (pt.lob IS NULL OR pt.lob = ua.lob)
)
SELECT *
from crosstab(
'select
cad.account,
cad.campaign,
cad.ad_group,
cad.category,
cad.full_value
FROM checked_adgroup cad
WHERE cad.all_correct AND cad.category IS NOT NULL
ORDER BY 1,2,3')
AS final_result(
account text, campaign text, ad_group text,
division text, lob text, match_type text );
错误消息:错误:关系“ checked_adgroup”不存在第7行:FROM Checked_adgroup cad
checked_adgroup cte的输出如下所示:
最终声明的期望输出是:
欢迎社区。首先,请不要发布图片,因为它们无法使用,在某些情况下它们是被禁止的并且无法查看。而是使用格式化的文本。我没有太多使用交叉表功能,但是它确实提供了包含两个查询的第二个版本,第二个馈入了第一个查询。但是,您发布的查询中存在几个错误,需要以任何一种方式进行更正。首先。寻找-<
with checked_adgroup AS (
SELECT
ua.new_adgroup,
ua.account,
ua.campaign,
ua.ad_group,
ua."position",
cp.category,
pt.full_value,
--<< missing column or ending , above should not be there, assumption missing column see below.
FROM unnest_adgroup ua
LEFT JOIN taxonomy_category cp ON ua."position" = cp."position"
LEFT JOIN taxonomy pt ON ua.short_val = pt.short_value AND cp.category = pt.category AND (pt.lob IS NULL OR pt.lob = ua.lob)
)
SELECT *
from crosstab(
'select
cad.account,
cad.campaign,
cad.ad_group,
cad.category,
cad.full_value
FROM checked_adgroup cad
WHERE cad.all_correct AND cad.category IS NOT NULL
--<< above line has 2 errors:
--<< Incorrectly formatted needs to cad.all_correct is not null AND cad.category IS NOT NULL
--<< column cad.all_correct does not exist (see missing column above
ORDER BY 1,2,3')
AS final_result(
account text, campaign text, ad_group text,
division text, lob text, match_type text);
现在,我们需要将CTE转换为交叉表可能很适合使用的第二个查询。我已经用Postgres $ Quoting $标识了每个对象,并不是因为有必要,而是因为标准字符串引号(')就足够了,而更多的是从可见性角度出发。
select *
from crosstab(
$ct1$select
account,
campaign,
ad_group,
category,
full_value
--<< from checked_adgroup cad
--<< where cad.all_correct and cad.category is not null
--<< moved above lines to second query to avoid reference and removed qualification
order by 1,2,3
$ct1$
, $ct2$select *
from (
select
ua.new_adgroup,
ua.account,
ua.campaign,
ua.ad_group,
ua."position",
cp.category,
pt.full_value,
'mssing from orig posted query' all_correct
from unnest_adgroup ua
left join taxonomy_category cp on ua."position" = cp."position"
left join taxonomy pt on ua.short_val = pt.short_value
and cp.category = pt.category
and (pt.lob is null or pt.lob = ua.lob)
) s
where all_correct is not null and cad.category is not null
--<< move from query1
$ct2$ )
as final_result(
account text, campaign text, ad_group text,
division text, lob text, match_type text );
但是现在我得到一个错误关系unnest_adgroup不存在。这是正确的,因为您没有发布定义,也没有发布其他引用的表。但这似乎暗示语法是正确的。诚然,如果这样的话,这可能是不合时宜的,所以,我以后总是可以删除。但是,目前我没有其他项目,只能呆在家里,这似乎是一个有趣的问题。期待结果。祝好运。