【问题标题】:STRING_AGG with distinct without sub-querySTRING_AGG 具有不同的无子查询
【发布时间】:2020-09-15 05:52:31
【问题描述】:

这是我的数据

Code    SubCode    Colour     Fruit     Car     City     Name
A       A1         Red        Apple     Honda   Mel      John
A       A1         Green      Apple     Toyota  NYC      John
A       A1         Red        Banana    Honda   Lon      John
A       A1         Red        Banana    Opel    Mel      John
A       A2         ...
A       A2         ...
A       A3
A       A3

这是我的 sql

SELECT Code, SubCode, STRING_AGG(Colour, ',') STRING_AGG(Fruit, ',') STRING_AGG(Car, ',') STRING_AGG(City, ',') STRING_AGG(Name, ',')
FROM myTable

我得到了这个结果

Code    SubCode    Colour              Fruit                       Car                      City     Name
A       A1         Red,Green,Red,Red   Apple,Apple,Banana,Banan    Honda,Toyota,Honda,Opel  ...

有没有办法让我在

中得到不同的值

对于单个值,我可以创建一个子查询 STRING_AGG?

Code    SubCode    Colour      Fruit           Car                   City     Name
A       A1         Red,Green   Apple,Banana    Honda,Toyota,Opel     ...

【问题讨论】:

    标签: sql sql-server string subquery aggregate-functions


    【解决方案1】:

    唉,SQL Server 的string_agg() 目前不支持DISTINCT。所以你需要多个子查询,像这样:

    select 
        code, 
        subcode, 
        (select string_agg(color, ',') from (select distinct color from mytable  t1 where t1.code = t.code and t1.subcode = t.subcode) t) colors,
        (select string_agg(fruit, ',') from (select distinct fruit from mytable  t1 where t1.code = t.code and t1.subcode = t.subcode) t) fruits,
        (select string_agg(car  , ',') from (select distinct car   from mytable  t1 where t1.code = t.code and t1.subcode = t.subcode) t) cars,
        (select string_agg(city , ',') from (select distinct city  from mytable  t1 where t1.code = t.code and t1.subcode = t.subcode) t) cities,
        (select string_agg(name , ',') from (select distinct name  from mytable  t1 where t1.code = t.code and t1.subcode = t.subcode) t) names
    from mytable t
    group by code, subcode
    

    请注意,您的原始查询缺少 group by 子句,因此它是无效的 SQL。我也解决了这个问题。

    【讨论】:

      【解决方案2】:

      遗憾的是string_agg() 不支持distinct。但是,使用row_number() 很容易模拟:

      SELECT Code, SubCode,
             STRING_AGG(CASE WHEN seqnum_colour = 1 THEN Colour END, ','),
             STRING_AGG(CASE WHEN seqnum_fruit= 1 THEN Fruit END, ','),
             STRING_AGG(CASE WHEN seqnum_car = 1 THEN Car END, ','),
             STRING_AGG(CASE WHEN seqnum_city = 1 THEN City END, ','),
             STRING_AGG(CASE WHEN seqnum_name = 1 THEN Name END, ',')
      FROM (SELECT t.*,
                   ROW_NUMBER() OVER (PARTITION BY Code, SubCode, Colour ORDER BY Code) as seqnum_colour,
                   ROW_NUMBER() OVER (PARTITION BY Code, SubCode, Fruit ORDER BY Code) as seqnum_fruit,
                   ROW_NUMBER() OVER (PARTITION BY Code, SubCode, Car ORDER BY Code) as seqnum_car,
                   ROW_NUMBER() OVER (PARTITION BY Code, SubCode, City ORDER BY Code) as seqnum_city,
                   ROW_NUMBER() OVER (PARTITION BY Code, SubCode, Name ORDER BY Code) as seqnum_name
            FROM myTable t
           ) t
      GROUP BY code, subcode;
      

      Here 是一个 dbfiddle。

      【讨论】:

        猜你喜欢
        • 2012-05-12
        • 1970-01-01
        • 2021-12-12
        • 1970-01-01
        • 1970-01-01
        • 2020-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多