【发布时间】:2020-01-30 19:08:08
【问题描述】:
输出图片,从这里我想改变顺序
`
ALTER PROCEDURE [dbo].[ResourceLoading]
@ResourceId varchar(40),
@Bucketdate varchar(40),
@EndBucketDate varchar(40)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @Variable nvarchar(max)='',
@ResourceTask nvarchar(max)=''
select @Variable += QUOTENAME(BucketDate)+ ',' from BucketMaster
where BucketDate <= @EndBucketDate and BucketDate >= @Bucketdate
Order by BucketDate;
set @Variable = Left(@Variable,len(@Variable)-1);
set @ResourceTask =
'select * from
(select UsedCapacity as value,BucketDate,ResourceId,''UsedCapacity'' As ''Resources''
from BucketCapacity inner join BucketMaster
on BucketCapacity.BucketId=BucketMaster.BucketId
and ResourceId='''+@ResourceId+''' and BucketDate <= '''+@EndBucketDate+''' and BucketDate >= '''+@BucketDate+'''
union all
select AvailableCapacity as value,BucketDate,ResourceId,''AvailableCapacity'' As ''Resources''
from BucketCapacity inner join BucketMaster
on BucketCapacity.BucketId=BucketMaster.BucketId and ResourceId='''+@ResourceId+'''
and BucketDate <= '''+@EndBucketDate+''' and BucketDate >= '''+@BucketDate+'''
union all
select (cast (round (UsedCapacity *1.00 / AvailableCapacity,3) as float )) as value,BucketDate,ResourceId,''ResourceLoad'' As ''Resources''
from BucketCapacity inner join BucketMaster
on BucketCapacity.BucketId=BucketMaster.BucketId and ResourceId='''+@ResourceId+'''
and BucketDate <= '''+@EndBucketDate+''' and BucketDate >= '''+@BucketDate+''') t
pivot(
sum(value) for BucketDate in ('+@Variable+')) as pivot_table;'
execute sp_executesql @ResourceTask
END
`
在执行此代码时,我在列资源中获得 3 条记录,顺序如下:
Available capacity
Resource Load
used capacity
我想把它改成:
used capacity
Available capacity
Resource Load
谁能帮我解决这个问题
【问题讨论】:
-
添加一些带有预期输出的示例数据。
-
在
ORDER BY子句中使用case表达式。 -
我添加了我的结果图片
标签: sql sql-server