【问题标题】:tSQL aggregate functions and group bystSQL 聚合函数和分组依据
【发布时间】:2020-04-15 17:47:04
【问题描述】:

所以,我似乎无法找到让它工作的方法。但是,我需要的是如下。

我有一张可以说类型的表。

Type_ID, Type_Description

然后我有一个项目表。 [Item Type是fk to type table]

Item_ID, Item_Type

然后我有一个结果表。 [Item_ID 是对 Item 表的 fk]

Result_ID, Item_ID, Cost

所以我需要的输出是按 Type_ID 分组的 - 项目数(可以是0), - 结果计数(可以为 0), - 和Cost之和(可以为0)

我没有直接访问这些表的权限。我必须构建 sql 并将其发送到 api,所以我不知道错误,如果成功则只知道结果,如果不知道错误 500。

似乎是较旧的 tSQL。作为 List 和 STRING_AGG 似乎不可用。

编辑:根据要求 - 示例数据

 +---------+------------------+ 
 | Type_ID | Type_Description | 
 +---------+------------------+
 |    1    |    Example 1     |
 +---------+------------------+
 |    2    |    Example 2     |
 +---------+------------------+
 +---------+---------------+ 
 | ITEM_ID |   ITEM_TYPE   | 
 +---------+---------------+
 |    1    |       1       |
 +---------+---------------+
 |    2    |       1       |
 +---------+---------------+
 |    3    |       1       |
 +---------+---------------+
 |    4    |       2       |
 +---------+---------------+
 |    5    |       2       |
 +---------+---------------+
 +-----------+---------+------+ 
 | Result_ID | Item_ID | Cost | 
 +-----------+---------+------+ 
 |     1     |    1    |  10  |
 +-----------+---------+------+
 |     2     |    1    |  20  |
 +-----------+---------+------+
 |     3     |    2    |   5  |
 +-----------+---------+------+
 |     4     |    5    |  100 |
 +-----------+---------+------+ 

期望的输出

 +---------+------------+--------------+------+
 | Type_ID | Item_Count | Result_Count | Cost |
 +---------+------------+--------------+------+
 |    1    |      3     |       3      |  35  |
 +---------+------------+--------------+------+
 |    2    |      2     |       1      |  100 |
 +---------+------------+--------------+------+

【问题讨论】:

  • 请添加样本数据和想要的结果。
  • @GordonLinoff 完成
  • 根据您的解释,我认为您需要在最终结果中添加 Type_ID 3 和全 0。否则,你会得到错误的答案。

标签: sql tsql join group-by count


【解决方案1】:

您可以加入和聚合。假设您的表名为typestypes_costscosts,则为:

select 
    t.type_id, 
    count(distinct tc.item_id) item_count, 
    count(distinct c.result_id) result_count, 
    sum(c.cost) cost
from types t
inner join types_costs tc on tc.item_type = t.type_id
left costs c on c.item_id = tc.item_id
group by t.type_id

一个重要的事情是使用left join 来带来costs 表,这样item_ids 在costs 中不存在的@s 不会在您更改计数之前被消除。根据您的实际用例,您可能还需要在表types_costs 上使用left join

【讨论】:

    【解决方案2】:

    我觉得GMB的回答挺好的,但是万一有没有物品的类型(你要求的东西),就不会显示了。 所以首先让我们创建输入数据:

    select 1 as Type_ID, 'Example 1' as Type_Description into #type
    union all
    select 2, 'Example 2'
    union all
    select 3, 'Example 3'
    
    select 1 Result_ID, 1 Item_ID, 10 Cost into #item
    union all
    select 2, 1, 20 Cost
    union all
    select 3, 2, 5 Cost
    union all
    select 4, 5, 100 Cost
    
    select 1 Item_ID, 1 Item_Type INTO #item_type
    union all
    select 2, 1
    union all
    select 3, 1
    union all
    select 4, 2
    union all
    select 5, 2
    

    注意我还添加了一个没有项目的类型 3 来测试没有项目的情况。

    然后是你需要的查询:

    SELECT
        t.Type_ID, 
        COUNT(DISTINCT it.Item_ID) Item_Count,
        COUNT(DISTINCT i.Result_ID) Result_Count, 
        SUM(ISNULL(Cost, 0)) Cost
    FROM #type t
    LEFT JOIN #item_type it on it.Item_Type = t.Type_ID
    LEFT JOIN #item i on i.Item_ID = it.Item_ID
    GROUP BY t.Type_ID
    

    我认为这很简单,不需要太多解释,但如果需要,请随时在 cmets 中询问。

    结果就像你要求的一样,还有一个类型 3 的行:

     +---------+------------+--------------+------+
     | Type_ID | Item_Count | Result_Count | Cost |
     +---------+------------+--------------+------+
     |    1    |      3     |       3      |  35  |
     +---------+------------+--------------+------+
     |    2    |      2     |       1      |  100 |
     +---------+------------+--------------+------+
     |    3    |      0     |       0      |  0   |
     +---------+------------+--------------+------+
    

    您提到项目数可能为 0,结果数也可能为 0。但是这两个值不是总是0,还是两者都是> 0?只有在您的类型项目多对多表没有 FK 的情况下,您才会遇到这种情况。例如,如果我添加:

    insert into #item_type
    select 6, 3
    

    那么最后一行是:

     +---------+------------+--------------+------+
     |    3    |      1     |       0      |  0   |
     +---------+------------+--------------+------+
    

    我不确定这在您的场景中是否有意义,但由于您的帖子暗示项目和结果可以独立为 0,这让我有点困惑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 2021-10-29
      • 2020-01-17
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多