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