在bigquery中根据ID和时间对数据进行分组

时间:2020-07-08 07:26:29

标签: sql google-bigquery

我正在查询,可以根据ID和时间汇总数据。我已经成功地基于ID汇总了数据,但是当我添加时间时,查询无法识别时间列。

示例代码:

SELECT
DATE(Request_Timestamp) as Date,
     ID, 
 SUM(CASE WHEN Language = 'EN' THEN 1 ELSE 0 END) AS lang_EN,
 SUM(CASE WHEN Language = 'SN' THEN 1 ELSE 0 END) AS lang_SN,

 FROM (
   SELECT 
    DATE(Request_Timestamp),
     ID, 
      CASE 
        WHEN Language in ('EN', 'English') THEN 'EN' 
        WHEN Language in ('MY', 'Malay')  THEN 'MY' ELSE Language 
      END AS Language,
     
   FROM table
   GROUP BY   ID, Language, DATE(Request_Timestamp)
 )
 GROUP BY  ID, DATE(Request_Timestamp)
)

样本输入:

enter image description here

样本输出

enter image description here

错误(在SELECT之后):

Unrecognized name: Request_Timestamp at [9:6] 

已更新:它适用于Request_Timestamp,但不适用于DATE(Request_Timestamp)

3 个答案:

答案 0 :(得分:2)

已解决:

SELECT
     DATE,
        ..
    
     FROM (
       SELECT 
        DATE(Request_Timestamp)as DATE,
         ID, 
          CASE 
           ..
          END AS Language,
         
       FROM table
       GROUP BY   ID, Language, Request_Timestamp
     ) t
     GROUP BY ID, DATE, Language 

答案 1 :(得分:0)

别名被遗漏,多余,我被删除了吗

Full traceback: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-41-b8b10328035d> in <module>()
----> 1 import librosa
      2 import numpy
      3 import skimage
      4 import pkg_resources
      5 from setuptools.py31compat import iter_entry_points

/anaconda3/lib/python3.6/site-packages/librosa/__init__.py in <module>()
     10 # And all the librosa sub-modules
     11 from ._cache import cache
---> 12 from . import core
     13 from . import beat
     14 from . import decompose

/anaconda3/lib/python3.6/site-packages/librosa/core/__init__.py in <module>()
    123 """
    124 
--> 125 from .time_frequency import *  # pylint: disable=wildcard-import
    126 from .audio import *  # pylint: disable=wildcard-import
    127 from .spectrum import *  # pylint: disable=wildcard-import

/anaconda3/lib/python3.6/site-packages/librosa/core/time_frequency.py in <module>()
      9 import six
     10 
---> 11 from ..util.exceptions import ParameterError
     12 from ..util.deprecation import Deprecated
     13 

/anaconda3/lib/python3.6/site-packages/librosa/util/__init__.py in <module>()
     75 """
     76 
---> 77 from .utils import *  # pylint: disable=wildcard-import
     78 from .files import *  # pylint: disable=wildcard-import
     79 from .matching import *  # pylint: disable=wildcard-import

/anaconda3/lib/python3.6/site-packages/librosa/util/utils.py in <module>()
      8 
      9 import numpy as np
---> 10 import numba
     11 from numpy.lib.stride_tricks import as_strided
     12 

/anaconda3/lib/python3.6/site-packages/numba/__init__.py in <module>()
     26 
     27 # Re-export types itself
---> 28 import numba.core.types as types
     29 
     30 # Re-export all type names

AttributeError: module 'numba' has no attribute 'core'

答案 2 :(得分:0)

您似乎只想要条件聚合。因为您只想计算匹配的布尔条件,所以请使用COUNTIF()

SELECT DATE(Request_Timestamp) as Date, ID, 
       COUNTIF(Language in ('EN', 'English')) AS lang_EN,
       COUNTIF(Language in ('MY', 'Malay')) AS lang_MY
FROM table
GROUP BY DATE, ID;