0
votes

The error is on line 4 and s3.fullVisitorId is underlined. I imagine it is going to throw an error on each of the selected items in that group though.

I tried adding a SELECT statement after DISTINCT and the inline error goes away but when I try to run the query I get the error: "Table name "s3" cannot be resolved: dataset name is missing."

Any help would be appreciated. I am finding translating queries from legacy sql to standard is a bit of a headache.

Here is the full query:

#standardSQL
SELECT
  CAST(CONCAT(SUBSTR(date,1,4),'-',SUBSTR(date,5,2),'-',SUBSTR(date,7,2)) AS DATE) AS Date,
  COUNT(DISTINCT s3.fullVisitorId) AS users,
  COUNT(s0.firstHit) AS carts,
  COUNT(s1.firstHit) AS order_details,
  COUNT(s2.firstHit) AS order_confirmation
FROM (
  SELECT
    IFNULL(s3.date,
      IFNULL(s0.date,
        IFNULL(s1.date,
          s2.date))) AS date,
    s3.fullVisitorId,
    s0.fullVisitorId,
    s0.visitId,
    s0.firstHit,
    s1.firstHit,
    s2.firstHit
  FROM (
      # user subquery
    SELECT
      date,
      fullVisitorId,
      visitId
    FROM
      `big-query-project-34643.162968675.ga_sessions_*`
    WHERE
      _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY))
      AND FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 0 DAY))
      AND totals.visits = 1
    GROUP BY
      date,
      fullVisitorId,
      visitId) s3
  FULL OUTER JOIN
    ((
      # first subquery
    SELECT
      date,
      fullVisitorId,
      visitId,
      MIN(h.hitNumber) AS firstHit
    FROM
      `big-query-project-34643.162968675.ga_sessions_*`, unnest(hits) as h
    WHERE
      _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY))
      AND FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 0 DAY))
      AND REGEXP_CONTAINS(h.page.pagePath,
        '/cart')
      AND totals.visits = 1
    GROUP BY
      date,
      fullVisitorId,
      visitId)) s0
  ON
    s3.fullVisitorId = s0.fullVisitorId
    AND s3.visitId = s0.visitId
  FULL OUTER JOIN
     ((
      # Second Subquery
    SELECT
      date,
      fullVisitorId,
      visitId,
      MIN(h.hitNumber) AS firstHit
    FROM
      `big-query-project-34643.162968675.ga_sessions_*`, unnest(hits) as h
    WHERE
      _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY))
      AND FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 0 DAY))
      AND REGEXP_CONTAINS(h.page.pagePath,
        '/order-details')
      AND totals.visits = 1
    GROUP BY
      date,
      fullVisitorId,
      visitId)) s1
  ON
    s0.fullVisitorId = s1.fullVisitorId
    AND s0.visitId = s1.visitId
  FULL OUTER JOIN
     ((
      # Third Subquery
    SELECT
      date,
      fullVisitorId,
      visitId,
      MIN(h.hitNumber) AS firstHit
    FROM
      `big-query-project-34643.162968675.ga_sessions_*`, unnest(hits) as h
    WHERE
      _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY))
      AND FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 0 DAY))
      AND REGEXP_CONTAINS(h.page.pagePath,
        '/orderconfirmation')
      AND totals.visits = 1
    GROUP BY
      date,
      fullVisitorId,
      visitId)) s2
  ON
    s1.fullVisitorId = s2.fullVisitorId
    AND s1.visitId = s2.visitId)
GROUP BY
  date
ORDER BY
  date
1

1 Answers

2
votes

You have multiple issues here - notably: in lines 14-19

s3.fullVisitorId,
s0.fullVisitorId,
s0.visitId,
s0.firstHit,
s1.firstHit,
s2.firstHit

I would recommend to at least provide aliases like [for example] below

s3.fullVisitorId s3_fullVisitorId,
s0.fullVisitorId s0_fullVisitorId,
s0.visitId,
s0.firstHit s0_firstHit,
s1.firstHit s1_firstHit,
s2.firstHit s2_firstHit    

and then in lines 4-7 - reference the alias

COUNT(DISTINCT s3_fullVisitorId) AS users,
COUNT(s0_firstHit) AS carts,
COUNT(s1_firstHit) AS order_details,
COUNT(s2_firstHit) AS order_confirmation