简体   繁体   English

SQL 使用 sum 不按主键分组

[英]SQL use sum without grouping by primary keys

I am trying out a query wherein I want only one single sum for two different rows.我正在尝试一个查询,其中我只想要两个不同行的一个总和。

select     sum(holdings) 
from       market_data.mstar_holdings mh
inner join market_data.holder_type ht 
           on ht.holder_type_code = mh.mstar_security_specific_holder_type
where      mstar_investment_id = 'E0USA00AY9' 
           and ht.adjustment_flag = true 
           and ht.is_consolidated =false 
--group by ownership_percentage,
           ht.adjustment_threshold 
having ownership_percentage>ht.adjustment_threshold

Currently, in output, I am getting two records.目前,在 output 中,我得到了两条记录。 1000089 and 616295556 in two rows. 1000089 和 616295556 分两行。 But I want the output as 1000089+616295556 in one single row.但我希望 output 为 1000089+616295556 在一行中。 If I comment the group by line, it is giving me error.如果我逐行评论该组,它会给我错误。 Is there any way I'll get only one record each time summing up all holdings rows?有什么办法每次汇总所有馆藏行时我只会得到一条记录?

The query you placed in the comments is different from the one posted in the question.您在评论中提出的查询与问题中发布的查询不同。 At any rate, this works:无论如何,这有效:

select sum(holdings)
from (
    select     sum(holdings) as holdings
    from       market_data.mstar_holdings mh
    inner join market_data.holder_type ht 
               on ht.holder_type_code = mh.mstar_security_specific_holder_type
    where      mstar_investment_id = 'E0USA00AY9' 
               and ht.adjustment_flag = true 
               and ht.is_consolidated =false 
    group by ownership_percentage, ht.adjustment_threshold 
    --having ownership_percentage >ht.adjustment_threshold --explain why you have this here. May need to move elsewhere, such as where statement. 
)z

Fiddle found here .小提琴在这里找到

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

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