SQL分组计算百分比
我是这个领域的新手,很抱歉我的经验不足。
我有该类别的类别名称和不同的值条目。我想将这些名称相同的名称分组,并根据总计获得百分比。 简而言之,“ x是y数字的百分比?”
我想处理的查询;
select Category_NM as 'Categories',Convert(decimal(10,2),AVG(Payment)) as 'Average'
from Islem
where Category_Type = 'Gider'
group by Category_NM
我想喜欢此查询
select
Category_NM,SUM(Payment) odemeler,
CONVERT(decimal(5,1), ((select sum(Payment)
from Islem
where Category_Type = 'Gider'
group by Category_NM) /
(select sum(Payment)
from Islem
where Category_Type = 'Gider')) *100)
from Islem
where Category_Type = 'GİDER'
,然后我考虑此错误
子查询返回超过1个值。当子查询跟随=,!=,<,< = ,,> =或当子查询用作表达式时。
我应该怎么办
I'm new to this field, sorry for my inexperience so;
I have category names and different value entries for that category. I want to group the ones with the same name and get the percentage according to the grand total with the results.
In short, "x is what percent of y number?"
The query I want to process ;
select Category_NM as 'Categories',Convert(decimal(10,2),AVG(Payment)) as 'Average'
from Islem
where Category_Type = 'Gider'
group by Category_NM
and I want to like this query
select
Category_NM,SUM(Payment) odemeler,
CONVERT(decimal(5,1), ((select sum(Payment)
from Islem
where Category_Type = 'Gider'
group by Category_NM) /
(select sum(Payment)
from Islem
where Category_Type = 'Gider')) *100)
from Islem
where Category_Type = 'GİDER'
and I take this error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
What should I do
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您基本上有TWI选项。
首先,使用连接的子选择
或通过离子subselect删除组,然后仅总和那些wit wit at catecomory_nm
两个查询都无法产生您的错误
db&lt;
you have basically twi options.
First, use a joined subselect
Or get rid of the Group By ion the subselect and only sum up those rows wit the same Category_NM
both query can't produce your error
db<>fiddle here