在表上汇总查询?

时间:2017-10-17 15:25:00

标签: mysql google-bigquery

我陷入困境,我必须从customer表中选择具有customer_id和amount_paid值的数据。我希望以以下形式显示结果:用户的前3个值应在列名组中显示为文本Group1,以及该用户的4到10个值以获取文本Group2和其余Group3。

您能否告诉我如何为每位客户分组价值?

谢谢

1 个答案:

答案 0 :(得分:2)

  

我希望以以下形式显示结果:用户的前3个值应在列名组中显示为文本Group1,以及该用户的4到10个值以获取文本Group2和rest3

   

以下是BigQuery Standard SQL

#standardSQL
SELECT
  user_id, 
  CASE 
    WHEN pos BETWEEN 1 AND 3 THEN 1
    WHEN pos BETWEEN 4 AND 10 THEN 2
    ELSE 3
  END grp,
  SUM(amount_paid) amount_paid
FROM (
  SELECT 
    user_id, amount_paid, 
    ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY amount_paid DESC) pos
  FROM customer 
)
GROUP BY user_id, grp
-- ORDER BY user_id, grp   

您可以使用以下虚拟生成数据进行测试/播放

#standardSQL
WITH users AS (
  SELECT user_id FROM UNNEST(GENERATE_ARRAY(1,5)) user_id
),
amounts AS (
  SELECT ROUND(50 * RAND()) amount_paid FROM UNNEST(GENERATE_ARRAY(1,50)) amount_paid
),
customer AS (
  SELECT user_id, ROUND(amount_paid * RAND()) amount_paid  
  FROM users
  CROSS JOIN amounts
)
SELECT
  user_id, 
  CASE 
    WHEN pos BETWEEN 1 AND 3 THEN 1
    WHEN pos BETWEEN 4 AND 10 THEN 2
    ELSE 3
  END grp,
  SUM(amount_paid) amount_paid
FROM (
  SELECT 
    user_id, amount_paid, 
    ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY amount_paid DESC) pos
  FROM customer 
)
GROUP BY user_id, grp
ORDER BY user_id, grp   

输出如下所示

user_id grp amount_paid  
1       1   147.0    
1       2   323.0    
1       3   879.0    
2       1   147.0    
2       2   323.0    
2       3   879.0    
 . . .    

所以你仍然需要计算分享(从你的问题并希望)对你来说不是问题

  

添加了份额计算

#standardSQL
WITH grps AS (
  SELECT
    user_id, 
    CASE 
      WHEN pos BETWEEN 1 AND 3 THEN 1
      WHEN pos BETWEEN 4 AND 10 THEN 2
      ELSE 3
    END grp,
    SUM(amount_paid) amount_paid
  FROM (
    SELECT 
      user_id, amount_paid, 
      ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY amount_paid DESC) pos
    FROM customer 
  )
  GROUP BY user_id, grp
)
SELECT * ,
  ROUND(amount_paid / SUM(amount_paid) OVER(PARTITION BY user_id), 3) share
FROM grps
-- ORDER BY user_id, grp