简体   繁体   中英

How to simplfy this query?

Query 1:

SET @count = 0;
    UPDATE a_daily_copy_copy
    SET a_daily_copy_copy.Cummulative_Target = @count:= target + @count 
    where a_daily_copy_copy.Site_id = 1 
          and a_daily_copy_copy.Year=4 
          and a_daily_copy_copy.Billing_cycle=1
    ORDER BY date

Query 2: Modified the a_daily_copy_copy.Billing_cycle=2

 SET @count = 0;
    UPDATE a_daily_copy_copy
    SET a_daily_copy_copy.Cummulative_Target = @count:= target + @count
    where a_daily_copy_copy.Site_id = 1 
          and a_daily_copy_copy.Year=4 
          and a_daily_copy_copy.Billing_cycle=2
    ORDER BY date

I'm a beginner and as of now I'm running the query every time manually by editing the query 1 , and I know both queries can be consolidated into a single query.

I tried solving with Group by function but couldnt come up with Please help me.

Have screened the table: 在此输入图像描述

Looks to me that you can just do:

SET @count = 0;
    UPDATE a_daily_copy_copy
    SET a_daily_copy_copy.Cummulative_Target = @count:= target + @count where a_daily_copy_copy.Site_id = 1 and a_daily_copy_copy.Year=4 and a_daily_copy_copy.Billing_cycle IN (1, 2)
    ORDER BY date

...unless I've missed a difference between the two queries other than the billing cycle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM