将站点列表和出现次数连接到列中

时间:2014-11-10 20:53:33

标签: sql sql-server tsql subquery concatenation

使用此页面:Can I Comma Delimit Multiple Rows Into One Column?

......我已经能够拿出这样的结果:

enter image description here

使用此查询:

SELECT
    [Client ID],
    STUFF((SELECT ', ' + [Location (counts)]
           FROM (
                SELECT TOP 100 PERCENT [Client ID]
                    , ltrim(str([Store Num])) + ' (' + ltrim(str(count([Store Num]))) + ') ' [Location (counts)]
                FROM @tbl_coreData
                GROUP BY [Store Num], [Client ID]
                ORDER BY COUNT([Store Num]) DESC) tblThis 
           WHERE ([Store Num] = Result.[Store Num] 
             AND [Client ID] = Result.[Client ID]) 
           FOR XML PATH ('')),1,1,'') AS BATCHNOLIST
FROM @tbl_coreData AS Result
GROUP BY [Client ID], [Store Num]

我希望列表按出现次数(括号中的数字)排序。我的尝试是

SELECT TOP 100 PERCENT 
    [Client ID]

ORDER BY COUNT([Store Num]) DESC

但这没有用。

如何按照出现下降的顺序显示连接值?

与往常一样,欢迎并感谢所有帮助。

2 个答案:

答案 0 :(得分:2)

这样的事情:

with pre AS
(
  SELECT 
    [Client ID],
    ltrim(str([Store Num])) as [Store Num]
    count([Store Num]) as locationcount
  FROM @tbl_coreData
  Group By [Store Num], [Client ID]
)
Select [Client ID],
       STUFF((SELECT ', ' + [Location (counts)]
              FROM (
                SELECT [Client ID], [Store Num] + ' (' + locationcount + ') ' [Location (counts)]
                FROM pre
                Order By locationcount desc
              ) tblThis 
              WHERE ([Store Num] = Result.[Store Num] AND [Client ID] = Result.[Client ID]) 
              FOR XML PATH ('')),1,1,'') AS BATCHNOLIST
From pre AS Result
order by locationcount desc

答案 1 :(得分:1)

经过多次烦躁和使用Hogan的建议后,我想出了这个:

DECLARE @Heroes TABLE (
    [HeroName]      VARCHAR(20),
    [HeroFriend]    VARCHAR(20)
)

INSERT INTO @Heroes ( [HeroName], [HeroFriend] )
VALUES  ( 'Superman', 'Aquaman' ), 
        ( 'Superman', 'Batman' ), 
        ( 'Superman', 'Wolverine' ), 
        ( 'Superman', 'Batman' ), 
        ( 'Superman', 'Wolverine' ), 
        ( 'Superman', 'Batman' ), 
        ( 'Superman', 'Ironman' ), 
        ( 'Superman', 'Wolverine' ), 
        ( 'Superman', 'Batman' ), 
        ( 'Superman', 'Ironman' ), 
        ( 'Superman', 'Wolverine' ), 
        ( 'Superman', 'Batman' ), 
        ( 'Superman', 'Ironman' ), 
        ( 'Superman', 'Wolverine' ), 
        ( 'Superman', 'Wolverine' ), 
        ( 'Batman', 'Wolverine' );

with pre as (
  SELECT
    [HeroName],
    count([HeroName]) [locationcount],
    '(' + ltrim(str(count([HeroName]))) + ') ' + [HeroFriend] [Concat]
  FROM @Heroes
  Group By [HeroName],
           [HeroFriend]
)

Select DISTINCT
       [HeroName],
       STUFF((SELECT ',' + [Concat]
              FROM (
                    SELECT TOP 100 [HeroName],[Concat]
                    FROM pre
                    order by locationcount desc
              ) tblThis 
              WHERE ([HeroName] = Result.[HeroName]
                 )
              FOR XML PATH ('')),1,1,'') AS [Loc Counts]
From pre AS Result

由于某些原因,当我尝试连续进行连接和/或按计数排序时,SQL并不喜欢它。代码将无错误地运行,但排序被忽略。但是把这些放进"用"阻止事先解决了它。