如何在SQL语句中将值从一列显示为两列?

时间:2012-03-01 00:20:19

标签: sql join pivot

我有一张如下表:

enter image description here

有多个WebSiteID。我希望输出如下:

Date  , WebSiteID1_Views, WebSiteID2_Views
1/1/12, 91              , 43
1/2/12, 84              , 34

where SiteUSerID = some userdefined ID

我很确定我需要做某种加入,但我无法在Stack上找到答案。

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

这是PIVOT方式:

SELECT [Date],
       COALESCE([1], 0) AS WebSiteID1_Views,
       COALESCE([2], 0) AS WebSiteID2_Views
FROM (
    SELECT [Date], WebSiteID, WebSiteViews
    FROM @yourTable
    WHERE SiteUSerID = [some userdefined ID]
) AS T
PIVOT(SUM(WebSiteViews) FOR WebSiteID IN([1], [2])) AS P

......非 - PIVOT方式:

SELECT
    [Date],
    SUM(CASE WebSiteID WHEN 1 THEN WebSiteViews ELSE 0 END) AS WebSiteID1_Views,
    SUM(CASE WebSiteID WHEN 2 THEN WebSiteViews ELSE 0 END) AS WebSiteID2_Views
FROM @yourTable
WHERE SiteUSerID = [some userdefined ID]
GROUP BY Date

答案 1 :(得分:0)

你想要的是动态组装的枢轴。

这样,您可以拥有多个未确定的WebSiteID值,并且仍可以使用。

我现在没时间为你的问题写一个具体的答案,但你可以在这里找到一个模拟解决方案:https://stackoverflow.com/a/7182489/570191

只需根据您的需求进行调整

答案 2 :(得分:0)

也许是这样的:

测试数据

CREATE TABLE Table1
        (
            ID INT,
            Date DATETIME,
            SiteUser INT,
            WebSiteID INT,
            WebSiteViews INT
        )
INSERT INTO Table1
VALUES
    (1,'2011-10-06 00:00:00:000',36,1,197),
    (2,'2011-10-06 00:00:00:000',37,1,70),
    (3,'2011-10-06 00:00:00:000',36,1,111),
    (4,'2011-10-06 00:00:00:000',32,1,24),
    (5,'2012-02-26 00:00:00:000',31,1,37),
    (6,'2012-02-26 00:00:00:000',36,1,92),
    (7,'2012-02-26 00:00:00:000',34,1,50),
    (8,'2012-02-26 00:00:00:000',18,1,43),
    (9,'2012-02-26 00:00:00:000',89,1,12),
    (10,'2012-02-15 00:00:00:000',45,2,45),
    (11,'2012-02-15 00:00:00:000',78,2,54),
    (12,'2012-03-24 00:00:00:000',36,2,45),
    (13,'2012-03-24 00:00:00:000',36,2,197),
    (14,'2012-03-24 00:00:00:000',45,2,300),
    (15,'2012-03-24 00:00:00:000',3,2,78),
    (16,'2012-03-24 00:00:00:000',2,2,45),
    (17,'2012-03-24 00:00:00:000',1,2,100),
    (18,'2012-03-24 00:00:00:000',4,2,1)

唯一列名称

DECLARE @cols VARCHAR(MAX)
;WITH CTE
AS
(
    SELECT
        ROW_NUMBER() OVER(PARTITION BY Table1.WebSiteID ORDER BY Table1.WebSiteID) AS RowNbr,
        CAST(Table1.WebSiteID AS VARCHAR(100)) AS WebSiteID
    FROM
        Table1
)
SELECT  @cols = COALESCE(@cols + ','+QUOTENAME('WebSiteID'+WebSiteID+'_Views'),
                    QUOTENAME('WebSiteID'+WebSiteID+'_Views'))
FROM
    CTE
WHERE
    CTE.RowNbr=1

动态支点

DECLARE @SiteUser INT=36
DECLARE @query NVARCHAR(4000)=
N'SELECT
    *
FROM
(
    SELECT
        convert(varchar(6), Date, 103)+RIGHT(CAST(YEAR(Date) AS VARCHAR(4)),2) AS Date,
        Table1.WebSiteViews,
        ''WebSiteID''+CAST(WebSiteID AS VARCHAR(100))+''_Views'' AS WebSiteID
    FROM
        Table1
    WHERE
        Table1.SiteUser='+CAST(@SiteUser AS VARCHAR(100))+'
) AS p
PIVOT
(
    SUM(WebSiteViews)
    FOR WebSiteID IN ('+@cols+')
)AS pvt'
EXECUTE(@query)

在我的情况下,我会删除临时表:

DROP TABLE Table1