2016-06-24 4 views
1

私は現在、BigQueryで使用するために古いT-SQLスクリプトの一部を変換していますが、問題はあります。SELECTで使用された非集約フィールドCASE WHENの一環として、GROUP BYで宣言しなければなりません - 私は望みません!これはT-SQLの問題ではないようですが、これの周りには最初の3つのフィールドだけをグループ化する必要がありますか?グループ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