[英]How to give 'where' condition in select query while selecting the columns?
I want to give condition in a column selection while performing the select statement.我想在执行 select 语句时在列选择中给出条件。
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.我想执行 TOTAL_TIMEONSITE 的平均值,重命名它,并希望针对访问者对 20 年 6 月、20 年 7 月和 20 年 8 月存在的值进行平均。
Also the range of the whole query must be the month of Aug'20 only.此外,整个查询的范围只能是 20 年 8 月。 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.
因此,我想对 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')));
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.问题是它给了我“选择 TOTAL_TIMEONSITE 的子查询”作为结果列名,并且该列中的值都是相同的,但我希望这些值对于访问者来说是唯一的。
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'所以我将假设
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;
gives:给出:
TS ![]() |
DATE_A ![]() |
DATE_B ![]() |
---|---|---|
2020-08-31 23:59:00.000 ![]() |
2020-08-31 ![]() |
2020-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
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 ![]() |
TRUE![]() |
TRUE![]() |
TRUE![]() |
TRUE![]() |
Anyways, if I understand what you want I would write it like using CTE to make it more readable (to me):无论如何,如果我明白你想要什么,我会像使用 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
;
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
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.