两个组列的SQL语句

时间:2013-08-26 15:52:21

标签: sql sql-server group-by

我在为sqlserver2008创建SQL语句时遇到问题。我有以下数据:

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

这意味着人员 Mike Tom 选择优先级为1的城市 Linz Wien 。<登记/> John 选择优先级为1的 Linz Sarah 选择优先级为2的 Linz

现在我想要以下输出:

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

我已经有了以下SQL-Statement但我没有得到预期的结果,因为这个查询会说 John 还有一个优先级为 Wien 的条目。< / p>

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

您也可以使用此SQL Fiddle

如何在SQL中编写此查询的任何想法?

2 个答案:

答案 0 :(得分: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. 获取按PersonPriority对数据进行分组的结果集,并将逗号分隔的城市列表列为第三列

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

  3. 在上面的查询中,第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,它基本上做同样的事情,但具有不同的分组标准。

答案 1 :(得分: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