如何给出其中'选择列时选择查询的条件?

发布于 2025-02-02 21:31:38 字数 1057 浏览 4 评论 0原文

我想在执行Select语句时在列选择中提供条件。

我想进行总计_timeonsite的平均值,重命名,并希望平均为Jun'20,Jul'20和Aug'20对访客的月份的平均值。

而且,整个查询的范围必须仅是8月20日的月份。因此,我想对total_timeonsite限制,以便将Jun'20,Jul'20和8月20日的几个月的值与访客相提并论。

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的选择子查询”为结果列名称,该列中的值都是相同的,但我希望这些值对访问者来说是唯一的。

I want to give condition in a column selection while performing the select statement.

I want to perform average of TOTAL_TIMEONSITE, RENAME IT, and want to average it for the values existing in the month of Jun'20, Jul'20 and Aug'20 against a visitor.

Also the range of the whole query must be the month of Aug'20 only. So I want to put the constraint on TOTAL_TIMEONSITE so that it averages the values for the months of Jun'20, Jul'20 and Aug'20 against a visitor.

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')));

The issue is that it is giving me the 'select subquery for TOTAL_TIMEONSITE' as the resultant column name and the values in that column are all same but I want the values to be unique for visitors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2025-02-09 21:31:38

因此,对于雪花:

所以我要假设访问starttime_tstimestamp因此
cast((((访问starttime_ts)作为日期)与`extstarttime_ts :: date'giss

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

tsdate_a date_b2020-08-31
23:59:00.0002020-08-31202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020

-ADS :: DATE'给予:因此,日期范围也可以更简单

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_adate_adate_bcomp_acomp_bcomp_ccomp_d
2020-08-31 13:59:00.0002020-08-312020-08-31true true true TrueTrue TrueTrue TrueEngry

无论如何,如果我知道您想要什么像使用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
;

So for Snowflake:

So I am going to assume visitstarttime_ts is a timestamp thus
cast((visitstarttime_ts) as DATE) is the same as `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;

gives:

TSDATE_ADATE_B
2020-08-31 23:59:00.0002020-08-312020-08-31

and thus the date range also can be simpler

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
TSDATE_ADATE_BCOMP_ACOMP_BCOMP_CCOMP_D
2020-08-31 13:59:00.0002020-08-312020-08-31TRUETRUETRUETRUE

Anyways, if I understand what you want I would write it like using CTE to make it more readable (to me):

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
;

But if you want that to be sub-selects, they are the same:

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
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文