简体   繁体   English

从记录的字段之一之和大于

[英]Get record ids from groups where the sum of one of the field of their records is greater than

I have records as such: 我有这样的记录:

    Id  ForeignKey  Level   ValueA  ValueB
    1   1001        1       2       10
    2   1001        1       10      10
    3   1001        1       20      20
    4   1001        2       20      30
    5   1002        1       1       100
    6   1003        1       1       100
    7   1004        1       1       100

I want to get the Ids of each record of the groups grouped by ForeignKey and Level where the sum of the group's records' ValueA values divided by the sum of ValueB values is greater than 0.5 我想获取由ForeignKey和Level分组的组的每个记录的ID,其中组记录的ValueA值的总和除以ValueB值的总和大于0.5

In this case, I'd like to retrieve the Id of the three first records as (2 + 10 + 20) / (10 + 10 + 20) = 0.8 在这种情况下,我想检索(3 + 2 + 10 + 20)/(10 + 10 + 20)= 0.8的前三个记录的ID

Here is what I've got so far: 这是到目前为止我得到的:

    select 
    ForeignKey,
    SUM(ValueA) as ValueASum,
    SUM(ValueB) as ValueBSum,
    from tableA 
    group by ForeignKey
    having (SUM(ValueA) / SUM(ValueB) > 0.5)

The result is 结果是

    ForeignKey  ValueASum   ValueBSum
    1001        32          40

How do I get the ids of the records from this point? 从这一点上如何获取记录的ID? If I add the Id in the select, I must group on it and then have a group for each record. 如果我在选择中添加ID,则必须对其进行分组,然后为每个记录分组。

Thanks for your time 谢谢你的时间

Hm, how about 嗯,怎么样

select id from your_table where foreignkey = 1001

Is something wrong with working with multiple queries? 处理多个查询有问题吗?

If you want you can do a subquery: 如果需要,可以执行子查询:

select id from your_table where foreignkey in ( select foreignkey from ( <yourQuery> ) sq);

UPDATE: 更新:

select t.id from Table1 t
inner join 
( 
    select 
    ForeignKey, level,
    SUM(ValueA) as ValueASum,
    SUM(ValueB) as ValueBSum
    from Table1 
where level = 1
    group by ForeignKey, Level
    having (SUM(ValueA) / SUM(ValueB) > 0.5) ) sq
ON t.foreignkey = sq.foreignkey AND t.level = sq.level

I added where level = 1 just because your given resultset not what I get when I execute your query. 我添加where level = 1只是因为您给定的结果集不是执行查询时得到的结果。

See it working live in an sqlfiddle . 看到它在sqlfiddle中实时工作。

You were on the right track, but if you wanted it from each "Level", you would need to add that into your group by also. 您的方向是正确的,但是如果您希望每个“级别”都需要它,则还需要将其添加到您的组中。

select
      tA2.ID,
      tA2.ForeignKey,
      tA2.Level,
      tA2.ValueA,
      tA2.ValueB
   from
      ( select 
              tA.ForeignKey,
              tA.Level,
              SUM(tA.ValueA) as ValueASum,
              SUM(tA.ValueB) as ValueBSum,
           from 
              tableA tA
           group by 
              tA.ForeignKey,
              tA.Level
           having 
              (SUM(tA.ValueA) / SUM(tA.ValueB) > 0.5) ) PreQualified
      JOIN tableA tA2
         on PreQualified.ForeignKey = tA2.ForeignKey
        AND PreQualified.Level = tA2.Level

This would give all values that matched the qualifying condition. 这将给出与合格条件匹配的所有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM