编写一个SQL查询,按渠道和上个月的收入生成每月收入

编写一个SQL查询,按渠道和上个月的收入生成每月收入,sql,database,pgadmin,revenue,Sql,Database,Pgadmin,Revenue,大家好,我有两个输出如下的表: 月表 事务表 我需要按渠道计算每月收入和上个月的收入:我进行了此查询,但尚未完成 Select date_created, channel, sum(revenue) as monthly_revenue from transaction_table GROUP BY date_created,channel 结果应显示每月收入和上个月的当月收入 我该怎么做呢?您可以尝试在表之间使用联接 Select a.month_index, a.year_mon

大家好,我有两个输出如下的表:

月表

事务表

我需要按渠道计算每月收入和上个月的收入:我进行了此查询,但尚未完成

Select date_created, channel, sum(revenue) as monthly_revenue 
from transaction_table  
GROUP BY  date_created,channel
结果应显示每月收入和上个月的当月收入


我该怎么做呢?

您可以尝试在表之间使用联接

Select a.month_index, a.year_month, b.channel, sum(b.revenue) as monthly_revenue 
from Month_Table a 
from transaction_table  b ON b.date_created between a.month_start_date and a.month_and_date
    amd month(b.date_created) = betwwen month(curdate()) -1 and month(curdate())
GROUP BY  a.month_index, a.year_month, b.channel
order by a.year_month desc

您可以尝试在表之间使用联接

Select a.month_index, a.year_month, b.channel, sum(b.revenue) as monthly_revenue 
from Month_Table a 
from transaction_table  b ON b.date_created between a.month_start_date and a.month_and_date
    amd month(b.date_created) = betwwen month(curdate()) -1 and month(curdate())
GROUP BY  a.month_index, a.year_month, b.channel
order by a.year_month desc
试试这个:

Select t1.date_created, t1.channel, sum(t1.revenue) as monthly_revenue ,sum(t2.revenue) prev_month_revenue
from transaction_table t1 left join transaction_table t2  on t1.channel = t2.channel and to_char(t1.date_created,'MM') = to_char(add_months(t2.date_created,-1),'MM')
GROUP BY  t1.date_created,t1.channel;
试试这个:

Select t1.date_created, t1.channel, sum(t1.revenue) as monthly_revenue ,sum(t2.revenue) prev_month_revenue
from transaction_table t1 left join transaction_table t2  on t1.channel = t2.channel and to_char(t1.date_created,'MM') = to_char(add_months(t2.date_created,-1),'MM')
GROUP BY  t1.date_created,t1.channel;
试试这个代码

with resultTable as(
select RT.channel,RT.sumRevenue,LT.[month-start_date],LT.month_end_date,LT.year_month
from (select t.channel,sum(revenue) as sumRevenue,M.month_index from Month_Table M,Transaction_Table T
where t.date_created BETWEEN m.[month-start_date] AND m.month_end_date
group by m.month_index,t.channel) RT Join Month_Table LT on RT.month_index = LT.month_index
)
select * from resultTable
输出:

或者使用此查询

with resultTable as(
select RT.channel,RT.sumRevenue,LT.[month-start_date],LT.month_end_date,LT.year_month
from (select t.channel,sum(revenue) as sumRevenue,M.month_index from Month_Table M,Transaction_Table T
where t.date_created BETWEEN m.[month-start_date] AND m.month_end_date
group by m.month_index,t.channel) RT Join Month_Table LT on RT.month_index = LT.month_index
)
select *,LAG(sumRevenue,1) OVER (PARTITION BY channel ORDER BY channel) previous_month_sales from resultTable
输出:

试试这个代码

with resultTable as(
select RT.channel,RT.sumRevenue,LT.[month-start_date],LT.month_end_date,LT.year_month
from (select t.channel,sum(revenue) as sumRevenue,M.month_index from Month_Table M,Transaction_Table T
where t.date_created BETWEEN m.[month-start_date] AND m.month_end_date
group by m.month_index,t.channel) RT Join Month_Table LT on RT.month_index = LT.month_index
)
select * from resultTable
输出:

