0

我有一个数据表如下:

t1
____________________________________________________
resources          | companyName | Response | Score 
David, Matt        |  companyA   |  YES     |   5
Matt               |  compqanyB  |  NO      |   8
Kyle, Kyle, David  |  companyC   |  YES     |   2

如您所见,resources是一个逗号分隔的字符串。此外,并非所有成员resources都必须是唯一的(参见第 3 行)。

我希望任何列表中的GROUP BY每个DISTINCT成员都可用。所有其他列将被聚合。

预期结果:

query
______________________________________________________________________________
resources     |  companyName        |    Response         | Score
David         | *agregated result*  | *agregated result*  | *agregated result*  
Matt          | *agregated result*  | *agregated result*  | *agregated result*  
Kyle          | *agregated result*  | *agregated result*  | *agregated result*  

编辑:

另一种可能:

query
____________________________________________________
resources          | companyName | Response | Score 
David              |  companyA   |  YES     |   5
Matt               |  companyA   |  YES     |   5
Matt               |  compqanyB  |  NO      |   8
Kyle               |  companyC   |  YES     |   2
David              |  companyC   |  YES     |   2
4

1 回答 1

3

让我假设您有一个包含各个名称的资源表。在这种情况下,您可以使用以下查询执行您想要的操作:

select r.name, <other aggregated results>
from t1 join
     Resources r
     on concat(', ', t1.resources, ', ') like concat(', %', r.name, ', %')
group by r.name

如果您没有资源表,则可能应该。将这些东西存储在逗号分隔的列表中通常是一个坏主意。如果数据没有被规范化并且出现拼写错误,这是一个特别糟糕的主意。

于 2012-10-25T14:52:20.330 回答