我想查询返回如图所示,它几乎解释了我需要什么:
我的存储过程会返回,但只是基本的:
CREATE PROCEDURE [dbo].[p_TargetStatus]
AS
BEGIN
--NOT SURE IF NEEDED
Declare @OnTime float
Declare @Overdue float
Declare @AllON float
Declare @AllOV float
Declare @AnswON float
Declare @AnswOV float
--END NOT SURE IF NEEDED
SET NOCOUNT ON;
IF OBJECT_ID('#tmp1') IS NOT NULL
DROP TABLE #tmp1
CREATE TABLE #tmp1
(
AUD_ID BIGINT,
RowCounter BIGINT,
DistinctCounter BIGINT,
NACounter BIGINT,
Total BIGINT,
[Status] VARCHAR(MAX)
)
INSERT INTO #tmp1 EXEC [p_GetCompleteIncompleteNaOverviewSCORE]
--NOT SURE IF NEEDED
Set @OnTime = (Select Count([Status])
From #tmp1
Where [Status] = 'OPEN')
Set @AllON = (SELECT COUNT([Status])
From #tmp1)
SET @AnswON = @OnTime/@AnswON
Set @Overdue = (Select Count([Status])
From #tmp1
Where [Status] = 'CLOSED')
Set @AllOV = (SELECT COUNT([Status])
From #tmp1)
SET @AnswOV = @Overdue/@AllOV
--END NOT SURE IF NEEDED
SELECT COUNT(*) AS [Count],[Target Status] AS TargetStatus
FROM (
SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101)) < CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,GETDATE()), 101))
THEN 'Over Due'
ELSE 'On Time' END AS [Target Status]
FROM #tmp1 t1 INNER JOIN dbo.Audit t2
ON t1.AUD_ID = t2.AUD_ID
WHERE t1.[Status] = 'Open') DER
GROUP BY [Target Status]
END
GO
我试过但有点失败。我可以弄清楚但这个百分比对我来说似乎很奇怪?
答案 0 :(得分:0)
使用类似的东西:
select sum
( case
when -- open expression
then 1
else 0
end
)
/ count(*)
percentage_open
from table
group
by status
答案 1 :(得分:0)
我对开放的表达方式感兴趣,因为我需要一个案件,一个案件需要另一个案件
我做的几乎一样,就像这样一个完整的例子
create table #events (reqdate int, Opened tinyint)
insert into #events values (5,1)
insert into #events values (6,0)
insert into #events values (3,1)
insert into #events values (2,0)
insert into #events values (5,1)
select 'Closed : ' + RTRIM(SUM(case when Opened = 0 then 1 else 0 end)) + ' out of ' + rtrim(count(*)),
cast(SUM(case when Opened = 0 then 1 else 0 end) as float) / COUNT(*)
from #events
union
select 'Open : ' + RTRIM(SUM(case when Opened = 1 then 1 else 0 end)) + ' out of ' + rtrim(count(*)),
cast(SUM(case when Opened = 1 then 1 else 0 end) as float) / COUNT(*)
from #events
drop table #events
这里是没有联合的方法的副本
create table #events (reqdate int, Opened tinyint)
insert into #events values (5,1)
insert into #events values (6,0)
insert into #events values (3,1)
insert into #events values (2,0)
insert into #events values (5,1)
select 'Closed : ' + RTRIM(SUM(case when Opened = blabla then 1 else 0 end)) + ' out of ' + rtrim(count(*)),
cast(SUM(case when Opened = blabla then 1 else 0 end) as float) / COUNT(*)
from #events
cross join (select distinct opened as blabla from #events) p
group by blabla
drop table #events