MySQL如何实现这个查询?

时间:2012-03-21 20:02:10

标签: mysql subquery

我有以下Mock表:

+---------+-------------------+------+------+
| NAME    | TITLE             | SIZE |  Hit |
+---------+-------------------+------+------+
| A       | Hippo1            | H    |  0   |
| A       | Hippo2            | H    |  0   |
| A       | Hippo3            | H    |  1   |   
| A       | Hippo1            | M    |  0   |
| A       | Hippo2            | M    |  1   |
| A       | Hippo3            | M    |  1   |
| A       | Hippo1            | L    |  0   |
| A       | Hippo2            | L    |  1   |    
| A       | Hippo3            | L    |  1   |
+---------+-------------------+------+------+
| B       | Snail1            | H    |  1   |
| B       | Snail1            | M    |  0   |
| B       | Snail1            | L    |  1   |
+---------+-------------------+------+------+
| C       | Dog               | H    |  1   |
| C       | Dog               | M    |  0   |
+---------+-------------------+------+------+
| D       | Sheep             | H    |  0   |
| D       | Sheep             | L    |  1   |
+---------+-------------------+------+------+
| E       | Fish              | H    |  1   |
| E       | Fish              | H    |  1   |
+---------+-------------------+------+------+

我想要的结果

+---------+----------+-------+
| NAME    | TITLE    | SCORE |
+---------+----------+-------+
| A       | Hippo    |  100  |
| B       | Snail    |   80  |
| C       | Dog      |   80  |
| D       | Sheep    |   10  |
| E       | Fish     |  100  |
+---------+----------+-------+

需要进行计算:

  • 类型最多只能包含三个值:{H,M,L};
  • 当存在所有值时,将它们分级如下:H = 70 M = 20 L = 10
  • 所有独特的案例

    1. 案例{H,M}:H = 80 M = 20
    2. 情况{M,L}:M = 60L = 40
    3. 情况{H,L}:H = 90L = 10
    4. 案例{H}:H = 100
    5. 案例{M}:M = 100
    6. 案例{L}:L = 100
    7. 情况{H,M,L}:H = 70 M = 20 L = 10

解释

Hippo Has case {H,M,L}
Snail has case {H,M,L}
Dog has case {H,M}
Sheep has case {H,L}
Fish has case {H} 

进一步说明

河马:虽然不是所有尺码都有一个打击,但是Hippo已经获得了100分,因为每个案例中至少有一个已经获得了一次冠军。因此 HIPPO3 有三个H M L我很高兴,所以 hippo 可以被认为是100%

从上面提取(线条是观察者的逻辑构造)

| A       | Hippo1            | H    |  0   |
| A       | Hippo2            | H    |  0   |
| A       | Hippo3            | H    |  1   | <--Here   
+-------------------------------------------+    
| A       | Hippo1            | M    |  0   |
| A       | Hippo2            | M    |  1   |
| A       | Hippo3            | M    |  1   | <--Here
+-------------------------------------------+   
| A       | Hippo1            | L    |  0   |
| A       | Hippo2            | L    |  1   |    
| A       | Hippo3            | L    |  1   | <--Here

1 个答案:

答案 0 :(得分:1)

你走了:

select name, sum(FinalVal) Score from (
  select distinct t.name,
    case
      when size = 'H' then Hval
      when size = 'M' then Mval
      else Lval
    end FinalVal
  from (
    select name,
      case
        when sizes = 'H,L,M' then 70
        when sizes = 'H,M' then 80
        when sizes = 'H,L' then 90
        when sizes = 'H' then 100
        else 0
      end Hval,
      case
        when sizes = 'H,L,M' then 20
        when sizes = 'H,M' then 20
        when sizes = 'L,M' then 60
        when sizes = 'M' then 100
        else 0
      end Mval,
      case
        when sizes = 'H,L,M' then 10
        when sizes = 'L,M' then 40
        when sizes = 'H,L' then 10
        when sizes = 'L' then 100
        else 0
      end Lval
    from (
      select name, group_concat(distinct size order by size) sizes from t
      group by name
    ) s1
  ) s2
  join t on t.name = s2.name
  where hit
) final
group by name

目前尚不清楚如何根据您的数据获取标题(实际上结果包含原始表格中没有的数据,例如Hippo,而不是Hippo1。但我会留给你。

这将导致:

+------+-------+
| NAME | SCORE |
+------+-------+
| A    |   100 |
| B    |    80 |
| C    |    80 |
| D    |    10 |
| E    |   100 |
+------+-------+