创建查询sql,我添加了一个新列,其中包含我的属性操作的结果

时间:2021-10-10 19:19:35

My query would be

我的疑问是

select obj, sum (att1), sum (att2)
from T1
group by obj
order by / * here I need to sort the obj by the results obtained between att1 / att2. * / -

Important the attribute of the table T1 obj is repeated but with different values ​​of att1 and att2 ---

重要的是表T1 obj的属性重复但是具有不同的att1和att2值---

what I want is shown in this table

我想要的是这张表

Obj Att1. Att2. ResultOper 
AA. 20.    4.        5 
B.B. 14.    2.       7 
Cc. 10.     5.        2

1 个解决方案

#1


0  

"I need to sort the obj by the results obtained between att1 / att2":

“我需要通过att1 / att2之间获得的结果对obj进行排序”:

select obj, sum (att1) s1, sum (att2) s2, sum (att1) / sum (att2) s3
from T1
group by obj
order by s3

Or, wrap it up in a derived table:

或者,将其包装在派生表中:

select obj, s1, s2,  s1 / s2 as s3
from
(
    select obj, sum(att1) s1, sum(att2) s2
    from T1
    group by obj
) dt
order by s3

#1


0  

"I need to sort the obj by the results obtained between att1 / att2":

“我需要通过att1 / att2之间获得的结果对obj进行排序”:

select obj, sum (att1) s1, sum (att2) s2, sum (att1) / sum (att2) s3
from T1
group by obj
order by s3

Or, wrap it up in a derived table:

或者,将其包装在派生表中:

select obj, s1, s2,  s1 / s2 as s3
from
(
    select obj, sum(att1) s1, sum(att2) s2
    from T1
    group by obj
) dt
order by s3