簡體   English   中英

選擇列時如何在選擇查詢中給出“位置”條件?

[英]How to give 'where' condition in select query while selecting the columns?

我想在執行 select 語句時在列選擇中給出條件。

我想執行 TOTAL_TIMEONSITE 的平均值,重命名它,並希望針對訪問者對 20 年 6 月、20 年 7 月和 20 年 8 月存在的值進行平均。

此外,整個查詢的范圍只能是 20 年 8 月。 因此,我想對 TOTAL_TIMEONSITE 施加約束,以便它平均 20 年 6 月、20 年 7 月和 20 年 8 月對訪客的值。

select FULLVISITORID AS VISITOR_ID,
VISITID AS VISIT_ID,
VISITSTARTTIME_TS,
USER_ACCOUNT_TYPE,
(select AVG(TOTAL_TIMEONSITE) AS AVG_TOTAL_TIME_ON_SITE_LAST_3M FROM "ACRO_DEV"."GA"."GA_MAIN" WHERE
 (cast((visitstarttime_ts) as DATE) >= to_date('2020-06-01 00:00:00.000') and CAST((visitstarttime_ts) AS DATE) <= to_date('2020-08-31 23:59:00.000'))
 GROUP BY TOTAL_TIMEONSITE),
CHANNELGROUPING,
GEONETWORK_CONTINENT
from "ACRO_DEV"."GA"."GA_MAIN"
where (FULLVISITORID) in (select distinct (FULLVISITORID) from "ACRO_DEV"."GA"."GA_MAIN" where user_account_type in ('anonymous', 'registered') 
and (cast((visitstarttime_ts) as DATE) >= to_date('2020-08-01 00:00:00.000') and CAST((visitstarttime_ts) AS DATE) <= to_date('2020-08-31 23:59:00.000')));

問題是它給了我“選擇 TOTAL_TIMEONSITE 的子查詢”作為結果列名,並且該列中的值都是相同的,但我希望這些值對於訪問者來說是唯一的。

所以對於雪花:

所以我將假設visitstarttime_ts是一個timestamp ,因此cast((visitstarttime_ts) as DATE)與 `visitstarttime_ts::date' 相同

select to_timestamp('2020-08-31 23:59:00') as ts
    ,cast((ts) as DATE) as date_a
    ,ts::date as date_b;

給出:

TS DATE_A DATE_B
2020-08-31 23:59:00.000 2020-08-31 2020-08-31

因此日期范圍也可以更簡單

select to_timestamp('2020-08-31 13:59:00') as ts
    ,cast((ts) as DATE) as date_a
    ,ts::date as date_b
    ,date_a >= to_date('2020-08-01 00:00:00.000') and date_a <= to_date('2020-08-31 23:59:00.000') as comp_a
    ,date_b >= to_date('2020-08-01 00:00:00.000') and date_b <= to_date('2020-08-31 23:59:00.000') as comp_b
    ,date_b >= '2020-08-01'::date and date_a <= '2020-08-31 23:59:00.000'::date as comp_c
    ,date_b between '2020-08-01'::date and '2020-08-31 23:59:00.000'::date as comp_d
TS DATE_A DATE_B COMP_A COMP_B COMP_C COMP_D
2020-08-31 13:59:00.000 2020-08-31 2020-08-31 真的 真的 真的 真的

無論如何,如果我明白你想要什么,我會像使用 CTE 一樣編寫它以使其更具可讀性(對我而言):

with distinct_aug_ids as (
    SELECT DISTINCT 
        fullvisitorid 
    FROM acro_dev.ga.ga_main
    WHERE user_account_type IN ('anonymous', 'registered') 
        AND visitstarttime_ts::date BETWEEN '2020-08-01::date AND '2020-08-31'::date
), three_month_avg as (
    SELECT 
        fullvisitorid
        ,AVG(total_timeonsite) AS avg_total_time_on_site_last_3m
    FROM acro_dev.ga.ga_main
    WHERE visitstarttime_ts::DATE BETWEEN to_date('2020-06-01 00:00:00.000') AND to_date('2020-08-31 23:59:00.000')
    GROUP BY 1
)
select 
    m.fullvisitorid as visitor_id,
    m.visitid as visit_id,
    m.visitstarttime_ts,
    m.user_account_type,
    tma.avg_total_time_on_site_last_3m,
    m.channelgrouping,
    m.geonetwork_continent
FROM acro_dev.ga.ga_main as m
JOIN distinct_aug_ids AS dai
    ON m.fullvisitorid = dai.fullvisitorid
JOIN three_month_avg AS tma
    ON m.fullvisitorid = tma.fullvisitorid
;

但是,如果您希望它成為子選擇,它們是相同的:

select 
    m.fullvisitorid as visitor_id,
    m.visitid as visit_id,
    m.visitstarttime_ts,
    m.user_account_type,
    tma.avg_total_time_on_site_last_3m,
    m.channelgrouping,
    m.geonetwork_continent
FROM acro_dev.ga.ga_main as m
JOIN (
    SELECT DISTINCT 
        fullvisitorid 
    FROM acro_dev.ga.ga_main
    WHERE user_account_type IN ('anonymous', 'registered') 
        AND visitstarttime_ts::date BETWEEN '2020-08-01::date AND '2020-08-31'::date
) AS dai
    ON m.fullvisitorid = dai.fullvisitorid
JOIN (
    SELECT 
        fullvisitorid
        ,AVG(total_timeonsite) AS avg_total_time_on_site_last_3m
    FROM acro_dev.ga.ga_main
    WHERE visitstarttime_ts::DATE BETWEEN to_date('2020-06-01 00:00:00.000') AND to_date('2020-08-31 23:59:00.000')
    GROUP BY 1
)AS tma
    ON m.fullvisitorid = tma.fullvisitorid
;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM