5

I have a problem creating a SQL-statement for sqlserver2008. I have the following data:

city     person     priority
-----------------------------------
Linz     Mike       1
Wien     Mike       1   
Linz     Tom        1
Wien     Tom        1
Linz     John       1
Linz     Sarah      2

This means that the persons Mike and Tom choose the cities Linz and Wien with priority 1.
John chooses Linz with priority 1.
Sarah chooses Linz with priority 2.

now I want the following output:

cities          persons          priority
-----------------------------------
Linz, Wien      Mike, Tom       1
Linz            John            1
Linz            Sarah           2

I already have following SQL-Statement but I do not get the expected result as this query would say that John also has an entry for Wien with priority 1.

SELECT
(SELECT 
  STUFF((SELECT ', ' + d.City 
  FROM (SELECT DISTINCT d2.City FROM dbo.DummyTable d2
        WHERE d2.Priority = d1.Priority) d
  FOR XML PATH('')), 1, 2, '')
  ) 
AS Cities,
(SELECT 
  STUFF((SELECT ', ' + d.Person 
  FROM (SELECT DISTINCT d2.Person FROM dbo.DummyTable d2
        WHERE d2.Priority = d1.Priority) d
  FOR XML PATH('')), 1, 2, '')
  ) 
AS Persons,
d1.Priority
FROM
dbo.DummyTable d1
GROUP BY d1.Priority

You can also use this SQL Fiddle

Any ideas how this query could be written in SQL?

4

2 回答 2

4

这是一种方法:

;with PersonCityGroupPreferences as (
  select
    Person,
    Priority, 
    stuff ((
      select ', ' + d2.City
      from DummyTable d2
      where d1.Priority = d2.Priority
          and d1.Person = d2.Person
      FOR XML PATH('')
    ), 1, 2, '') Cities
  from DummyTable d1
  group by Person, Priority
)
select 
  Cities,
  stuff ((
    select ', ' + p2.Person
    from PersonCityGroupPreferences p2
    where p1.Cities = p2.Cities
      and p1.Priority = p2.Priority
    FOR XML PATH('')
  ), 1, 2, '') Persons,
  Priority
from PersonCityGroupPreferences p1
group by Priority, Cities

SQLFiddle 链接:http ://www.sqlfiddle.com/#!3/d831d/57

为了达到最终的效果,我将解决方案分为两步:

  1. Person获取按和对数据进行分组的结果集,Priority并包含逗号分隔的城市列表作为第三列

  2. 取获得点 1 的结果集并执行相同的操作,但现在按列分组Cities(逗号分隔列表)并Priority生成相应人员的逗号分隔列表。

在上面的查询中,第 1 步是这个查询:

select
  Person,
  Priority, 
  stuff ((
    select ', ' + d2.City
    from DummyTable d2
    where d1.Priority = d2.Priority
    and d1.Person = d2.Person
    FOR XML PATH('')
  ), 1, 2, '') Cities
from DummyTable d1
group by Person, Priority

以下是部分结果在 SQL 中的样子:http ://www.sqlfiddle.com/#!3/d831d/58

然后,我将第一个查询公开为CTE,使其可用于(外部)查询 2,它基本上做同样的事情,但具有不同的分组标准。

于 2013-08-26T16:13:09.870 回答
0

我认为您可以最大程度地做到这一点:http ://www.sqlfiddle.com/#!3/d831d/26

SELECT
(SELECT 
  STUFF((SELECT ', ' + d.City 
  FROM (SELECT DISTINCT d2.City FROM dbo.DummyTable d2
        WHERE d2.Priority = d1.Priority and d2.City = d1.City) d
  FOR XML PATH('')), 1, 2, '')
  ) 
AS Cities,
(SELECT 
  STUFF((SELECT ', ' + d.Person 
  FROM (SELECT DISTINCT d2.Person FROM dbo.DummyTable d2
        WHERE d2.Priority = d1.Priority and d2.City = d1.City) d
  FOR XML PATH('')), 1, 2, '')
  ) 
AS Person,
d1.Priority
FROM
dbo.DummyTable d1
GROUP BY d1.Priority,d1.City
于 2013-08-26T16:05:10.790 回答