1
我目前正在將一些舊的T-SQL腳本轉換爲在BigQuery中使用 - 但我遇到了一個問題 - 似乎對於SELECT中使用的每個非聚合字段,甚至作爲CASE WHEN的一部分,必須在GROUP BY中聲明 - 我不想!這在T-SQL中似乎沒有問題,無論如何,我只需要在前三個字段上進行分組?Group by by Google BigQuery
即無需:
ss.UK_Sample_Size,
ss.Study_Design_Type
代碼附:預先
SELECT
LCRN AS [OrganisationName],
os.populationinmillions as [population_in_millions],
os.percentageoftotalpopulation as [percentage_of_total_population],
SUM([recruitmentcount]) as recruitment,
SUM(CASE WHEN ss.UK_Sample_Size >=10000 then sum(recruitmentcount) end) as Band1_Recruitment,
SUM(CASE WHEN (ss.Study_Design_Type = 'Observational' or ss.Study_Design_Type = 'Not Specified') and (ss.UK_Sample_Size < 10000 or ss.UK_Sample_Size is null) then sum(recruitmentcount) end) as Observational_Recruitment,
SUM(CASE WHEN (ss.Study_Design_Type = 'Interventional' or ss.Study_Design_Type = 'Both') and (ss.UK_Sample_Size < 10000 or ss.UK_Sample_Size is null) then sum(recruitmentcount) end) as Interventional_Recruitment
// PROBLEM
// 1. We dont want to group on study design type and uk sample size
// 2. We want to select by a date value held as a variable
FROM
[mydataset.BQ_Upload_ALL] AS bq
JOIN
[mydataset.Study_Summary] AS ss
ON
bq.studyid = ss.study_id
JOIN
[mydataset.ONS_Population] as os
ON
bq.LCRN = os.LocalNetwork
WHERE
recruitmentactivitydate_fy = '2016/17'
GROUP BY
[OrganisationName],
[population_in_millions],
[percentage_of_total_population],
ss.UK_Sample_Size,
ss.Study_Design_Type
ORDER BY
recruitment DESC;
謝謝,
戴夫