没有聚合函数的 TSQL Pivot

发布于 2024-08-03 08:02:39 字数 1288 浏览 9 评论 0原文

我有一个这样的表...

CustomerIDDBColumnNameData
1FirstNameJoe
1MiddleNameS
1LastNameSmith
1Date12/12/2009
2FirstNameSam
2MiddleNameS
2LastNameFreddrick
2Date1/12/2009
3FirstNameJaime
3MiddleNameS
3LastNameCarol
3日期12/1/2009

我想要这个...

使用 PIVOT 可以吗?

客户ID 名字中间名姓氏日期
1JoeSSmith12/12/2009
2SamSFreddrick1/12/2009
3JaimeSCarol12/1/2009

I have a table like this...

CustomerIDDBColumnNameData
1FirstNameJoe
1MiddleNameS
1LastNameSmith
1Date12/12/2009
2FirstNameSam
2MiddleNameS
2LastNameFreddrick
2Date1/12/2009
3FirstNameJaime
3MiddleNameS
3LastNameCarol
3Date12/1/2009

And I want this...

Is this possible using PIVOT?

CustomerIDFirstNameMiddleNameLastNameDate
1JoeSSmith12/12/2009
2SamSFreddrick1/12/2009
3JaimeSCarol12/1/2009

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

抠脚大汉 2024-08-10 08:02:39

是的,但是为什么!!!

   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

yes, but why !!??

   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
花桑 2024-08-10 08:02:39

您可以使用 MAX 聚合,它仍然可以工作。一个值的最大值 = 该值。

在这种情况下,您还可以在 customerid 上自连接 5 次,按每个表引用的 dbColumnName 进行过滤。效果可能会更好。

You can use the MAX aggregate, it would still work. MAX of one value = that value..

In this case, you could also self join 5 times on customerid, filter by dbColumnName per table reference. It may work out better.

素衣风尘叹 2024-08-10 08:02:39
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;
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;
能怎样 2024-08-10 08:02:39

好吧,抱歉这个问题不好。 gbn 让我走上了正轨。
这就是我在答案中寻找的内容。

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

然后我必须使用 while 语句并将上述语句构建为 varchar 并使用 dynmaic 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 并从表中选择不同的列名称。感谢您的回答。

Ok, sorry for the poor question. gbn got me on the right track.
This is what I was looking for in an answer.

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

Then I had to use a while statement and build the above statement as a varchar and use dynmaic sql.

Using something like this

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)

Having a to build @fulltext using a while loop and select the distinct column names out of the table. Thanks for the answers.

平安喜乐 2024-08-10 08:02:39

OP实际上不需要在没有聚合的情况下进行旋转,但是对于那些来这里了解如何查看的人来说:

sql 参数化 cte 查询

该问题的答案涉及需要不进行聚合的数据透视的情况,因此执行此操作的示例是解决方案的一部分。

The OP didn't actually need to pivot without agregation but for those of you coming here to know how see:

sql parameterised cte query

The answer to that question involves a situation where pivot without aggregation is needed so an example of doing it is part of the solution.

岁月静好 2024-08-10 08:02:39
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。我希望,你明白了。

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' 

Edit: I have written this without an editor & have not run the SQL. I hope, you get the idea.

昇り龍 2024-08-10 08:02:39

这应该有效:

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

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

This should work:

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

pivot (max(Data) for  Demographic in (FirstName, MiddleName, LastName, [Date]))as bro
哎呦我呸! 2024-08-10 08:02:39

试试这个:

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

Try this:

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
谁的新欢旧爱 2024-08-10 08:02:39

这是为数据透视查询构建动态字段的好方法:

--将值汇总到临时表中

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')

Here is a great way to build dynamic fields for a pivot query:

--summarize values to a tmp table

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

---see the fields generated

print @STr

exec('  .... pivot code ...
pivot (avg(SalesAmt) for DateRange IN (' + @Str +')) AS P
order by Decile')
ゞ花落谁相伴 2024-08-10 08:02:39

根据定义,所有数据透视都是聚合的,但是有一种简单的方法可以确保所有数据都得到透视。
除了枢轴之外的列是分组依据的列。因此,您可以在由其他分组依据划分的数据中创建一个 row_number 并将其包含在您的数据透视数据中。例如:

with data as (
    select 'a' key1,'1' key2,'samplecolumn' as col,'3' as val
    union all
    select 'a' key1,'1' key2,'samplecolumn' as col,'6' as val
    union all
    select 'a' key1,'2' key2,'samplecolumn' as col,'7' as val
    union all
    select 'a' key1,'2' key2,'samplecolumn' as col,'9' as val
    union all
    select 'b' key1,'1' key2,'samplecolumn' as col,'23' as val
    union all
    select 'b' key1,'1' key2,'samplecolumn' as col,'45' as val
),
data_with_rownum as (
    select *, ROW_NUMBER() over (partition by key1,key2 order by (select null)) rownum from data
)
select key1,key2,rownum,samplecolumn from data_with_rownum
PIVOT (max(val) FOR col IN (samplecolumn)) AS p;

By definition, all pivots aggregate, however there is a simple way to make sure all the data gets pivoted.
The columns besides for the pivot are the group by's. So you can create a row_number in your data partioned by the other group by's and include that in your pivot data. for example:

with data as (
    select 'a' key1,'1' key2,'samplecolumn' as col,'3' as val
    union all
    select 'a' key1,'1' key2,'samplecolumn' as col,'6' as val
    union all
    select 'a' key1,'2' key2,'samplecolumn' as col,'7' as val
    union all
    select 'a' key1,'2' key2,'samplecolumn' as col,'9' as val
    union all
    select 'b' key1,'1' key2,'samplecolumn' as col,'23' as val
    union all
    select 'b' key1,'1' key2,'samplecolumn' as col,'45' as val
),
data_with_rownum as (
    select *, ROW_NUMBER() over (partition by key1,key2 order by (select null)) rownum from data
)
select key1,key2,rownum,samplecolumn from data_with_rownum
PIVOT (max(val) FOR col IN (samplecolumn)) AS p;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文