How to query (download) data from Google Big Query?

You should use below approach. So, as you can see productPrice is accessed using unnested ‘prod’

SELECT date
,prod.v2ProductName
,prod.productVariant
,(prod.productPrice / 1000000) AS Price
,sum(prod.productQuantity) as qtde_total
FROM `table.id.ga_sessions_*`,
UNNEST(hits) AS hits,
UNNEST(hits.product) AS prod
where totals.transactions IS NOT NULL
and productVariant != "(not set)"
and _TABLE_SUFFIX BETWEEN '20180101' AND '20210401'
and prod.productPrice != 0
group by date,
prod.v2ProductName,
prod.productVariant,
prod.productPrice
LIMIT 1000

NOTE: when you using GROUP BY you need to use aggregation functions for those fields in your select statement which are not part of the GROUP BY.

SELECT date,
    fullVisitorId,
    visitNumber,
    visitStartTime,
    IF(
        trafficSource.isTrueDirect,
        "(direct)",
        trafficSource.source
    ) AS Source,
    IF(
        trafficSource.isTrueDirect,
        "(none)",
        trafficSource.medium
    ) AS Medium,
    IF(
        trafficSource.isTrueDirect,
        "(not set)",
        trafficSource.campaign
    ) AS Campaign,
    IF(
        trafficSource.isTrueDirect,
        "Direct",
        channelGrouping
    ) AS Channel_Group,
    IF(
        trafficSource.isTrueDirect,
        "(not set)",
        trafficSource.adContent
    ) AS adContent,
    totals.timeOnSite AS timeOnSite,
    totals.transactions AS Transactions,
    (totals.totalTransactionRevenue / 1000000) AS Revenue,
    FROM `table.id.ga_sessions_*`,
    UNNEST(hits) AS hit
WHERE (
        _TABLE_SUFFIX BETWEEN '20190121' AND '20190421'
    )
    AND totals.visits = 1
GROUP BY date,
    fullVisitorId,
    visitNumber,
    visitStartTime,
    Source,
    Medium,
    Campaign,
    Channel_Group,
    adContent,
    timeOnSite,
    Transactions,
    Revenue
ORDER BY fullVisitorId,
    date,
    visitNumber;

 

Leave a Reply