SQL分组计算百分比

发布于 2025-01-22 08:53:06 字数 1058 浏览 0 评论 0原文

我是这个领域的新手,很抱歉我的经验不足。

我有该类别的类别名称和不同的值条目。我想将这些名称相同的名称分组,并根据总计获得百分比。 简而言之,“ 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

Here is my output

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2025-01-29 08:53:06

您基本上有TWI选项。

首先,使用连接的子选择

select 
    i1.Category_NM
    ,SUM(Payment) odemeler
    ,CONVERT(decimal(5,1),  (
                                SUM(i2.sum_pay)
                                / (select sum(Payment) from Islem where Category_Type = 'Gider')
                            ) *100) 
from Islem i1
JOIN (select Category_NM , sum(Payment) sum_pay from Islem where Category_Type = 'Gider' group by Category_NM) i2 ON i1.Category_NM = i2.Category_NM
where i1.Category_Type = 'GİDER'

或通过离子subselect删除组,然后仅总和那些wit wit at catecomory_nm

select 
    Category_NM
    ,SUM(Payment) odemeler
    ,CONVERT(decimal(5,1),  (
                                (select sum(Payment) from Islem i2 where Category_Type = 'Gider'  AND i2.Category_NM = i1.Category_NM) 
                                / (select sum(Payment) from Islem where Category_Type = 'Gider')
                            ) *100) 
from Islem i1 where Category_Type = 'GİDER'

两个查询都无法产生您的错误

 创建表ISLEM(
类别_nm int,
  aCTORY_TYPE VARCHAR(100),
付款浮动
 );
 
  GO
 
 插入ISLEM值(1,'gíder',1.2),(1,'gíder',2.2),(1,'gíder',3.2),3.2),3.2),3.2),3.2),
(2,'gíder',1.2),(2,'gíder',2.2),(2,'gieder',3.2),3.2)
去
 
 选择 
    类别_nm
    ,总和(付款)OdeMeler
    ,转换(十进制(5,1)((5,1))((
                                (从islem i2中选择sum(付款)category_type ='gider'和i2.category_nm = i1.category_nm) 
                                /(从islem中选择sum(付款)category_type ='gider')
                            ) *100) 
从islem i1中,category_type ='gíder'
按类别_NM组组
 
  GO
 
 category_nm | ODEMELER | (无列名)
-----------:| -------:| -------------------:
          1 | 6.6 | 50.0
          2 | 6.6 | 50.0
 选择 
    i1.category_nm
    ,总和(付款)OdeMeler
    ,转换(十进制(5,1)((5,1))((
                                sum(i2.sum_pay)
                                /(从islem中选择sum(付款)category_type ='gider')
                            ) *100) 
来自Islem I1
join(select category_nm,sum(付款)sum_pay从islem where category_type ='gider'组(category_nm)i2 on i1.category_nm = i2.category_nm
其中i1.category_type ='gíder'
组由i1.category_nm
去
 
 category_nm | ODEMELER | (无列名)
-----------:| -------:| -------------------:
          1 | 6.6 | 150.0
          2 | 6.6 | 150.0

db&lt;

you have basically twi options.

First, use a joined subselect

select 
    i1.Category_NM
    ,SUM(Payment) odemeler
    ,CONVERT(decimal(5,1),  (
                                SUM(i2.sum_pay)
                                / (select sum(Payment) from Islem where Category_Type = 'Gider')
                            ) *100) 
from Islem i1
JOIN (select Category_NM , sum(Payment) sum_pay from Islem where Category_Type = 'Gider' group by Category_NM) i2 ON i1.Category_NM = i2.Category_NM
where i1.Category_Type = 'GİDER'

Or get rid of the Group By ion the subselect and only sum up those rows wit the same Category_NM

select 
    Category_NM
    ,SUM(Payment) odemeler
    ,CONVERT(decimal(5,1),  (
                                (select sum(Payment) from Islem i2 where Category_Type = 'Gider'  AND i2.Category_NM = i1.Category_NM) 
                                / (select sum(Payment) from Islem where Category_Type = 'Gider')
                            ) *100) 
from Islem i1 where Category_Type = 'GİDER'

both query can't produce your error

CREATE TABLE Islem (
Category_NM int,
  Category_Type VARCHAR(100),
Payment float
 );
GO
INSERT INTO Islem VALUES (1,'GİDER',1.2),(1,'GİDER',2.2),(1,'GİDER',3.2),
(2,'GİDER',1.2),(2,'GİDER',2.2),(2,'GİDER',3.2)
GO
select 
    Category_NM
    ,SUM(Payment) odemeler
    ,CONVERT(decimal(5,1),  (
                                (select sum(Payment) from Islem i2 where Category_Type = 'Gider'  AND i2.Category_NM = i1.Category_NM) 
                                / (select sum(Payment) from Islem where Category_Type = 'Gider')
                            ) *100) 
from Islem i1 where Category_Type = 'GİDER'
GROUP BY Category_NM
GO
Category_NM | odemeler | (No column name)
----------: | -------: | ---------------:
          1 |      6.6 |             50.0
          2 |      6.6 |             50.0
select 
    i1.Category_NM
    ,SUM(Payment) odemeler
    ,CONVERT(decimal(5,1),  (
                                SUM(i2.sum_pay)
                                / (select sum(Payment) from Islem where Category_Type = 'Gider')
                            ) *100) 
from Islem i1
JOIN (select Category_NM , sum(Payment) sum_pay from Islem where Category_Type = 'Gider' group by Category_NM) i2 ON i1.Category_NM = i2.Category_NM
where i1.Category_Type = 'GİDER'
GROUP  BY i1.Category_NM
GO
Category_NM | odemeler | (No column name)
----------: | -------: | ---------------:
          1 |      6.6 |            150.0
          2 |      6.6 |            150.0

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文