Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql BigQuery:在一个查询中获取中值和不同计数_Sql_Google Bigquery - Fatal编程技术网

Sql BigQuery:在一个查询中获取中值和不同计数

Sql BigQuery:在一个查询中获取中值和不同计数,sql,google-bigquery,Sql,Google Bigquery,我有一个带有identity\u id、session\u length和第一次约会日期的表(每个identity都是唯一的)。我需要计算中间长度,但也要计算表中的distinctidentity\u id。我很难同时执行两个查询 下面的查询应该正确地计算了中间长度(我希望如此!): 但是,当我将COUNT(DISTINCT identity\u id)或COUNT(1)添加到FROM的内部查询时,我会收到错误,请求将identity\u id包含在分组依据中。这完全有道理,但当我这样做的时候,

我有一个带有
identity\u id
session\u length
第一次约会日期的表(每个identity都是唯一的)。我需要计算中间长度,但也要计算表中的distinct
identity\u id
。我很难同时执行两个查询

下面的查询应该正确地计算了
中间长度
(我希望如此!):

但是,当我将
COUNT(DISTINCT identity\u id)
COUNT(1)
添加到
FROM
内部
查询时,我会收到错误,请求将identity\u id包含在
分组依据中。这完全有道理,但当我这样做的时候,我得到了另一个错误,抱怨中位数长度没有被分组或聚合

表格示例

身份识别码 会话长度 第一次约会 1. 23 01-01-2020 2. 3. 01-02-2020 1. 2. 01-01-2020 3. 11 01-03-2020 4. 22 01-04-2020 5. 28 01-05-2020 2. 13 01-02-2020 2. 73 01-02-2020 3. 29 01-03-2020
您可以使用聚合:

SELECT identity_id, median_length,
       MIN(first_date), COUNT(*)
FROM (SELECT t.*,
             PERCENTILE_CONT(session_length, 0.5) OVER(PARTITION BY identity_id) AS median_length
      FROM my_table t
     ) t
GROUP BY identity_id, median_length;

您可以使用聚合:

SELECT identity_id, median_length,
       MIN(first_date), COUNT(*)
FROM (SELECT t.*,
             PERCENTILE_CONT(session_length, 0.5) OVER(PARTITION BY identity_id) AS median_length
      FROM my_table t
     ) t
GROUP BY identity_id, median_length;
下面考虑一下

create temp function median (arr any type) as (
  if(mod(array_length(arr), 2) = 0,
    ( arr[offset(div(array_length(arr), 2) - 1)] +
      arr[offset(div(array_length(arr), 2))])  / 2,
      arr[offset(div(array_length(arr), 2))] )
);
select identity_id, 
  median(array_agg(session_length order by session_length)) as median_length,
  min(first_date) as first_date, 
  count(1) as `count`
from `project.dataset.table`
group by identity_id   
如果应用于问题中的样本数据,则输出为

考虑以下内容

create temp function median (arr any type) as (
  if(mod(array_length(arr), 2) = 0,
    ( arr[offset(div(array_length(arr), 2) - 1)] +
      arr[offset(div(array_length(arr), 2))])  / 2,
      arr[offset(div(array_length(arr), 2))] )
);
select identity_id, 
  median(array_agg(session_length order by session_length)) as median_length,
  min(first_date) as first_date, 
  count(1) as `count`
from `project.dataset.table`
group by identity_id   
如果应用于问题中的样本数据,则输出为