使用' Group By'续集中两次

时间:2015-02-26 18:54:59

标签: sql sequel sequelpro

全部,我正在使用以下续集查询...

select
u.full_name, u.id, sum(hit_count), tag_names
from MyTable as t
inner join MyUsers as u on u.id=t.user_id
where t.created_at >= '2015-1-01 04:00:00' AND t.created_at < '2015-2-01 04:00:00' AND tag_names like '%Tree%'
group by u.id

...使用大约1,000行呈现一个看起来像这样的数据表:

| full_name    | id  | sum(hit_count) | tag_names                 |

| Fred Johnson | 001 | 1500           | Tree, Apple, Blue, Banana |
| Joe Smith    | 002 | 663            | Tree, Red, Pear, Yellow   |    
| Tina Cook    | 001 | 1500           | Tree, Berry, Red, Grape   |
| Kyle Cross   | 001 | 1500           | Tree, Berry, Red, Grape   |

但是,因为我知道有限数量的原色标签(例如红色,蓝色,黄色)可用,有没有办法调整我的续集查询也按主色标签分组(并添加一个主要颜色标签列到我的表)来渲染......

 | full_name    | id  | sum(hit_count) | tag_names                 | Primary_Color_Tag |

 | Fred Johnson | 001 | 1500           | Tree, Apple, Blue, Banana | Blue              | 
 | Joe Smith    | 002 | 500            | Tree, Red, Pear, Yellow   | Red               |  
 | Tina Cook    | 001 | 1500           | Tree, Berry, Red, Grape   | Red               |
 | Kyle Cross   | 001 | 1500           | Tree, Berry, Red, Grape   | Red               | 
 | Joe Smith    | 001 | 163            | Tree, Red, Pear, Yellow   | Yellow            | 

我在similar questions上找到了this forum看似相关的解决方案,但我无法拼凑他们的逻辑来重现我追求的表格。任何见解将不胜感激!

0 个答案:

没有答案