SQL: UPDATE from a complex select

2019-03-05 03:58发布

问题:

In a phone system scenario i have 2 tables.

  • table1 is composed by: customer_id, call_duration, calldate, skip_billing .
  • table2 is composed by: customer_id, bonus_seconds.

table1 stores all the calls for all customers and table2 stores the bonus_seconds which represents free conversation time allowed for a defined customer(ie: for customer 1 the FIRST 40 cumulative seconds are free).

I have to write a query to update table1 according to the condition explained below: set skip_billing within calls which are defined free in table2.

So I first need to group by customer_id and then iterate over the calls, incrementing a cumulative variable(cumsec) over call_duration and set skip_billing accordingly.

table1 example is:

|sqlid |customer_id |billsec | skipbill|
|0     |1           |12      | 1       |<--need to set 1 due to cume=12 for customer_id=1
|1     |1           |10      | 1       |<--need to set 1 due to cume=22 for customer_id=1
|2     |1           |15      | 1       |<--need to set 1 due to cume=37 for customer_id=1
|3     |1           |8       | 0       |<--nop(no operation) due to cume=45
|4     |2           |12      | 1       |<--need to set 1 due to cume=12 for customer_id=2
|5     |3           |12      | 1       |<--need to set 1 due to cume=12 for customer_id=3
|6     |2           |12      | 0       |<--nop due to cume=24 for customer_id=2
|7     |1           |12      | 0       |<--nop due to cume=49 for customer_id=1
|8     |3           |15      | 0       |<--nop due to cumsec=27 for customer_id=3

|customer_id |bonus_seconds|
|1           |40           |
|2           |20           |
|3           |15           |

I tried with query like this (thanks to Gordon Linoff) which returns the right set of row:

    SELECT t.cume, t.calldate, t.customer_id FROM (SELECT t.*, (@cume := @cume + billsec) AS cume FROM table1 t CROSS JOIN (SELECT @cume := 0) vars ORDER BY calldate) t, table2 sct WHERE t.cume <= sct.bonus_seconds AND t.customer_id=sct.customer_id ;

But when i try to use withing the UPDATE like below command it does not work because does not match anything.

    UPDATE table1 SET skipbill=1 WHERE sqlid=(SELECT t.sqlid FROM (SELECT t.*, (@cume := @cume + billsec) AS cume FROM table1 t CROSS JOIN (SELECT @cume := 0) vars ORDER BY calldate) t, table2 sct WHERE t.cume <= sct.bonus_seconds AND t.customer_id=sct.customer_id ) ;

How I can write the update task using that query or something better ?

Thank you in advance

回答1:

UPDATE table1 
SET skipbill = 1 
WHERE sqlid IN (
    SELECT DISTINCT t.sqlid 
    FROM (
        SELECT t.*, (@cume := @cume + billsec) AS cume 
        FROM table1 t 
        CROSS JOIN (SELECT @cume := 0) vars 
        ORDER BY calldate
    ) t, table2 sct 
    WHERE t.cume <= sct.bonus_seconds 
          AND t.customer_id = sct.customer_id
);


回答2:

It seems to me that you're billing customer 1 (for instance) for 3 seconds that should be part of their bonus, but anyway...

 SELECT x.*
      , SUM(y.billsec) cumu
      , IF(SUM(y.billsec)<=z.bonus_seconds,1,0) n 
   FROM my_table x 
   JOIN my_table y 
     ON y.customer_id = x.customer_id 
    AND y.sqlid <= x.sqlid 
   LEFT 
   JOIN bonus z 
     ON z.customer_id = x.customer_id 
  GROUP 
     BY x.customer_id
      , x.sqlid 
  ORDER 
     BY sqlid;