或者使用此查询

with resultTable as(
select RT.channel,RT.sumRevenue,LT.[month-start_date],LT.month_end_date,LT.year_month
from (select t.channel,sum(revenue) as sumRevenue,M.month_index from Month_Table M,Transaction_Table T
where t.date_created BETWEEN m.[month-start_date] AND m.month_end_date
group by m.month_index,t.channel) RT Join Month_Table LT on RT.month_index = LT.month_index
)
select *,LAG(sumRevenue,1) OVER (PARTITION BY channel ORDER BY channel) previous_month_sales from resultTable
输出:



嗨,你的答案只是一个密码?请提供更多的解释和细节。你也可以阅读并给出高质量的答案。谢谢你,瑞沙!嗨,你的答案只是一个密码?请提供更多的解释和细节。你也可以阅读并给出高质量的答案。谢谢你,瑞沙!我做了一些类似的更改,它帮助我正确地获得了月收入:选择M.month_index、M.year_month、T.channel、sumt.revenue作为月收入表M left join transaction_table T on T.date在M.month_start_date和M.month_end_date之间创建的M.month_index、M.year_month_month,T.channel Order by M.year\u month desc您真是个了不起的人谢谢:我现在有个问题,如何编辑该查询以添加上个月的收入?另一个问题:有两个月没有任何收入,所以它给了我空值,我如何排除它们?我将感谢您的帮助,因为我仍在寻求改进我的回答,不容易理解您的要求。。您应该更新您的问题,并根据提供的示例添加预期结果。我做了一些类似的更改,这有助于我正确获得每月收入:选择M.month\u index、M.year\u month、T.channel、,sumt.revenueas月收入表M left join交易表T on T.date在M.month\u开始日期和M.month\u结束日期组之间创建,按M.month\u索引,M.year\u month,T.channel订单按M.year\u month描述你真了不起谢谢:我现在有一个问题,如何编辑该查询以添加上个月的收入?另一个问题:有两个月没有任何收入,所以它给了我空值,我如何排除它们?我将感谢您的帮助,因为我仍在寻求改进我的回答,不容易理解您的要求。。您应该更新您的问题,并根据提供的示例添加预期结果当计算每个月的总和时,上个月的帐户需要什么?这是我考试中的一个问题,我也不明白为什么要显示上个月的收入0.0OK。我意识到。这是一个测试问题是的,这是一个测试问题:你认为我应该在查询中添加什么?你的答案是完美的!干得好Thx因为你的问题问的是上个月的收入,如果你能加上它,你的答案就会完整。当计算每个月的总和时,上个月的账户需要什么?这是我考试中的一个问题,我也不明白为什么要显示上个月的收入0.0OK。我意识到。这是一个测试问题是的,这是一个测试问题:你认为我应该在查询中添加什么?你的答案是完美的!很好,因为你的问题是上个月的问题,如果你能添加它,你的答案将是完整的。也非常有用,谢谢你为我提供了另一个解决方案->我做了以下更改,它工作得非常好:使用resultable作为select RT.channel,RT.sumRevenue,LT.month\u start\u date,LT.month\u end\u date,LT.year\u month from select t.channel、sumrevenue as sumrevenue、M.month\u index from month\u table M、transaction\u table t,其中t.date\u在M.month\u start\u date和M.month\u end\u date组之间创建,由M.month\u index表示,t.channel RT RT Join month\u table LT on RT.month\u index=LT.month\u index select*from resultable!!我非常感谢您的贡献不客气。干得好,SQL hero也很有帮助,感谢您为我提供了另一个解决方案->我做了以下更改,效果非常好:使用Resultable作为select RT.channel,RT.sumRevenue,LT.month\u start\u date,LT.month\u end\u date,LT.year\u month from select t.channel,sumrevenue作为sumrevenue,来自month_表M的M.month_索引,事务_表T,其中T.date_通过M.month_索引在M.month_开始日期和M.month_结束日期组之间创建,T.channel RT Join month_表LT on RT.month_索引=LT.month_索引从Resultable中选择*!!我真的很感谢你的贡献不客气干得好,SQL英雄