我有一个返回
的过程|On time: 1 of 45 |1 |
|Over due: 44 of 45|44|
我的程序:
CREATE PROCEDURE [dbo].[p_TargetStatus]
AS
BEGIN
--Try
Declare @OnTime float
Declare @Overdue float
Declare @AllON float
Declare @AllOV float
Declare @AnswON float
Declare @AnswOV float
SET NOCOUNT ON;
IF OBJECT_ID('#tmp1') IS NOT NULL
DROP TABLE #tmp1
SET NOCOUNT ON
CREATE TABLE #tmp1
(
AUD_ID BIGINT,
RowCounter BIGINT,
DistinctCounter BIGINT,
NACounter BIGINT,
Total BIGINT,
[Status] VARCHAR(MAX)
)
INSERT INTO #tmp1 EXEC [p_GetCompleteIncompleteNaOverviewSCORE]
--TRY 2
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
--TRY 2
DECLARE @Total AS INT
SELECT @Total = COUNT(*)
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] in ('Closed','Open')) DER
DECLARE @TotalTEST Float
DECLARE @OPEN Float
DECLARE @CLOSED FLOAT
DECLARE @PERC FLOAT
SELECT ([Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255)) + ' of ' + CAST(@Total AS NVARCHAR(255))) AS TargetStatus, COUNT(*)/@Total AS [Count]
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] in ('Closed','Open')) DER
GROUP BY [Target Status]
END
GO
我尝过这样的话:
DECLARE @TotalTEST Float
DECLARE @OPEN Float
DECLARE @CLOSED FLOAT
DECLARE @PERC FLOAT
Set @TotalTEST = (Select Count(*)from #tmp1)
PRint @TotalTEST
SET @OPEN = (SELECT COUNT(*)from #tmp1 Where [Status] = 'OPEN')
PRINT @OPEN
SET @CLOSED = (SELECT COUNT(*)from #tmp1 Where [Status] = 'Closed' AND )
PRINT @CLOSED
百分比应该(我认为)在这一行AS TargetStatus, *COUNT(*)/@Total* AS [Count]
结果应该是这样的;
|On time: 1 of 45 |0.02| (Roughly)
|Over due: 44 of 45|0.98|
代码很混乱,但我试过除以总数,我试过try2部分声明然后做一些但失败了。
答案 0 :(得分:1)
如果你除了两个整数,你总是得到0。
尝试这种方法:
declare @x int
declare @y int
set @x=1
set @y=45
select round(cast(@x as Float)/(CAST(@y as float)),2)
基本上,你需要使用浮点数或小数
AS TargetStatus, cast( COUNT(*) as float)/cast(@Total as float) AS totPct
答案 1 :(得分:1)
在CAST((CAST(COUNT(*) AS DECIMAL(5,2)) / CAST(@Total AS DECIMAL(5,2)) AS DECIMAL(5,2)) AS [Count]
COUNT(*)/@Total* AS [Count]