没有聚合函数的支点

我有一张这样的桌子。

CustomerID   DBColumnName   Data
--------------------------------------
1            FirstName      Joe
1            MiddleName     S
1            LastName       Smith
1            Date           12/12/2009
2            FirstName      Sam
2            MiddleName     S
2            LastName       Freddrick
2            Date           1/12/2009
3            FirstName      Jaime
3            MiddleName     S
3            LastName       Carol
3            Date           12/1/2009

我想要这个。

这可能使用 PIVOT 吗?

CustomerID  FirstName   MiddleName          LastName        Date
----------------------------------------------------------------------
1           Joe             S               Smith           12/12/2009
2           Sam             S               Freddrick       1/12/2009
3           Jaime           S               Carol           12/1/2009
240689 次浏览

你可以使用 MAX 聚合,它仍然可以工作。一个值的 MAX = 那个值。

在这种情况下,还可以在 customerid 上自我连接5次,每个表引用使用 dbColumnName 进行筛选。也许结果会更好。

是的,但是为什么?

   Select CustomerID,
Min(Case DBColumnName When 'FirstName' Then Data End) FirstName,
Min(Case DBColumnName When 'MiddleName' Then Data End) MiddleName,
Min(Case DBColumnName When 'LastName' Then Data End) LastName,
Min(Case DBColumnName When 'Date' Then Data End) Date
From table
Group By CustomerId
SELECT
main.CustomerID,
f.Data AS FirstName,
m.Data AS MiddleName,
l.Data AS LastName,
d.Data AS Date
FROM table main
INNER JOIN table f on f.CustomerID = main.CustomerID
INNER JOIN table m on m.CustomerID = main.CustomerID
INNER JOIN table l on l.CustomerID = main.CustomerID
INNER JOIN table d on d.CustomerID = main.CustomerID
WHERE f.DBColumnName = 'FirstName'
AND m.DBColumnName = 'MiddleName'
AND l.DBColumnName = 'LastName'
AND d.DBColumnName = 'Date'

编辑: 我写这个没有编辑器和没有运行 SQL。我希望,你得到的想法。

好吧,很抱歉问了这个问题。 gbn 让我回到了正确的轨道上。 这就是我想要的答案。

SELECT [FirstName], [MiddleName], [LastName], [Date]
FROM #temp
PIVOT
(   MIN([Data])
FOR [DBColumnName] IN ([FirstName], [MiddleName], [LastName], [Date])
)AS p

然后,我必须使用 while 语句,并将上面的语句构建为 varchar 语句,并使用 Dynamic sql。

用这样的东西

SET @fullsql = @fullsql + 'SELECT ' + REPLACE(REPLACE(@fulltext,'(',''),')','')
SET @fullsql = @fullsql + 'FROM #temp '
SET @fullsql = @fullsql + 'PIVOT'
SET @fullsql = @fullsql + '('
SET @fullsql = @fullsql + ' MIN([Data])'
SET @fullsql = @fullsql + ' FOR [DBColumnName] IN '+@fulltext
SET @fullsql = @fullsql + ')'
SET @fullsql = @fullsql + 'AS p'


EXEC (@fullsql)

使用 while 循环构建@fulltext,并从表中选择不同的列名。谢谢你的回答。

OP 实际上不需要没有聚合就转向,但是对于你们这些来到这里的人来说,知道如何看到:

Sql 参数化 cte 查询

这个问题的答案涉及这样一种情况,即需要没有聚合的 pivot,因此这样做的示例就是解决方案的一部分。

WITH pivot_data AS
(
SELECT customerid, -- Grouping Column
dbcolumnname, -- Spreading Column
data -- Aggregate Column
FROM pivot2
)
SELECT customerid, [firstname], [middlename], [lastname]
FROM pivot_data
PIVOT (max(data) FOR dbcolumnname IN ([firstname],[middlename],[lastname])) AS p;

试试这个:

SELECT CUSTOMER_ID, MAX(FIRSTNAME) AS FIRSTNAME, MAX(LASTNAME) AS LASTNAME ...


FROM
(


SELECT CUSTOMER_ID,
CASE WHEN DBCOLUMNNAME='FirstName' then DATA ELSE NULL END AS FIRSTNAME,
CASE WHEN DBCOLUMNNAME='LastName' then DATA ELSE NULL END AS LASTNAME,
... and so on ...
GROUP BY CUSTOMER_ID


) TEMP


GROUP BY CUSTOMER_ID

这应该会奏效:

select * from (select [CustomerID]  ,[Demographic] ,[Data]
from [dbo].[pivot]
) as Ter


pivot (max(Data) for  Demographic in (FirstName, MiddleName, LastName, [Date]))as bro

这里有一个为轴心查询构建动态字段的好方法:

——将值汇总到 tmp 表中

declare @STR varchar(1000)
SELECT  @STr =  COALESCE(@STr +', ', '')
+ QUOTENAME(DateRange)
from (select distinct DateRange, ID from ##pivot)d order by ID

——查看生成的字段

print @STr


exec('  .... pivot code ...
pivot (avg(SalesAmt) for DateRange IN (' + @Str +')) AS P
order by Decile')