简体   繁体   English

如何简化这个查询?

[英]How to simplfy this query?

Query 1: 查询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 查询2:修改了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. 我是初学者,截至目前我每次都通过编辑查询1来运行查询,我知道这两个查询都可以合并为一个查询。

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. ...除非我错过了除结算周期之外的两个查询之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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