2017-06-29 88 views
0

我使用的是Postgresql 9.6,並且想知道這是否可能。Postgresql通過onequery循環創建另一個查詢的結果

我有一個類似的查詢:

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和循環的價值觀和更換查詢的「搜索引擎優化」的一部分。

實例 如果我有mkt_types「搜索引擎優化」,「電子郵件」,「旗幟」,那麼我的最後的查詢最終會得到結果,如果我輸入了出來:

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; 

謝謝

+0

無關,但:'distinct'是**不**的功能。編寫'distinct(mkt_medium)'是沒有意義的(另外:'session_all'cte是一個壞主意 - 它會減慢你的查詢 - 只是把'sessions'表放在最後的查詢中) –

回答

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 
) 
+0

這很好,比我想要做的和垂直工作比我最初的嘗試要好得多。 –