Skip to content

Backmerge - fix grouping problem in bq#1276

Merged
wivern merged 1 commit into
masterfrom
issue-1267-fix_bq_grouping_backmerge
Aug 2, 2019
Merged

Backmerge - fix grouping problem in bq#1276
wivern merged 1 commit into
masterfrom
issue-1267-fix_bq_grouping_backmerge

Conversation

@ssuvorov-fls

Copy link
Copy Markdown
Contributor

fixes #1267

@ssuvorov-fls ssuvorov-fls requested a review from wivern August 2, 2019 11:46
@wivern wivern merged commit f5c6e92 into master Aug 2, 2019
@wivern wivern deleted the issue-1267-fix_bq_grouping_backmerge branch August 2, 2019 11:57
@chrisknoll

chrisknoll commented Aug 2, 2019

Copy link
Copy Markdown
Collaborator

Future reference on SQL formatting styles (this should probably go into a style guide somewhere) to make the code readable:

This was the format in the PR:

          INNER JOIN (SELECT iq1.cohort_definition_id, iq1.concept_id, Sum(iq1.count_value)
                   FROM   (
                     SELECT cohort_definition_id,
                            CAST(CASE WHEN isNumeric(stratum_1) = 1 THEN stratum_1 ELSE null END AS INT) AS concept_id,
                            count_value
                     FROM   @ohdsi_database_schema.heracles_results
                     WHERE  analysis_id IN ( 1830 )
                            AND cohort_definition_id = @cohortDefinitionId
                   ) iq1
                   GROUP  BY iq1.cohort_definition_id, iq1.concept_id
                   HAVING Sum(iq1.count_value) > @minCovariatePersonCount) ct1
               ON hr1.cohort_definition_id = ct1.cohort_definition_id 
                  AND hr1.concept_id = ct1.concept_id 

Should be formatted as so (overall indent should be applied based on where this appears in the outer query, but i'm intenting from column 0 to demonstrate):

INNER JOIN (
  SELECT iq1.cohort_definition_id, iq1.concept_id, Sum(iq1.count_value)
  FROM   (
    SELECT cohort_definition_id,
      CAST(CASE WHEN isNumeric(stratum_1) = 1 THEN stratum_1 ELSE null END AS INT) AS 
concept_id,
      count_value
    FROM   @ohdsi_database_schema.heracles_results
    WHERE  analysis_id IN ( 1830 )
      AND cohort_definition_id = @cohortDefinitionId
  ) iq1
  GROUP  BY iq1.cohort_definition_id, iq1.concept_id
  HAVING Sum(iq1.count_value) > @minCovariatePersonCount
) ct1 ON hr1.cohort_definition_id = ct1.cohort_definition_id 
  AND hr1.concept_id = ct1.concept_id 

Specific rules are:

SELECT - FROM - JOIN - WHERE - GROUP BY - ORDER BY should all be at the same indentation level.
Additional expressions for any of the above should be indented
The ON of a closing JOIN is on the same line.
Subqueries begin with ( on the line, followed with an indented block for the subquery (following the above rules) Sub-queries close on new line.

Example:

select col1, col2, col3
  col4, col5 -- col4 and col5 are additional expressions to select, so should be indented
from table1 t1 -- indenting is at the same level as select
join table2 t2 on t1.col1 = t2.col1 -- indenting is at  same level as select/from
left join ( -- indenting same level as select/from/join and subquery begins with ( on the  same line
  select col1, col2, 
    MAX(col3) as max_col3 -- indented expression belongs to select
  from table3 t3
  group by col1, col2 -- indented aligned with the select/from of this subquery
) z on z.col1 = t2.col1
order by t1.col1 asc, t1.col1 desc -- indented aligned with the top-level select/from/join that the order by applies to

I hope this is clear, if there are any questions, please don't hesitate to ask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid query for BigQuery

3 participants