2014-12-05 77 views
1

我使用下面的查詢BigQuery中的連接表,並獲得visitnumber和源誰訂購了產品的人:給BigQuery的子查詢的別名,以縮短查詢

QUERY:

SELECT visitor, transactionId as OrderNumber, source, referralPath, medium, keyword, campaign, visitNumber, visitStartTime, conversionTime, visitLastClikcTime 
FROM (
SELECT conversions.fullVisitorId as visitor, conversions.transactionId as transactionId, 
clickChanges.visitNumber as visitNumber, conversions.visitNumber as transactionVisitNumber, 
clickChanges.source as source, clickChanges.referralPath as referralPath, 
clickChanges.medium as medium, clickChanges.keyword as keyword, clickChanges.campaign as campaign, 
conversionTime, min(clikcTime) as visitStartTime, max(clikcTime) as visitLastClikcTime 
FROM (SELECT fullVisitorId, hits.transaction.transactionId as transactionId, visitNumber as visitNumber, 
CONCAT(CONCAT(SUBSTR(date, 7, 2), '.', SUBSTR(date, 5, 2), '.', SUBSTR(date, 0, 4)), ' ', CONCAT(STRING(hits.hour), ':', STRING(hits.minute))) as conversionTime 
FROM 
(TABLE_DATE_RANGE([54247008.ga_sessions_], 
       TIMESTAMP('2014-11-16'), 
       TIMESTAMP('2014-11-21'))) 
WHERE hits.transaction.transactionId is not null 
ORDER BY fullVisitorId, visitNumber 
) conversions 
JOIN EACH(
SELECT fullVisitorId, trafficSource.referralPath as referralPath, trafficSource.medium as medium, trafficSource.source as source, visitNumber,  
CONCAT(CONCAT(SUBSTR(date, 7, 2), '.', SUBSTR(date, 5, 2), '.', SUBSTR(date, 0, 4)), ' ', CONCAT(STRING(hits.hour), ':', STRING(hits.minute))) as clikcTime, 
trafficSource.campaign as campaign, trafficSource.keyword as keyword 
FROM (TABLE_DATE_RANGE([54247008.ga_sessions_], 
       TIMESTAMP('2014-11-16'), 
       TIMESTAMP('2014-11-21'))) 
GROUP BY fullVisitorId, referralPath, medium, source, visitNumber, clikcTime, campaign, keyword 
ORDER BY fullVisitorId, visitNumber 
) clickChanges 
ON 
conversions.fullVisitorId = clickChanges.fullVisitorId 
GROUP BY visitor, transactionId, visitNumber, transactionVisitNumber, source, referralPath, medium, keyword, campaign, conversionTime 
HAVING visitNumber <= transactionVisitNumber 
ORDER BY transactionId, visitNumber 
) 

現在我想獲得每個轉換ID的MAX(訪問號碼)的聚合列,並向我顯示與MAX相關的信息源(訪問號碼)。要做到這一點,我在這裏研究並建立了整個查詢,你可以看到下面,這是工作的另一個JOIN,但它也是非常長的和重複的:

相同的查詢(略有修改):

