用于更新百分比变化的 SQL 语句

发布于 2024-09-29 10:19:46 字数 779 浏览 2 评论 0原文

我已经搜索了这个答案,并且已经接近答案,但仍然不够接近。我很想知道 MySQL 是否有这个功能。

我已经使用 Perl 和 MySQL 4 进行开发,现在使用 MySQL 4。 我的表看起来像这样...

  • symbol varchar(25)
  • TodayDate
  • dateinterest int(11)

我的问题是......这些符号(大约 200,000 个)每天都会更新为兴趣字段的新数字。

一个例子是这样......

symbol  | todayDate  | interest
-------------------------------
A202015 | 2010-10-26 | 150
A202015 | 2010-10-25 | 100

理想情况下,我能够做的是在最后更新另一个字段,并与前一个记录相比发生百分比变化。上面的内容看起来像这样......

symbol  | todayDate  | interest | change
-----------------------------------------
A202015 | 2010-10-26 | 150      | 50
A202015 | 2010-10-25 | 100

我不认为这个功能在 MySQL 中是可能的。我得出的结论是,我只需要获取以前的记录信息,进行数学计算,然后用百分比信息更新最新记录。我只是想再仔细检查一下,看看是否有 MySQL 天才有智慧来通过我的道路。

I have searched S.O. for this answer and have came close to an answer but still not close enough. I am interested in knowing if MySQL has this capability.

I have developed in Perl and MySQL 4 and I'm on MySQL 4 now.
My table looks like this...

  • symbol varchar(25)
  • todayDate date
  • interest int(11)

My problem is this.....these symbols (about 200,000 of them) update everyday with a new number for the interest field.

An example would be this....

symbol  | todayDate  | interest
-------------------------------
A202015 | 2010-10-26 | 150
A202015 | 2010-10-25 | 100

Ideally what I would be able to do would be to update another field at the end with a percentage change from the previous record. The above would then look like this....

symbol  | todayDate  | interest | change
-----------------------------------------
A202015 | 2010-10-26 | 150      | 50
A202015 | 2010-10-25 | 100

I didn't think that this functionality was possible in MySQL. I have come to the conclusion that I just need to grab the previous record info, do the math and then update the latest record with the percentage info. I just thought I would double check and see if any MySQL geniuses had any wisdom to pass my way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场信仰旅途 2024-10-06 10:19:46

在与 Wilkie 女士通过电子邮件交谈后,结果发现她想要这样的百分比变化:

update t_test_1 as t1 
    set chng = (t1.interest - (
            select interest from (
                select *
                from t_test_1 as t11 
                ) as x
            where x.symbol = t1.symbol and x.todayDate < t1.todayDate 
            order by x.todayDate desc
            limit 1
            )) / 
            (
                select interest from (
                    select *
                    from t_test_1 as t11 
                ) as x2
                where x2.symbol = t1.symbol and x2.todayDate < t1.todayDate 
                order by x2.todayDate desc
                limit 1
            ) * 100 ;

After an email conversation with Ms. Wilkie, it turns out she wanted a percent change like this:

update t_test_1 as t1 
    set chng = (t1.interest - (
            select interest from (
                select *
                from t_test_1 as t11 
                ) as x
            where x.symbol = t1.symbol and x.todayDate < t1.todayDate 
            order by x.todayDate desc
            limit 1
            )) / 
            (
                select interest from (
                    select *
                    from t_test_1 as t11 
                ) as x2
                where x2.symbol = t1.symbol and x2.todayDate < t1.todayDate 
                order by x2.todayDate desc
                limit 1
            ) * 100 ;
夜灵血窟げ 2024-10-06 10:19:46

由于 MySQL 引用子查询的方式,这有点奇怪,但这将满足我的需求:

/*

create table t_test_1 (
    symbol varchar(20) not null,
    todayDate datetime not null,
    interest int not null,
    chng int null
)

*/

insert into t_test_1 (symbol, todayDate, interest, chng) values ( 'A202015', '2010-10-09', 90, null);
insert into t_test_1 (symbol, todayDate, interest, chng) values ( 'A202015', '2010-10-10', 80, null);
insert into t_test_1 (symbol, todayDate, interest, chng) values ( 'A202015', '2010-10-11', 120, null);


update t_test_1 as t1 
    set chng = t1.interest - (select interest from (
        select *
        from t_test_1 as t11 
        ) as x 
        where x.symbol = t1.symbol and x.todayDate < t1.todayDate 
        order by x.todayDate desc
        limit 1
        );


select * from t_test_1;

这会导致:

A202015 2010-10-09 90   NULL
A202015 2010-10-10 80   -10
A202015 2010-10-11 120  40

哦,我应该补充一下,这是针对 mysql 5.x 数据库服务器的。我不确定它是否适用于 4.x,因为我没有 4.x 服务器可供测试,抱歉。

-大学教师

Its a little weird because of the way MySQL references sub-queries, but this will do what you need i think:

/*

create table t_test_1 (
    symbol varchar(20) not null,
    todayDate datetime not null,
    interest int not null,
    chng int null
)

*/

insert into t_test_1 (symbol, todayDate, interest, chng) values ( 'A202015', '2010-10-09', 90, null);
insert into t_test_1 (symbol, todayDate, interest, chng) values ( 'A202015', '2010-10-10', 80, null);
insert into t_test_1 (symbol, todayDate, interest, chng) values ( 'A202015', '2010-10-11', 120, null);


update t_test_1 as t1 
    set chng = t1.interest - (select interest from (
        select *
        from t_test_1 as t11 
        ) as x 
        where x.symbol = t1.symbol and x.todayDate < t1.todayDate 
        order by x.todayDate desc
        limit 1
        );


select * from t_test_1;

this results in:

A202015 2010-10-09 90   NULL
A202015 2010-10-10 80   -10
A202015 2010-10-11 120  40

oh, i should add, this is against a mysql 5.x database server. i'm not sure if it will work against 4.x as i don't have a 4.x server to test with, sorry.

-don

爱格式化 2024-10-06 10:19:46

从示例数据中,我假设记录没有“更新”,而是正在插入新记录。

INSERT INTO `rates` (`symbol`,`todayDate`,`interest`,`change`) 
    SELECT symbol,CURDATE(),$interest,$interest - `interest` 
    FROM `rates` 
    WHERE `symbol`='$symbol' AND `todayDate` = CURDATE() - INTERVAL 1 DAY

($interest 和 $symbol 是包含您要插入的值的变量,rates 是表的名称 - 替换为实际值)

From the sample data I assume the records are not "updating" but rather new records are being inserted.

INSERT INTO `rates` (`symbol`,`todayDate`,`interest`,`change`) 
    SELECT symbol,CURDATE(),$interest,$interest - `interest` 
    FROM `rates` 
    WHERE `symbol`='$symbol' AND `todayDate` = CURDATE() - INTERVAL 1 DAY

($interest and $symbol are variables containing the values you are inserting, rates is the name of the table - replace with the actual values)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文