-1

我在雪花中有这段代码来获取下表:

CREATE OR REPLACE TABLE LIVE_ANALYTICS.REPORTING."GL_REPORT"
AS
SELECT
    SUM("TABLE_GL_ENTRY"."Amount" AS "AMOUNT",
    MONTH("TABLE_GL_ENTRY"."Posting Date") AS "MONTH",
    YEAR("TABLE_GL_ENTRY"."Posting Date") AS "YEAR",
    "TABLE_GL_ENTRY"."Global Dimension 1 Code" AS "ID_STORE",
    CASE
        WHEN "YEAR" = YEAR(CURRENT_DATE()) THEN 'AMOUNT_CURRENT_YEAR'
        ELSE 'AMOUNT_LAST_YEAR'
    END AS "METRIC"
FROM
    LIVE_ANALYTICS.NAVISION."G_L Entry" AS "TABLE_GL_ENTRY"
WHERE
    (("MONTH" = MONTH(CURRENT_DATE()) OR "MONTH" = MONTH(ADD_MONTHS(CURRENT_DATE, 1)))
    AND
    ("YEAR" = YEAR(CURRENT_DATE()) OR "YEAR" = YEAR(ADD_MONTHS(CURRENT_DATE, -12))))
    AND
    (("ID_STORE" LIKE '1')
        OR
    ("ID_STORE" LIKE '2')
        OR
    ("ID_STORE" LIKE '3'))    
GROUP BY
    MONTH",
    "YEAR",
    "ID_STORE",
;

在此处输入图像描述

仅当 "YEAR" = 2021 时,我才需要从该表中减去 "MONTH" 和 "ID_STORE" 的 "AMOUNT" 。

AMOUNT_CURRENT_YEAR - AMOUNT_LAST_YEAR

在此处输入图像描述

最后,我想将这些新结果与其他现有记录一起插入到现有表中。

在此处输入图像描述

我怎样才能做到这一点?有什么建议吗?

先感谢您

亲切的问候。

编辑:这是我前几天搜索的解决方案,谢谢大家!

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=9909600b9ac804ba67b7086c40e0c844

create table last_amount as 
select sum(amount) as 'amount_last', month, year, id, metric 
from data where metric like 'last' 
group by month, year, id, metric 
order by id, month, year;

create table current_amount as 
select sum(amount) as 'amount_current', month, year, id, metric 
from data where metric like 'current' 
group by month, year, id, metric 
order by id, month, year

create table subtract as
select
c.amount_current - l.amount_last as amount, c.month, c.year, c.id
from
last_amount l
join
current_amount c
on
l.id = c.id
and
l.month = c.month;

alter table subtract add metric varchar(255) default 'subtract';

insert into data
select * from subtract;

drop table last_amount;
drop table current_amount;

select * from data order by id, month, year;
4

6 回答 6

0

在这里,这可能会对您有所帮助,此代码可能不包含所有值,但它会让您了解逻辑。

使用链接作为示例:

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=7ad1e26d11b1c835fc77e9c4578bebf5

  • 第一:选择所有数据
  • 第二:引入lead ()将最新的第1行向后移动的功能
  • 第三:根据您的逻辑进行减法
  • 第四:加入原始和新的结果使用UNION
with all_data as (

select * from data

),

last_year_current_year as (

select 

*,
lead(amount) over(partition by id, month, year) as one_less

from all_data

)
,

subtraction as  (

select 

*,

one_less - amount as new_amount

from last_year_current_year



),

combining_data as (

select new_amount, month, year, id from subtraction

union

select amount, month, year, id from subtraction

)

select * from combining_data
于 2021-08-16T17:23:11.393 回答
0

您可以使用条件聚合和insert

insert into LIVE_ANALYTICS.NAVISION."G_L Entry" (amount, month, year, id_store, metric)
    select sum(case when metric = 'AMOUNT_CURRENT_YEAR' then amount
                    when metric = 'AMOUNT_LAST_YEAR' then - amount
               end),   
           month, year, id_store, 'SUBTRACT_LAST_YEAR'
    from LIVE_ANALYTICS.NAVISION."G_L Entry" gl
    where year = 2021
    group by month, year, id_store;
于 2021-08-16T17:59:18.693 回答
0

-- 这是在 SQL Server 中,但我希望 MySql 中的逻辑也相同,创建表 #temp1(金额,MonthNum int,YearNum int,ID_Store int,Metric varchar(100));

插入#temp1 值(10,8,2020,1,'AMOUNT LAST YEAR') 插入#temp1 值(20,8,2021,1,'AMOUNT CURRENT YEAR') 插入#temp1 值(30,8, 2020,2,'AMOUNT LAST YEAR') 插入#temp1 值(40,8,2021,2,'AMOUNT CURRENT YEAR') 插入#temp1 值(50,8,2020,3,'AMOUNT LAST YEAR')插入#temp1 值(60,8,2021,3,'AMOUNT CURRENT YEAR') 插入#temp1 值(70,9,2020,1,'AMOUNT__YEAR') 插入#temp1 值(0,9,2021, 1,'AMOUNT CURRENT YEAR') 插入#temp1 值(90,9,2020,2,'AMOUNT__YEAR') 插入#temp1 值(0,9,2021,2,'AMOUNT CURRENT YEAR') 插入#temp1值(110,9,2020,3,'AMOUNT__YEAR')插入到 #temp1 值(0,9,2021,3,'AMOUNT CURRENT YEAR')

-- 开始实际代码从这里开始 Select (t1.Amount-t3.Amount) as Amount ,t1.MonthNum ,t1.YearNum ,t1.ID_Store ,'Modified Metric' as Metric into #temp2 from #temp1 t1 join (Select t2.Amount,t2.MonthNum,t2.ID_Store from #temp1 t2 where t2.YearNum=2020) t3 ON t1.MonthNum=t3.MonthNum and t1.ID_Store=t3.ID_Store where t1.YearNum=2021

插入 #temp1 选择 * from #temp2

-- 结束实际代码从这里开始

从#temp1中选择*;

删除表#temp1;删除表#temp2;

于 2021-08-17T07:18:45.970 回答
0

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=9909600b9ac804ba67b7086c40e0c844

create table data ( amount int, month  int, year int,  id int, metric varchar(255) );


insert into data values (10, 8, 2020, 1, 'last');
insert into data values (20, 8, 2021, 1, 'current');
insert into data values (30, 8, 2020, 2, 'last');
insert into data values (60, 8, 2021, 3, 'current');
insert into data values (70, 9, 2020, 1, 'last');
insert into data values (0,  9, 2021, 1, 'current');
insert into data values (90, 9, 2020, 2, 'last');
insert into data values (40, 8, 2021, 2, 'current');
insert into data values (50, 8, 2020, 3, 'last');
insert into data values (0,  9, 2021, 2, 'current');
insert into data values (110, 9, 2020, 3, 'last');
insert into data values (0,  9, 2021, 3, 'current');

select * from data order by id, month, year

create table last_amount as 
select sum(amount) as 'amount_last', month, year, id, metric 
from data where metric like 'last' 
group by month, year, id, metric 
order by id, month, year;

create table current_amount as 
select sum(amount) as 'amount_current', month, year, id, metric 
from data where metric like 'current' 
group by month, year, id, metric 
order by id, month, year

create table subtract as
select
c.amount_current - l.amount_last as amount, c.month, c.year, c.id
from
last_amount l
join
current_amount c
on
l.id = c.id
and
l.month = c.month;

alter table subtract add metric varchar(255) default 'subtract';

insert into data
select * from subtract;

drop table last_amount;
drop table current_amount;

select * from data order by id, month, year;
于 2021-08-18T08:30:58.977 回答
0

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=6a50ae3dce1eda02250d79fbb6f95ca7

create table data ( amount int, month  int, year int,  id int, metric varchar(255) );


insert into data values (10, 8, 2020, 1, 'last');
insert into data values (20, 8, 2021, 1, 'current');
insert into data values (30, 8, 2020, 2, 'last');
insert into data values (60, 8, 2021, 3, 'current');
insert into data values (70, 9, 2020, 1, 'last');
insert into data values (0,  9, 2021, 1, 'current');
insert into data values (90, 9, 2020, 2, 'last');
insert into data values (40, 8, 2021, 2, 'current');
insert into data values (50, 8, 2020, 3, 'last');
insert into data values (0,  9, 2021, 2, 'current');
insert into data values (110, 9, 2020, 3, 'last');
insert into data values (0,  9, 2021, 3, 'current');
select * from data order by id, month, year


select * from data
union all
select
c.amount - l.amount as amount, c.month, c.year, c.id, 'subtract' as metric
from
data c
right join
data l
on
l.id = c.id
and
l.month = c.month
where c.year = 2021
and c.amount - l.amount != 0
order by id, month, year;
于 2021-09-10T17:52:41.393 回答
0

更好的选择:

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4273a6e920b54e118868ca44a5927cce

create table data ( amount int, month  int, year int,  id int, metric 
varchar(255) );
insert into data values (10, 8, 2020, 1, 'last');
insert into data values (20, 8, 2021, 1, 'current');
insert into data values (30, 8, 2020, 2, 'last');
insert into data values (60, 8, 2021, 3, 'current');
insert into data values (70, 9, 2020, 1, 'last');
insert into data values (0,  9, 2021, 1, 'current');
insert into data values (90, 9, 2020, 2, 'last');
insert into data values (40, 8, 2021, 2, 'current');
insert into data values (50, 8, 2020, 3, 'last');
insert into data values (0,  9, 2021, 2, 'current');
insert into data values (110, 9, 2020, 3, 'last');
insert into data values (0,  9, 2021, 3, 'current');
select * from data order by id, month, year

select * from data
union all
select
c.amount - l.amount as amount, c.month, c.year, c.id, 'subtract' as metric
from
(select * from data where year = 2021) c
left join
(select * from data where year = 2020) l
on
l.id = c.id
and
l.month = c.month
order by id, month, year, metric;
于 2021-09-10T18:03:56.327 回答