Sql server 在分组查询上使用透视

Sql server 在分组查询上使用透视,sql-server,pivot,Sql Server,Pivot,我一直在尝试为以下查询创建轴心: select mainstate, customertypeid, count(1) as [counter] from customers group by customertypeid, mainstate 此查询应在每个州显示尽可能多的客户类型,如下所示(订单依据无关紧要): 我尝试使用PIVOT如下(我肯定我错了): 我明白了: customertypeid|type1|type2|type3 1 0 0

我一直在尝试为以下查询创建轴心:

select mainstate, customertypeid, count(1) as [counter] from customers group by customertypeid, mainstate
此查询应在每个州显示尽可能多的客户类型,如下所示(订单依据无关紧要):

我尝试使用PIVOT如下(我肯定我错了):

我明白了:

customertypeid|type1|type2|type3
       1         0     0     0
       2         0     0     0
       3         0     0     0
报告应该是这样的:

State|type1|type2|type3
UT     20    100   200
CA     50    200   500
NY     30    120   300
*Ps:我真的不想回到CASE+SUM语句

非常感谢

这样做可以:

SELECT  mainstate [State],
        [1] type1,
        [2] type2,
        [3] type3
FROM (  SELECT mainstate, customertypeid, COUNT(1) [counter] 
        FROM customers 
        WHERE customertypeid in (1,2,3) 
        AND mainstate != '' 
        GROUP BY customertypeid, mainstate) as NonPivotedDataForReport2
PIVOT(SUM([counter]) FOR customertypeid IN ([1],[2],[3])) AS PivotedDataReport2
这将有助于:

SELECT  mainstate [State],
        [1] type1,
        [2] type2,
        [3] type3
FROM (  SELECT mainstate, customertypeid, COUNT(1) [counter] 
        FROM customers 
        WHERE customertypeid in (1,2,3) 
        AND mainstate != '' 
        GROUP BY customertypeid, mainstate) as NonPivotedDataForReport2
PIVOT(SUM([counter]) FOR customertypeid IN ([1],[2],[3])) AS PivotedDataReport2
这个(可能稍微编辑一下)应该可以在没有case/sum/pivot的情况下为您完成任务。创建临时表,插入起始数据,然后根据客户类型ID的数量动态添加列

declare @s varchar(10), @xx1 varchar(500)

select distinct state into #temp from customers

DECLARE myCursor CURSOR FOR SELECT distinct customertypeid from customers
open MyCursor
FETCH NEXT FROM myCursor into @S
WHILE @@FETCH_STATUS = 0
    begin
        set @xx1 = 'alter table #temp add ['+@s+'] varchar(5)'
        execute sp_executesql @xx1
            set @xx1 = 'update a set a.['+@s+'] = coalesce(b.counter,0) from #temp a, customers b where b.customertypeid = '+@s+' and a.state = b.state'
            execute sp_executesql @xx1
        FETCH NEXT FROM myCursor into @S
    End
Close myCursor
DEALLOCATE myCursor

select * from #temp
这个(可能稍微编辑一下)应该可以在没有case/sum/pivot的情况下为您完成任务。创建临时表,插入起始数据,然后根据客户类型ID的数量动态添加列

declare @s varchar(10), @xx1 varchar(500)

select distinct state into #temp from customers

DECLARE myCursor CURSOR FOR SELECT distinct customertypeid from customers
open MyCursor
FETCH NEXT FROM myCursor into @S
WHILE @@FETCH_STATUS = 0
    begin
        set @xx1 = 'alter table #temp add ['+@s+'] varchar(5)'
        execute sp_executesql @xx1
            set @xx1 = 'update a set a.['+@s+'] = coalesce(b.counter,0) from #temp a, customers b where b.customertypeid = '+@s+' and a.state = b.state'
            execute sp_executesql @xx1
        FETCH NEXT FROM myCursor into @S
    End
Close myCursor
DEALLOCATE myCursor

select * from #temp

如果您知道前面的列,并且客户ID不变,那么Lamak的答案更简单。如果您之前不知道这些列,而且正在进行中,那么动态添加这些列将是一种方法。如果您知道前面的列,并且客户ID不变,那么Lamak的答案是更简单的方法。如果您之前不知道这些列,并且正在运行,那么动态添加这些列将是一种方法。