[英]How display rows of data in to columns in SQL Server
我正在寻找一种方法来显示表中的数据,该表为 1 个电话号码返回 3 个(这个数字可能会有所不同)不同的记录。
结果表示例... https://i.stack.imgur.com/gB0db.png
但是,我想做的是这样显示它,基于按日期和时间排序的呼叫等级.... https://i.stack.imgur.com/dIJvZ.png
如果您知道所需的列数,则可以使用条件聚合:
select ani,
max(case when rank = 1 then calldate end) as calldate_1,
max(case when rank = 1 then calltime end) as calltime_1,
max(case when rank = 1 then calltype end) as calltype_1,
max(case when rank = 2 then calldate end) as calldate_2,
max(case when rank = 2 then calltime end) as calltime_2,
max(case when rank = 2 then calltype end) as calltype_2,
max(case when rank = 3 then calldate end) as calldate_3,
max(case when rank = 3 then calltime end) as calltime_3,
max(case when rank = 3 then calltype end) as calltype_3
from t
group by ani;
这不是您想要的,但可能有用:
返回两列(每个电话号码一行):ANI 和一个文本列,其中 rest o 数据连接。
SELECT
ANI,
STRING_AGG(
'[CallDate: ' + CONVERT(varchar, CallDate, 103) + ', '
'CallTime: ' + CONVERT(varchar, CallTime, 108) + ', '
'CallTypeID: ' + CAST(CallTypeID AS varchar) + ']', ' ') AS Data
FROM t
GROUP BY ANI;
如果列类型是一些文本,则不需要 CAST 或 CONVERT。
这是一个带有游标和动态脚本的解决方案 该解决方案可以用于每天的调用次数。
数据存储在表调用中
您期望的数据存储在表Calls_Columns中
2)创建一个表Clls_columns,第一列=ANI。
3) 然后对于 i=1 到 N,添加列 calldate_i,calltime_i...
4) 并使用 with CTE 指令更新 CALLS 表中的每一列。
希望能帮助到你:
if exists(select 1 from sysobjects where name='CALLS') drop table calls
if exists(select 1 from sysobjects where name='Calls_column') drop table Calls_column
CREATE TABLE CALLS (ani varchar(20),callDate date,callTime time, calltypeID int,CallRank int)
insert into CALLS values
('01234567890','01/04/2021','12:35:25','111','1'),
('01234567890','01/04/2021','13:35:10','111','2'),
('01234567890','01/04/2021','15:40:00','111','3'),
('71234567890','02/04/2021','12:35:25','112','1'),
('71234567890','02/04/2021','13:35:10','112','2'),
('71234567890','02/04/2021','15:40:00','112','3'),
('71234567890','02/04/2021','17:40:00','112','4')
declare @max as int
set @max=(
select top 1 count(1) N from CALLS group by calldate order by count(1) desc)
create table Calls_column(ani varchar(20))
declare @script as varchar(max)
declare @i as int
set @i=1
while @i <=@max
begin
set @script='alter table Calls_column add callDate_'+CAST(@i as varchar(2))+' date'
exec(@script)
set @script='alter table Calls_column add callTime_'+CAST(@i as varchar(2))+' Time'
exec(@script)
set @script='alter table Calls_column add calltypeID_'+CAST(@i as varchar(2))+' int'
exec(@script)
set @i=@i+1
end;
declare mycursor cursor for
with cte as(
select ani,calldate,calltime,calltypeID,ROW_NUMBER() over(partition by ani order by calltypeid) n from CALLS)
select * from cte
declare @ani as varchar(20)
declare @calldate as date
declare @calltime as time
declare @calltypeID as int
declare @o as int
open mycursor
fetch mycursor into @ani,@calldate,@calltime,@calltypeID,@o
while @@FETCH_STATUS=0
begin
set @script='if not exists(select 1 from Calls_column where ani='''+@ani+''') insert Calls_column(ani)
values('''+@ani+''')'
print @script
exec(@script)
set @script='if exists(select 1 from Calls_column where ani='''+@ani+''') update Calls_column
set calldate_'+CAST(@O AS varchar(2))+'='''+cast(@calldate as varchar(20))+''' where ani='''+@ani+''''
print @script
exec(@script)
set @script='if exists(select 1 from Calls_column where ani='''+@ani+''') update Calls_column
set calltime_'+CAST(@O AS varchar(2))+'='''+cast(@calltime as varchar(20))+''' where ani='''+@ani+''''
print @script
exec(@script)
set @script='if exists(select 1 from Calls_column where ani='''+@ani+''') update Calls_column
set calltypeid_'+CAST(@O AS varchar(2))+'='''+cast(@calltypeid as varchar(20))+''' where ani='''+@ani+''''
print @script
exec(@script)
fetch mycursor into @ani,@calldate,@calltime,@calltypeID,@o
end
close mycursor
deallocate mycursor
select * from Calls_column
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.