【问题标题】:Oracle combining two monthly sums from to different tablesOracle 将两个月的总和合并到不同的表中
【发布时间】:2014-03-28 23:21:36
【问题描述】:

我已经坚持了好多年了。这可能是显而易见的,但老实说,我没有那么多 SQL 知识。我得到了以下两个表:

Date1       Cost1
-------------------
01-01-14    50,65
02-01-14    12,12

Date2       Cost2
-------------------
01-01-14    123,12
02-01-14    14,15    

我需要以下输出:

Number of month    Total amount of table 1 and 2 for that specific month
-------------------------------------------------------------------------
1                  2425252,52
2                  53252,89

我得到了适用于单个表的查询,我可以在我的实际代码中对它们进行排序,但我只知道查询是可能的,但我根本不知道怎么做。我一直在研究内连接和外连接,但由于我使用了两个 group by 语句,我得到了每个月数乘以 12 且值不正确的结果。

非常感谢您。

【问题讨论】:

  • 一月份的50,65123,12 如何变成2425252,52
  • 抱歉,这些只是随机测试编号。每个月有大约 600 个值,这导致了相当高的总数

标签: sql oracle sum multiple-tables


【解决方案1】:

您可以将结果合并在一起,然后对这些结果求和。请记住,您正在根据 OP 跨越数年。如果这不是本意,那么我还提供了按年和月分组的替代方案。

按月份分组:

SELECT c1.monthNum
    , sum(c1.cost) as cost
FROM 
(
    SELECT to_char(t1.date1, 'MM') as monthNum, SUM(t1.cost1)  as cost
    FROM table1 t1
    WHERE ..your table1 where clause here...
    GROUP BY to_char(t1.date1, 'MM') 

    UNION ALL

    SELECT to_char(t2.date1, 'MM') as monthNum, SUM(t2.cost1)  as cost
    FROM table1 t2
    WHERE ..your table2 where clause here...    
    GROUP BY to_char(t2.date1, 'MM')
) c1
GROUP BY c1.monthNum

或按年份分组:

SELECT c1.yearNum
    , c1.monthNum
    , sum(c1.cost) as cost
FROM 
(
    SELECT to_char(t1.date1, 'YYYY') AS yearNum, to_char(t1.date1, 'MM') as monthNum, SUM(t1.cost1)  as cost
    FROM table1 t1
    WHERE ..your table1 where clause here...
    GROUP BY to_char(t1.date1, 'YYYY'), to_char(t1.date1, 'MM') 

    UNION ALL

    SELECT to_char(t2.date1, 'YYYY') AS yearNum, to_char(t2.date1, 'MM') as monthNum, SUM(t2.cost1)  as cost
    FROM table1 t2
    WHERE ..your table2 where clause here...    
    GROUP BY to_char(t2.date1, 'YYYY'), to_char(t2.date1, 'MM')
) c1
GROUP BY c1.yearNum, c1.monthNum

【讨论】:

  • 就是这样!非常感谢!
【解决方案2】:

此代码可用于按天分组。 我插入了日期为29-APR-15 and 30-APR-15的数据

select 
     extract(month from tbl_inc.date_id) as mon,
     extract(year from tbl_inc.date_id) as yr ,
     ta as incamt ,
     da as expamt ,
     tbl_inc.*,
     tbl_expn.*
from 
    (select trunc(date_id) date_id, user_id, sum(amount) ta 
from 
    tbl_inc 
group by 
    trunc(date_id), user_id ) tbl_inc,
    ( select trunc(date_id) date_id, user_id, sum(amount) da 
from 
    tbl_expn 
group by 
    trunc(date_id), user_id ) tbl_expn 
where 
     tbl_inc.user_id = tbl_expn.user_id 
and  
     tbl_inc.date_id = tbl_expn.date_id 
and  
     tbl_inc.user_id='222' 
and  
     extract(year from tbl_inc.date_id)='2015' 
order by 
     extract(month from tbl_inc.date_id)

本次查询结果如下

MON YR      INCAMT  EXPAMT  DATE_ID    USER_ID  TA      DATE_ID     USER_ID DA
4   2015    9155    2600    30-APR-15   222     9155    30-APR-15   222     2600
4   2015    45      435     29-APR-15   222     45      29-APR-15   222     435

这里有人帮我从查询中获得以下输出

MON YR      INCAMT  EXPAMT  USER_ID
4   2015    9200    3035    222     

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多