SELECT a.visitor, a.transactionId as OrderNumber, a.source, a.referralPath, a.medium, a.keyword, a.campaign, a.visitNumber as max_visitnumber, a.visitStartTime, a.conversionTime, a.visitLastClikcTime 
FROM (
SELECT conversions.fullVisitorId as visitor, conversions.transactionId as transactionId, 
clickChanges.visitNumber as visitNumber, conversions.visitNumber as transactionVisitNumber, 
clickChanges.source as source, clickChanges.referralPath as referralPath, 
clickChanges.medium as medium, clickChanges.keyword as keyword, clickChanges.campaign as campaign, 
conversionTime, min(clikcTime) as visitStartTime, max(clikcTime) as visitLastClikcTime 
FROM (SELECT fullVisitorId, hits.transaction.transactionId as transactionId, visitNumber as visitNumber, 
CONCAT(CONCAT(SUBSTR(date, 7, 2), '.', SUBSTR(date, 5, 2), '.', SUBSTR(date, 0, 4)), ' ', CONCAT(STRING(hits.hour), ':', STRING(hits.minute))) as conversionTime 
FROM 
(TABLE_DATE_RANGE([54247008.ga_sessions_], 
       TIMESTAMP('2014-11-16'), 
       TIMESTAMP('2014-11-21'))) 
WHERE hits.transaction.transactionId is not null 
ORDER BY fullVisitorId, visitNumber 
) conversions 
JOIN EACH(
SELECT fullVisitorId, trafficSource.referralPath as referralPath, trafficSource.medium as medium, trafficSource.source as source, visitNumber,  
CONCAT(CONCAT(SUBSTR(date, 7, 2), '.', SUBSTR(date, 5, 2), '.', SUBSTR(date, 0, 4)), ' ', CONCAT(STRING(hits.hour), ':', STRING(hits.minute))) as clikcTime, 
trafficSource.campaign as campaign, trafficSource.keyword as keyword 
FROM (TABLE_DATE_RANGE([54247008.ga_sessions_], 
       TIMESTAMP('2014-11-16'), 
       TIMESTAMP('2014-11-21'))) 
GROUP BY fullVisitorId, referralPath, medium, source, visitNumber, clikcTime, campaign, keyword 
ORDER BY fullVisitorId, visitNumber 
) clickChanges 
ON 
conversions.fullVisitorId = clickChanges.fullVisitorId 
GROUP BY visitor, transactionId, visitNumber, transactionVisitNumber, source, referralPath, medium, keyword, campaign, conversionTime 
HAVING visitNumber <= transactionVisitNumber 
ORDER BY transactionId, visitNumber 
) a 
JOIN EACH 
(
SELECT MAX(visitNumber) as max_visitnumber, transactionId 
FROM (
SELECT conversions.fullVisitorId as visitor, conversions.transactionId as transactionId, 
clickChanges.visitNumber as visitNumber, conversions.visitNumber as transactionVisitNumber, 
clickChanges.source as source, clickChanges.referralPath as referralPath, 
clickChanges.medium as medium, clickChanges.keyword as keyword, clickChanges.campaign as campaign, 
conversionTime, min(clikcTime) as visitStartTime, max(clikcTime) as visitLastClikcTime 
FROM (SELECT fullVisitorId, hits.transaction.transactionId as transactionId, visitNumber as visitNumber, 
CONCAT(CONCAT(SUBSTR(date, 7, 2), '.', SUBSTR(date, 5, 2), '.', SUBSTR(date, 0, 4)), ' ', CONCAT(STRING(hits.hour), ':', STRING(hits.minute))) as conversionTime 
FROM 
(TABLE_DATE_RANGE([54247008.ga_sessions_], 
       TIMESTAMP('2014-11-16'), 
       TIMESTAMP('2014-11-21'))) 
WHERE hits.transaction.transactionId is not null 
ORDER BY fullVisitorId, visitNumber 
) conversions 
JOIN EACH(
SELECT fullVisitorId, trafficSource.referralPath as referralPath, trafficSource.medium as medium, trafficSource.source as source, visitNumber,  
CONCAT(CONCAT(SUBSTR(date, 7, 2), '.', SUBSTR(date, 5, 2), '.', SUBSTR(date, 0, 4)), ' ', CONCAT(STRING(hits.hour), ':', STRING(hits.minute))) as clikcTime, 
trafficSource.campaign as campaign, trafficSource.keyword as keyword 
FROM (TABLE_DATE_RANGE([54247008.ga_sessions_], 
       TIMESTAMP('2014-11-16'), 
       TIMESTAMP('2014-11-21'))) 
GROUP BY fullVisitorId, referralPath, medium, source, visitNumber, clikcTime, campaign, keyword 
ORDER BY fullVisitorId, visitNumber 
) clickChanges 
ON 
conversions.fullVisitorId = clickChanges.fullVisitorId 
GROUP BY visitor, transactionId, visitNumber, transactionVisitNumber, source, referralPath, medium, keyword, campaign, conversionTime 
HAVING visitNumber <= transactionVisitNumber 
ORDER BY transactionId, visitNumber 
) 
GROUP EACH BY transactionId 
) b 
ON 
b.max_visitnumber = a.visitnumber and 
b.transactionId = a.transactionId 
ORDER BY max_visitnumber DESC 
  1. 所以我已經把幾乎相同的查詢兩次,只是改變它的一小部分。有沒有更好的方法來做到這一點,並縮短查詢?

  2. 我在StackOverflow上發現有人建議使用WITH AS的常規表達式,但它在BigQuery中似乎不起作用。任何其他想法使這個查詢更有效率?

+1

使用視圖...... – Pentium10 2014-12-05 13:32:11

+0

你能詳細解釋一下嗎?我是一個完全新手,只是調整了一位前同事寫的查詢 – Mustika 2014-12-05 13:39:47

+0

以下是對BigQuery文檔的引用:https://cloud.google.com/bigquery/querying-data#views – 2014-12-05 18:58:58

回答

3

正如評論所說,視圖就是你想要的:一個查詢的別名,你可以在其他查​​詢中重用。

https://cloud.google.com/bigquery/querying-data#views

注意看法[當前]不兼容TABLE_DATE_RANGE,所以你需要重寫你的查詢/視圖明確地叫出表。