oracle SQL如何通过不同的id进行条件求和?

vnzz0bqm  于 2022-12-03  发布在  Oracle
关注(0)|答案(8)|浏览(189)

假设您的结果集如下所示:

DATE          ID    cost    
---------------------------------------
 01/01/2011    1     10      
 01/01/2011    1     10      
 01/01/2011    2     10      
 01/01/2011    2     10

我需要一种方法来计算成本值的总和,但仅对每个不同的ID计算一次,以便在按日期分组时得到如下结果
第一次
我第一次尝试的是

sum(distinct cost)

但诅咒只返回10我也试过了:

sum(case when distinct id then cost else 0 end)

但这不是函数查询。

91zkwejq

91zkwejq1#

我将假设同一个ID在同一天总是有相同的开销。我还将假设您的RDBMS支持派生表。在这种情况下,您需要的是:

select date, sum(cost)
from 
  (select distinct date, id, cost from YourTable)
group by date

已更新

Oracle派生表不需要别名。

jvlzgdj9

jvlzgdj92#

I think all you want to do here is group by both date and id, as MPelletier wrote.
FWIW - you can do a distinct inside an aggregate in some dbs: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_sum
And even better, use group by with rollup to get totals along with the grouping: http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

SELECT DATE, ID, SUM(cost)
FROM table_name
GROUP BY DATE, ID WITH ROLLUP;
nbewdwxp

nbewdwxp3#

我认为张贴问题的逻辑有一些缺陷。
假设您有以下结果集:

DATE          ID    cost    
---------------------------------------
 01/01/2011    1     10      
 01/01/2011    1     15     
 01/01/2011    2     10      
 01/01/2011    2     10

查询应返回什么?

DATE          cost    
---------------------------------------
 01/01/2011    20

DATE          cost    
---------------------------------------
 01/01/2011    25

接受的答案将返回35
还是说这样的一套根本不可能有?

flmtquvp

flmtquvp4#

不了解数据库/SQL引擎:
SELECT日期,SUM(成本)FROM您的表GROUP BY日期,ID;

SELECT Date, First(TCost)
FROM
(SELECT Date, Sum(Cost) as TCost FROM YourTable GROUP BY Date, ID)
GROUP BY Date
1u4esq0p

1u4esq0p5#

您可以使用此查询。

SELECT dates, Sum(cost) as TCost FROM TABLE_NAME GROUP BY  ID
57hvy0tb

57hvy0tb6#

也许你可以用这个黑客。
将您要加总的值-cost-与id字段连接起来,并在它们之间插入一个字母,例如'a'。您可以将DISTINCT用于这些连接的字段,以便在聚合函数中只使用一次。
当你使用SUM时,这些字符串字段自动转换为整数,MySQL会在第一个字母处将其截断。
这是因为有许多不同的字符串可以转换为值1,例如“1”、“1”或“1a”。
来源:https://dev.mysql.com/doc/refman/5.7/en/type-conversion.html

SELECT
    id,
    SUM(cost),
    SUM(DISTINCT cost),
    GROUP_CONCAT(CONCAT(cost, 'a', id)),
    GROUP_CONCAT(DISTINCT CONCAT(cost, 'a', id)),
    SUM(DISTINCT CONCAT(cost, 'a', id)) expectedSum
FROM (
    SELECT '2011-01-01' AS date, 1 AS id, 10 AS cost
    UNION ALL SELECT '2011-01-01', 1, 10
    UNION ALL SELECT '2011-01-01', 2, 10
    UNION ALL SELECT '2011-01-01', 2, 10
) __tmp
GROUP BY
    id

小心!这不是一个很好的通用SQL解决方案,这是一个技巧。我不确定它是否适用于每个特殊情况。

8tntrjer

8tntrjer7#

试试这个

SELECT SUM(cost) AS Expr1
     , ID
     , date
 FROM (SELECT DISTINCT date
            , ID
            , cost
         FROM table_name AS table_name_1
        GROUP BY date, ID, cost) AS derivedtbl_1
 GROUP BY ID, date

或此

SELECT     SUM(cost) AS Expr1,  date
  FROM ( SELECT DISTINCT date, ID, cost
           FROM table_name AS table_name_1
           GROUP BY date, ID, cost) AS derivedtbl_1
 GROUP BY date
5q4ezhmt

5q4ezhmt8#

试试这个

我正在使用Mysql进行此. DEMO TABLE(表名为“tablename”. id,成本类型为“int”)

id | cost  
 1 | 15  
 1 | 25  
 1 | 10  
 2 | 25  
 2 | 5  
 2 | 10  
 3 | 20    
 3 | 15    
 3 | 10    
 4 | 20    
 4 | 20

查询使用

select id
     , SUM(cost) as 'Total Cost' 
  from sumchk 
 group by id

或(使用下面的一个)

select id, sum(cost) as 'Total Cost'  
  from (select * 
          from tablename
  ) a  
group by id;

结果:

id  |  Total Cost  
   1  |    50  
   2  |    40  
   3  |    45  
   4  |    40

相关问题