使用 GROUP BY 时如何计算不同值

问题描述 投票:0回答:1

我想根据 Teradata 中的“group by”语句对不同值进行计数,并将这些值保存到列中。

这是我的 group by 子句之后的数据示例:

Team_ID  | Individual_ID | Num_Inidividual_Books | 
--------------------------------------------------
   1     |     02        |          4            |
--------------------------------------------------
--------------------------------------------------
   1     |     03        |          1            |
--------------------------------------------------
--------------------------------------------------
   1     |     01        |          4            |
--------------------------------------------------
--------------------------------------------------
   2     |     05        |          11            |
--------------------------------------------------
--------------------------------------------------
   2     |     09        |          11            |
--------------------------------------------------
--------------------------------------------------
   2     |     08        |          11            |
--------------------------------------------------

我想添加一个“分组的不同值”列,所以它看起来像这样:

Team_ID  | Individual_ID | Num_Inidividual_Books | Num_Distinct_Book_Amount_Per_Grouping |
------------------------------------------------------------------------------------------
   1     |     02        |          4            |                2
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
   1     |     03        |          1            |                2
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
   1     |     01        |          4            |                2
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
   2     |     05        |          11            |               1
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
   2     |     09        |          11            |               1
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
   2     |     08        |          11            |               1
------------------------------------------------------------------------------------------

我尝试在查询中使用 COUNT() 和 COUNT(DISTINCT) 与 GROUP BY 子句,但出现错误:

Cannot nest non-aggregate functions.
我认为我的处理方式错误。有什么建议吗?

如果我需要提供更多信息,请告诉我。 谢谢!

sql group-by count teradata
1个回答
0
投票

你可以试试这个:

SELECT a.team_id,
       a.individual_id,
       a.num_individual_books,
       (SELECT COUNT(DISTINCT b.num_individual_books)
        FROM   my_table b
        WHERE  a.team_id = b.team_id) AS num_distinct_books
FROM   my_table a;
© www.soinside.com 2019 - 2024. All rights reserved.