2016-06-24 138 views
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; 

謝謝,

戴夫

回答

1

下面嘗試針對頻帶/觀測/介入內側(移動資格邏輯子選項)

SELECT 
    [OrganisationName], 
    [population_in_millions], 
    [percentage_of_total_population], 
    SUM([recruitmentcount]) AS recruitment, 
    SUM(Band1_recruitmentcount) AS Band1_Recruitment, 
    SUM(Observational_recruitmentcount) AS Observational_Recruitment, 
    SUM(Interventional_recruitmentcount) AS Interventional_Recruitment 
FROM (
    SELECT 
    LCRN AS [OrganisationName], 
    os.populationinmillions AS [population_in_millions], 
    os.percentageoftotalpopulation AS [percentage_of_total_population], 
    recruitmentcount, 
    CASE WHEN ss.UK_Sample_Size >= 10000 THEN recruitmentcount ELSE 0 END AS Band1_recruitmentcount, 
    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 recruitmentcount ELSE 0 END AS Observational_recruitmentcount, 
    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 recruitmentcount ELSE 0 END AS Interventional_recruitmentcount 
    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], 
ORDER BY recruitment DESC