Postgresql循环遍历onequery以创建另一个查询的结果

时间:2017-06-29 02:43:41

标签: sql postgresql pivot

我正在使用Postgresql 9.6并想知道这是否可能。

我有类似的查询:

with mkt_types as (
    select distinct(mkt_medium)
    from sessions
), sessions_all as (
    select *
    from sessions
)
select
        count(visitor_id) as total_visits, 
            count(1) FILTER (where mkt_medium = 'seo') as seo_visitors, 
            count(1) FILTER (where mkt_medium = 'seo' and domain_sessionidx = 1) as seo_new_visitors,
            count(1) FILTER (where mkt_medium = 'seo' and conversion_visit = 'true') as seo_converters,
            count(1) FILTER (where mkt_medium = 'seo' and domain_sessionidx = 1 and conversion_visit = 'true') as seo_new_converters
    from sessions_all;

是否可以从mkt_types中获取值并循环显示结果并替换' seo'部分查询。

实施例 如果我有mkt_types' seo',' email',' banner'那么我的最终查询最终会得到我输入的结果:

with mkt_types as (
    select distinct(mkt_medium)
    from sessions
), sessions_all as (
    select *
    from sessions
)
select
        count(visitor_id) as total_visits, 
            count(1) FILTER (where mkt_medium = 'seo') as seo_visitors, 
            count(1) FILTER (where mkt_medium = 'seo' and domain_sessionidx = 1) as seo_new_visitors,
            count(1) FILTER (where mkt_medium = 'seo' and conversion_visit = 'true') as seo_converters,
            count(1) FILTER (where mkt_medium = 'seo' and domain_sessionidx = 1 and conversion_visit = 'true') as seo_new_converters,
            count(1) FILTER (where mkt_medium = 'email') as email_visitors, 
            count(1) FILTER (where mkt_medium = 'email' and domain_sessionidx = 1) as email_new_visitors,
            count(1) FILTER (where mkt_medium = 'email' and conversion_visit = 'true') as email_converters,
            count(1) FILTER (where mkt_medium = 'email' and domain_sessionidx = 1 and conversion_visit = 'true') as email_new_converters,
            count(1) FILTER (where mkt_medium = 'banner') as banner_visitors, 
            count(1) FILTER (where mkt_medium = 'banner' and domain_sessionidx = 1) as banner_new_visitors,
            count(1) FILTER (where mkt_medium = 'banner' and conversion_visit = 'true') as banner_converters,
            count(1) FILTER (where mkt_medium = 'banner' and domain_sessionidx = 1 and conversion_visit = 'true') as banner_new_converters
    from sessions_all;

谢谢

1 个答案:

答案 0 :(得分:0)

在我看来,似乎我们不能这样做。当然你可以放入函数并构建查询。

如果您可以垂直而不是水平获取结果,我可以建议另一个查询:

(
 select 'all' as mkt_medium,
    count(1) as total_visits, 
    count(1) FILTER (where domain_sessionidx = 1) as new_visitors, 
    count(1) FILTER (where conversion_visit = 'true') as converters, 
    count(1) FILTER (where domain_sessionidx = 1 and conversion_visit = 'true') as new_converters
   FROM sessions_all
) UNION ALL (
 SELECT mkt_medium,
            count(1) as total_visits, 
    count(1) FILTER (where domain_sessionidx = 1) as new_visitors, 
    count(1) FILTER (where conversion_visit = 'true') as converters, 
    count(1) FILTER (where domain_sessionidx = 1 and conversion_visit = 'true') as new_converters
    from sessions_all
   GROUP BY mkt_medium
)