简体   繁体   English

SQL Server 连接 GROUP BY

[英]SQL Server Concatenate GROUP BY

I have a query that looks like this我有一个看起来像这样的查询

SELECT J.JobID,T.Title FROM JobsTagMap J
Left Join Tags T
ON J.TagID=T.TagID

That returns the following dataset (simplified, JobID is actually a UniqueIdentifier)返回如下数据集(简而言之,JobID实际上是一个UniqueIdentifier)

JobID    Title
1        Tag1
1        Tag2
2        Tag2
2        Tag5
2        Tag9

Now, i'd like to group this by the JobID-column and concatenate the Title, so the results is as following现在,我想按 JobID 列对其进行分组并连接标题,因此结果如下

JobID    Title
1        Tag1,Tag2
2        Tag2,Tag5,Tag9

How would i do that?我该怎么做?

If you are using sql server 2005+.如果您使用的是 sql server 2005+。 Then you can do like this:然后你可以这样做:

SELECT 
    JobsTagMap.JobID,
    STUFF
    (
        (
            SELECT 
                ',' +Title
            FROM
                Tags
            WHERE
                Tags.TagID=JobsTagMap.TagID
            FOR XML PATH('')
        )
    ,1,1,'') AS Title
FROM JobsTagMap

EDIT编辑

Because you did not show us the table structure and the data in the different tables.因为您没有向我们展示表结构和不同表中的数据。 It was a lite bit hard to know.这有点难知道。 So I assume that your table structure looks something like this:所以我假设你的表结构看起来像这样:

CREATE TABLE JobsTagMap
(
    JobID INT,
    TagID INT
)

CREATE TABLE Tags
(
    TagID INT,
    Title VARCHAR(100)
)

With this data:有了这个数据:

INSERT INTO JobsTagMap
VALUES(1,1),(1,2),(2,2),(2,4),(2,5)

INSERT INTO Tags
VALUES(1,'Tag1'),(2,'Tag2'),(3,'Tag2'),(4,'Tag5'),(5,'Tag9')

If you are getting that data that you are showing the JobID cannot be unique.如果您正在获取显示的数据,则JobID不能是唯一的。 You might have the a Job table somewhere where it is unique.您可能在某个位置拥有一个Job表,它是唯一的。 If you just want to use these table that you are showing then you need to do something like this:如果您只想使用您显示的这些表,那么您需要执行以下操作:

;WITH CTE
AS
(
    SELECT
        ROW_NUMBER() OVER(PARTITION BY JobID ORDER BY JobID) AS RowNbr,
        JobsTagMap.*
    FROM
        JobsTagMap
)
SELECT
    *,
    STUFF
    (
        (
            SELECT 
                ',' +Title
            FROM
                Tags
                JOIN JobsTagMap
                    ON Tags.TagID=JobsTagMap.TagID
            WHERE
                JobsTagMap.JobID=CTE.JobID
            FOR XML PATH('')
        )
    ,1,1,'') AS Title
FROM
    CTE
WHERE
    CTE.RowNbr=1

This will get you this result:这会给你这个结果:

1   1   1   Tag1,Tag2
1   2   2   Tag2,Tag5,Tag9

So in the future always show what table structure and it data .所以以后总是显示什么表结构和它的数据 That will give you better answers这会给你更好的答案

I use a scalar function for exactly that.我正是为此使用了标量函数。 There are going to be some purist that decry should never use a row based operation but hey this works and if you are only returning a few rows then response time is fine.会有一些纯粹主义者认为 decry 永远不应该使用基于行的操作,但是嘿,这是可行的,如果您只返回几行,那么响应时间就可以了。

CREATE FUNCTION [dbo].[JoinMVText]

(

  @sID int,

  @fieldID tinyint

)

RETURNS VARCHAR(MAX)

AS 

BEGIN

   DECLARE @MVtextList varchar(max)

   SELECT @MVtextList = COALESCE(@MVtextList + '; ', '') + docMVtext.value

   FROM docMVtext with (nolock) 

   WHERE docMVtext.sID = @sID and fieldID = @fieldID

   RETURN @MVtextList

END

I had the same problem as you did and I figured out how to workaround slow sub-selects.我遇到了和你一样的问题,我想出了如何解决慢子选择的方法。

Using GROUP BY :使用GROUP BY

(70500 rows affected)

 SQL Server Execution Times:
   CPU time = 94 ms,  elapsed time = 833 ms.

Using Sub-Selects :使用子选择

(70500 rows affected)

 SQL Server Execution Times:
   CPU time = 1469 ms,  elapsed time = 2323 ms.

Sub-selects are more then 4 times slower...子选择要慢 4 倍以上......

Here's the solution:这是解决方案:

SELECT 
    J.JobID,
    STRING_AGG(ISNULL(T.Title, ''), ',') as Title
FROM JobsTagMap J
LEFT JOIN Tags T ON J.TagID = T.TagID
GROUP BY J.JobID;

Let me know if something is not clear enough :)如果有什么不够清楚,请告诉我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM