如何在SQL中合并相同日期的数据?

huangapple go评论141阅读模式
英文:

how to combine data on same date in sql?

问题

如何在SQL中合并相同日期的数据? 我的数据库表

我的预期结果类似于这样 我期望的结果

英文:

how to combine data on same date in sql? my database table

my expected result is something like this the result I expected

答案1

得分: 0

你可以在日期字段上使用"GROUP BY"语句:

create table SOQ4
(
    ops_date date,
    well_profit int, 
    well_loss int
);

insert into SOQ4 values
('2018-02-28', 0, 163),
('2018-02-28', 453, 0),
('2018-02-28', 0, 0),
('2018-03-31', 0, 228),
('2018-03-31', 469, 0),
('2018-03-31', 0, 0),
('2018-04-30', 0, 226),
('2018-04-30', 424, 0),
('2018-04-30', 0, 0);

SELECT 
    ops_date, 
    SUM(well_profit) AS well_profit,
    SUM(well_loss) AS well_loss 
FROM
    SOQ4 
GROUP BY 
    ops_date;

输出:如何在SQL中合并相同日期的数据?

英文:

You can use group by statement on the date

create table SOQ4
(
    ops_date date,
    well_profit int, 
    well_loss int
);

insert into SOQ4 values
('2018-02-28',0,163),
('2018-02-28',453,0),
('2018-02-28',0,0),
('2018-03-31',0,228),
('2018-03-31',469,0),
('2018-03-31',0,0),
('2018-04-30',0,226),
('2018-04-30',424,0),
('2018-04-30',0,0);

SELECT 
    ops_date, 
    SUM(well_profit) AS well_profit,
    SUM(well_loss) AS well_profit 
FROM
    SOQ4 
GROUP BY 
    ops_date;

Output:

如何在SQL中合并相同日期的数据?

huangapple
  • 本文由 发表于 2023年4月11日 01:28:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定