使用分组自定义查询然后计算

时间:2018-05-25 13:41:46

标签: mysql

我正在使用events.I想知道如何计算事件中的sum或使用单个查询 http://sqlfiddle.com/#!9/ad6d1c/1

问题的DDL:

CREATE TABLE `table1` (
  `id` int(11) NOT NULL,
  `group_id` int(11) NOT NULL DEFAULT '0',
  `in_use` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0->in_use,1->not_in_use',
  `auto_assign` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0->Yes,1->No'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `table1`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `table1`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
INSERT INTO `table1` (`id`, `group_id`, `in_use`, `auto_assign`) VALUES
(1, 3, 1, 0),(2, 2, 0,1),(3, 1, 1, 1),(4, 3, 1, 0),(5, 3, 0, 0),(6, 3, 0, 1),
(7, 3, 1, 0),(8, 3, 0, 1),(9, 3, 0, 1),(10, 3, 0, 1),(11, 3, 0, 1),(12, 3, 1, 1),
(13, 3, 1, 0),(14, 3, 0, 0),(15, 3, 0, 0),(16, 3, 0, 0),(17, 3, 0, 0),(18, 3, 1, 1),
(19, 3, 0, 0),(20, 3, 0, 0)

预期产出:

| count | in_use | auto_assign |  sum | check_count |
|-------|--------|-------------|------|------------ |
|     7 |      0 |           0 |  11  |     5       |
|     5 |      0 |           1 |  07  |     3       |
|     4 |      1 |           0 |  11  |     5       |
|     2 |      1 |           1 |  07  |     3       |

我们可以看到auto_assign=0总计11次(7+4)和。{ auto_assign=17次(5+2)此计数应存入新列sum

check_count列是sum列的百分比值.Percentage将被预定义。

让我们拿50%,所以计数11(总和列值) - > 50%= 5.5 = ROUND(5.5)== 5(整数)。相同方式计数7(总和列值) - > 50%= 3.5 = ROUND(3.5)= 3(整数)

这里5 > 4(auto_assign=0 and in_use=1 )。所以必须将记录插入另一个表(table2)。如果没有那么。

同样,如果3> 2,那么还需要将记录插入到另一个表(table2)中。如果不是,则不需要。

注意:这个逻辑我想在event

中实现

这有点复杂,但请在活动中建议我如何做到这一点。

详细说明:

此处5的percentage_Value为auto_assign =0auto_assign=0 and in_use=1 count 4less than 5 auto_assign=0 and in_use=1,然后必须将记录插入表中2.

假设,如果table2计数为6,则无需将记录插入3

同样,

此处auto_assign =1的percentage_Value为auto_assign=1 and in_use=1count 2 less than 3auto_assign=1 and in_use=1 table2,然后必须将记录插入表中2.

假设,如果Insert into table2(cli_group_id,auto_assign,percentage_value,result_value) values(3,0,5,4) 计数为4,则无需将记录插入GradientDescentOptimizer

将查询插入table2:

import tensorflow as tf

# Guess 2.5 as a starting point
x = tf.Variable(2.5, name='x', dtype=tf.float32)
log_x_squared = tf.square(tf.log(x))

optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(log_x_squared)

init = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(init)
    print("start ", "x:", session.run(x), "log(x)^2:", session.run(log_x_squared))
    for step in range(10):  
      session.run(train)
      print("step", step, "x:", session.run(x), "log(x)^2:", session.run(log_x_squared))

1 个答案:

答案 0 :(得分:0)

DEMO Fiddle

解决问题:我们需要通过auto_Assigns计算记录数;所以我们使用该值生成派生表(B)并在auto_Assign上连接回基表。然后,这为我们提供了一些我们需要的列,我们使用truncate函数和除法模型来获取check_count

SELECT count(*), in_use, A.Auto_Assign, B.SumC, truncate(B.SumC/2,0) as check_Count
  FROM table1 A
  INNER JOIN (Select Auto_Assign, count(*) sumC 
              from table1 
              where Group_ID = 3
              Group by Auto_Assign) B
   on A.Auto_Assign = B.Auto_Assign
  WHERE GROUP_ID = 3
  Group by in_use, A.Auto_Assign

我们可以通过加入来消除double where子句:

SELECT count(*), in_use, A.Auto_Assign, B.SumC, truncate(B.SumC/2,0) as check_Count
  FROM table1 A
  INNER JOIN (Select Auto_Assign, count(*) sumC, Group_ID
              from table1 
              where Group_ID = 3
              Group by Auto_Assign, Group_ID) B
   on A.Auto_Assign = B.Auto_Assign
   and A.Group_ID = B.Group_ID
  Group by in_use, A.Auto_Assign

我需要澄清问题的其余部分:我不确定5> 4你看着我看到的除了支票计数以外没有3,但是那不是"同样的方式"所以我不确定你要追求的是什么。

这里5> 4(auto_assign = 0和in_use = 1)。因此必须将记录插入另一个表(table2)。如果没有那么。

同样,如果3> 2,那么还需要将记录插入另一个表(table2)。如果不是那么。

注意:这个逻辑我想在事件

中实现

这有点复杂,但请在活动中建议我如何做到这一点。

所以要创建活动:DOCS

结果是:

CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 6 Minutes
DO
  INSERT INTO table2 
  SELECT count(*) as mCount
       , in_use
       , A.Auto_Assign
       , B.SumC, truncate(B.SumC/2,0) as check_Count
  FROM table1 A
  INNER JOIN (SELECT Auto_Assign, count(*) sumC, Group_ID
              FROM table1 
              WHERE Group_ID = 3
              GROUP BY Auto_Assign, Group_ID) B
     ON A.Auto_Assign = B.Auto_Assign
    AND A.Group_ID = B.Group_ID
  GROUP BY  in_use, A.Auto_Assign