MYSQL - 仅计算特定列中变量的出现次数

时间:2016-12-21 14:01:49

标签: mysql sql

我搜索了这个答案但却找不到任何答案。希望得到一点帮助。

  

需要查询:每列中每个数字出现的次数。

tableName =" Schedule"

+-----+-------+-----------+-------+
| key | prime | secondary | third |
+-----+-------+-----------+-------+
|   1 |     6 |         6 |     7 |
|   2 |     7 |         9 |     9 |
|   3 |     6 |         9 |     6 |
|   4 |     9 |         9 |     9 |
+-----+-------+-----------+-------+

我希望我的结果是每列中每次出现的计数......

+--------+------ +-----------+-------+
| number | prime | secondary | third |
+--------+-------+-----------+-------+
|     6  |     2 |         1 |     1 | 
|     7  |     1 |         0 |     1 |
|     9  |     1 |         3 |     2 | 
+-------+-----------+--------+-------+

所以#6在" prime"中出现2次。列,在" secondary"中出现1x列在"第三个"中出现1x列。

2 个答案:

答案 0 :(得分:1)

使用import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.routepolicy.quartz.CronScheduledRoutePolicy; public class App1 { public static void main(final String[] arguments) { final CamelContext camelContext = new DefaultCamelContext(); try { camelContext.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { CronScheduledRoutePolicy startPolicy = new CronScheduledRoutePolicy(); startPolicy.setRouteStartTime("0 0/5 * 1/1 * ? *"); from("file:E:\\TestingWatch1\\input") .routeId("testRoute").routePolicy(startPolicy) .to("file:E:\\TestingWatch1\\output"); } }); camelContext.start(); Thread.sleep(10000); //camelContext.stop(); } catch (Exception camelException) { } } } 从三列中获取所有不同的数字。 union这些数字的每列的left join以获得最终结果。

count

答案 1 :(得分:0)

select nall.id,
    (select count(*) from Schedule where prime = nall.id) as prime,
    (select count(*) from Schedule where secondary = nall.id) as secondary,
    (select count(*) from Schedule where third = nall.id) as third
from
    (select prime as id from Schedule 
    union
    select secondary as id from Schedule 
    union
    select third as id from Schedule ) nall;