使用case语句作为if语句

时间:2018-04-20 18:52:28

标签: google-bigquery

我试图在BigQuery中创建一个IF语句。我已经构建了一个可以工作但不会从表中选择数据的概念,我只能将其显示为1或0

示例:

SELECT --AS STRUCT 
 CASE
 WHEN (
       Select Count(1) FROM (  --  If the records are the same, then return = 0, if the records are not the same then > 1
                              Select Distinct ESCO, SOURCE, LDCTEXT, STATUS,DDR_DATE, TempF, HeatingDegreeDays, DecaTherms
                              from `gas-ddr.gas_ddr_outbound.LexingtonDDRsOutbound_onchange_Prior_Filtered` 
                                Except Distinct
                              Select Distinct ESCO, SOURCE, LDCTEXT, STATUS,DDR_DATE, TempF, HeatingDegreeDays, DecaTherms
                              from `gas-ddr.gas_ddr_outbound.LexingtonDDRsOutbound_onchange_Latest_Filtered` 
                           )
      )= 0 
  THEN 
(Select * from `gas-ddr.gas_ddr_outbound.LexingtonDDRsOutbound_onchange_Latest`)  -- This Does not 
work  Scalar subquery cannot have more than one column unless using SELECT AS 
STRUCT to build STRUCT values at [16:4] END




 SELECT --AS STRUCT 
CASE
 WHEN (
       Select Count(1) FROM (  --  If the records are the same, then return = 0, if the records are not the same then > 1
                              Select Distinct ESCO, SOURCE, LDCTEXT, STATUS,DDR_DATE, TempF, HeatingDegreeDays, DecaTherms
                              from `gas-ddr.gas_ddr_outbound.LexingtonDDRsOutbound_onchange_Prior_Filtered` 
                                Except Distinct
                              Select Distinct ESCO, SOURCE, LDCTEXT, STATUS,DDR_DATE, TempF, HeatingDegreeDays, DecaTherms
                              from `gas-ddr.gas_ddr_outbound.LexingtonDDRsOutbound_onchange_Latest_Filtered` 
                           )
      )= 0 
  THEN 1 --- This does work

否则 0 END

如何获取此查询以返回现有表的结果?

1 个答案:

答案 0 :(得分:2)

你的问题仍然有点通用,所以我的回答也一样 - 只是模仿你的用例,我可以从你的评论中反向设计它

所以,在下面的代码中 - project.dataset.yourtable模仿你的表格;而 project.dataset.yourtable_Prior_Filteredproject.dataset.yourtable_Latest_Filtered模仿您的各自观点

   
#standardSQL
WITH `project.dataset.yourtable` AS (
  SELECT 'aaa' cols, 'prior' filter UNION ALL  
  SELECT 'bbb' cols, 'latest' filter 
), `project.dataset.yourtable_Prior_Filtered` AS (
  SELECT cols FROM `project.dataset.yourtable` WHERE filter = 'prior'
), `project.dataset.yourtable_Latest_Filtered` AS (
  SELECT cols FROM `project.dataset.yourtable` WHERE filter = 'latest'
), check AS (
  SELECT COUNT(1) > 0 changed FROM (
    SELECT DISTINCT cols FROM `project.dataset.yourtable_Latest_Filtered`
    EXCEPT DISTINCT
    SELECT DISTINCT cols FROM `project.dataset.yourtable_Prior_Filtered`
  )
)
SELECT t.* FROM `project.dataset.yourtable` t
CROSS JOIN check WHERE check.changed

结果是

Row cols    filter   
1   aaa     prior    
2   bbb     latest   

如果您将表格更改为

WITH `project.dataset.yourtable` AS (
  SELECT 'aaa' cols, 'prior' filter UNION ALL  
  SELECT 'aaa' cols, 'latest' filter 
) ......

结果将是

Row cols    filter   
Query returned zero records.

我希望这能给你正确的方向

  

添加了更多解释:

我可能是错的 - 但根据您的问题 - 看起来您有一个表project.dataset.yourtable和两个观看project.dataset.yourtable_Prior_Filteredproject.dataset.yourtable_Latest_Filtered,它们会在事件之前和之后呈现您的表格状态

所以,上面答案中的前三个CTE只是模仿了你在问题中描述的那些表和视图。 他们在这里,所以你可以看到概念,并可以在没有任何额外工作的情况下使用它,然后再将其调整为真实的用例。

对于您的真实用例,您应该省略它们并使用您的真实表格和视图名称以及所拥有的任何列 所以你要查询的是:

#standardSQL
WITH check AS (
  SELECT COUNT(1) > 0 changed FROM (
    SELECT DISTINCT cols FROM `project.dataset.yourtable_Latest_Filtered`
    EXCEPT DISTINCT
    SELECT DISTINCT cols FROM `project.dataset.yourtable_Prior_Filtered`
  )
)
SELECT t.* FROM `project.dataset.yourtable` t
CROSS JOIN check WHERE check.changed   
  

任何语言都应该是一个非常简单的IF语句   很不幸的是,不行!它只能通过简单的IF来完成,如果您认为它合适,您可以向BigQuery团队提交feature request,无论您认为有意义