簡體   English   中英

如何在我的 PIVOT 值中為我的每個列添加一個額外的非聚合列?

[英]How do I include an additional non-aggregated column for each of my in my PIVOT values?

我有以下代碼片段,它給了我下面的當前結果 我正在嘗試為每個透視值添加一個附加列,以便包含我的每個 siteuserid / tagname 組合的lastview數據(請參閱預期結果)。 由於本專欄不是匯總,我不相信額外的 pivot 會有所幫助。 我嘗試了多種添加lastview的方法,但它總是導致額外的行而不是所需的 output。

create table #taghits (userid int, email varchar(20), tagname varchar(20), hits int, lastview date)

insert into #taghits select 1, 'email1@here.com', 'tag1', 3, '2020-03-24';
insert into #taghits select 2, 'email2@here.com', 'tag1', 1, '2020-03-17';
insert into #taghits select 2, 'email2@here.com', 'tag2', 1, '2020-03-18';
insert into #taghits select 3, 'email3@here.com', 'tag1', 2, '2020-03-25';
insert into #taghits select 3, 'email3@here.com', 'tag2', 5, '2020-03-28';

select * from #taghits;

DECLARE @Columns3 as NVARCHAR(MAX)
SELECT @Columns3 = ISNULL(@Columns3 + ', ','') + QUOTENAME(TagName)
FROM (
    select distinct TagName
    from #taghits
) AS TagNames
ORDER BY TagNames.TagName

DECLARE @scolumns as NVARCHAR(MAX)
SELECT @scolumns = ISNULL(@Scolumns + ', ','')+ 'ISNULL(' + QUOTENAME(TagName) + ', 0) AS '+ QUOTENAME(TagName)
FROM (select distinct TagName
    from #taghits) AS TagNames
ORDER BY TagNames.TagName

DECLARE @SQL as NVARCHAR(MAX)
SET @SQL = '
    select userid, email, ' + @scolumns + '
    from
        (
        select userid, email, tagname, hits
        from #taghits
        ) as TagHits
    PIVOT (
        SUM(hits)
        FOR TagName IN (' + @Columns3 + ')
    ) AS PivotTable
    order by userId
'

exec sp_executesql @SQL;

當前結果

| userid | email           | tag1 | tag2 |
|--------|-----------------|------|------|
| 1      | email1@here.com | 3    | 0    |
| 2      | email2@here.com | 1    | 1    |
| 3      | email3@here.com | 2    | 5    |

期望的結果

| userid | email           | tag1_hits | tag1_lastview | tag2_hits | tag2_lastview |
|--------|-----------------|-----------|---------------|-----------|---------------|
| 1      | email1@here.com | 3         | 2020-03-24    | 0         | null          |
| 2      | email2@here.com | 1         | 2020-03-17    | 1         | 2020-03-18    |
| 3      | email3@here.com | 2         | 2020-03-25    | 5         | 2020-03-28    |

嘗試以下操作:

DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX);

SET @cols = STUFF((select distinct ', 
            SUM(CASE WHEN tagname=''' + CAST(tagname as varchar(10)) + ''' THEN [hits] ELSE 0 END) AS [' + CAST(tagname as varchar(10)) + '_hits],
            MAX(CASE WHEN tagname=''' + CAST(tagname as varchar(10)) + ''' THEN [lastview] ELSE NULL END) AS [' + CAST(tagname as varchar(10)) + '_lastview]'
            /*---------------You can add other columns here similar to above--------------*/
            FROM #taghits 
            FOR XML PATH(''),type).value('.','varchar(max)'),1,2,'')
SET @query = 'SELECT userid, email, ' + @Cols + '  FROM #taghits group by userid, email' 

print (@query)
exec(@query)

在此處查看 db<>fiddle。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM