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.
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:
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):
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
;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.