There are three tables
I want to make Graph of Asset Vs Status means How many percentage Assets are available in particular status.
Stored Procedure :
CREATE proc [dbo].[Sp_Asset_VS_Status]
@TimeSpan nvarchar(30),
@Duration int
as
begin
Declare @TotalAsset int;
select @TotalAsset=COUNT(AssetId) from Asset where Isdeleted=0
if @TotalAsset>0
Begin
if @TimeSpan='h'
begin
select (100*Count(distinct AssetId))/12 as TotalPercentage, TrackingStatusName from Tracking
inner join TrackingStatus on TrackingStatus.TrackingStatusId=Tracking.TrackingStatusId
where Tracking.IsDeleted=0 and TrackingStatus.IsDeleted=0
and Tracking.TrackingDate between DATEADD(HOUR,-@Duration,GETDATE()) and GETDATE()
Group by TrackingStatus.TrackingStatusName order by TotalPercentage DESC
End
else if @TimeSpan='d'
begin
select (100*Count(distinct AssetId))/12 as TotalPercentage, TrackingStatusName from Tracking
inner join TrackingStatus on TrackingStatus.TrackingStatusId=Tracking.TrackingStatusId
where Tracking.IsDeleted=0 and TrackingStatus.IsDeleted=0
and Tracking.TrackingDate between DATEADD(DAY,-@Duration,GETDATE()) and GETDATE()
Group by TrackingStatus.TrackingStatusName order by TotalPercentage DESC
End
else if @TimeSpan='w'
begin
select (100*Count(distinct AssetId))/12 as TotalPercentage, TrackingStatusName from Tracking
inner join TrackingStatus on TrackingStatus.TrackingStatusId=Tracking.TrackingStatusId
where Tracking.IsDeleted=0 and TrackingStatus.IsDeleted=0
and Tracking.TrackingDate between DATEADD(WEEK,-@Duration,GETDATE()) and GETDATE()
Group by TrackingStatus.TrackingStatusName order by TotalPercentage DESC
End
else if @TimeSpan='m'
begin
select (100*Count(distinct AssetId))/12 as TotalPercentage, TrackingStatusName from Tracking
inner join TrackingStatus on TrackingStatus.TrackingStatusId=Tracking.TrackingStatusId
where Tracking.IsDeleted=0 and TrackingStatus.IsDeleted=0
and Tracking.TrackingDate between DATEADD(MONTH,-@Duration,GETDATE()) and GETDATE()
Group by TrackingStatus.TrackingStatusName order by TotalPercentage DESC
End
else if @TimeSpan='y'
begin
select (100*Count(distinct AssetId))/12 as TotalPercentage, TrackingStatusName from Tracking
inner join TrackingStatus on TrackingStatus.TrackingStatusId=Tracking.TrackingStatusId
where Tracking.IsDeleted=0 and TrackingStatus.IsDeleted=0
and Tracking.TrackingDate between DATEADD(YEAR,-@Duration,GETDATE()) and GETDATE()
Group by TrackingStatus.TrackingStatusName order by TotalPercentage DESC
End
Else
begin
select (100*Count(distinct AssetId))/12 as TotalPercentage, TrackingStatusName from Tracking
inner join TrackingStatus on TrackingStatus.TrackingStatusId=Tracking.TrackingStatusId
where Tracking.IsDeleted=0 and TrackingStatus.IsDeleted=0
Group by TrackingStatus.TrackingStatusName order by TotalPercentage DESC
End
End
End
So I am getting result in descending order.
I want to display first 6 rows from query. First 5 rows as it is and 6 row is sum of all Total Percentage starting from 6th row as shown in figure.
So after fifth row, How to merge rows into one row and display as others???
Since the Status field value from the 6th record onwards starts with the word "status", you can use the following statement for your 6th/last query in your SQL block and that should give you 1 row that adds up the TotalPercentages for all "status" rows.
begin
select (100*Count(distinct AssetId))/12 as TotalPercentage
,SUBSTRING(UPPER(TrackingStatusName), 1, 6)
from Tracking
inner
join TrackingStatus
on TrackingStatus.TrackingStatusId=Tracking.TrackingStatusId
where Tracking.IsDeleted=0 and TrackingStatus.IsDeleted=0
Group
by SUBSTRING(UPPER(TrackingStatus.TrackingStatusName), 1, 6)
order
by TotalPercentage DESC
End
In response to the comment, the below updated SQL should work, it should 'Others' as the second column for the last SQL
begin
select (100*Count(distinct AssetId))/12 as TotalPercentage
,SUBSTRING(STUFF(TrackingStatusName, PATINDEX('%______%', TrackingStatusName), 6, 'Others'), 1, 6)
from Tracking
inner
join TrackingStatus
on TrackingStatus.TrackingStatusId=Tracking.TrackingStatusId
where Tracking.IsDeleted=0 and TrackingStatus.IsDeleted=0
Group
by SUBSTRING(STUFF(TrackingStatusName, PATINDEX('%______%', TrackingStatusName), 6, 'Others'), 1, 6)
order
by TotalPercentage DESC
End
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.