Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 在SQL Server中计算总计_Sql Server 2008 - Fatal编程技术网

Sql server 2008 在SQL Server中计算总计

Sql server 2008 在SQL Server中计算总计,sql-server-2008,Sql Server 2008,我在计算案例陈述中的运行总数时遇到问题。 我有两个表@report和@question以及两个变量@countCurrent和@countsuggered 根据@report表中的数字与静态值或@question表中的值进行比较,我需要增加@countCurrent或@countsuggered 这是我到目前为止得到的结果,但不是在任一列中得到5的组合,而是得到0/1。我认为这是加入的一部分,但我看不出是什么 declare @MinSuccessRate float, @cou

我在计算案例陈述中的运行总数时遇到问题。 我有两个表
@report
@question
以及两个变量
@countCurrent
@countsuggered

根据
@report
表中的数字与静态值或
@question
表中的值进行比较,我需要增加
@countCurrent
@countsuggered

这是我到目前为止得到的结果,但不是在任一列中得到5的组合,而是得到0/1。我认为这是加入的一部分,但我看不出是什么

declare @MinSuccessRate float,
        @countCurrent int,
        @countSuggested int

declare @report table
(
    intID   int identity(1,1),
    intReportID int,
    intParticipantID int,
    acceptable float,
    optimum float
)
insert @report
    select 1,1,.25,.75 union all
    select 1,2,.45,.75 union all
    select 1,3,.35,.75 union all
    select 1,4,.55,.75 union all
    select 1,5,.65,.75

declare @question table
(
    intID   int identity(1,1),
    intParticipantID    int,
    answer  float
)

insert @question
select 1,35 union all
select 1,55 union all
select 1,65 union all
select 1,75 union all
select 1,85

SET @MinSuccessRate=0.75
SET @countCurrent=0
SET @countSuggested=0

UPDATE @report
SET @countCurrent= 
    CASE WHEN acceptable>=@MinSuccessRate 
        THEN @countCurrent+1 
        ELSE 0 
        END,
    @countSuggested=
    CASE WHEN optimum*100 >=q.answer 
        THEN @countSuggested+1 
        ELSE 0 
        END
FROM @report pr 
    INNER JOIN @question q 
    ON pr.intParticipantID=q.intParticipantID
WHERE pr.intReportID=1

select @countCurrent [Current],@countSuggested [Suggested]

提前谢谢

您可以在多个表
UPDATE
中选中此项,每个目标记录最多只能更新一次(无论联接返回多少次)

但是,您根本不需要在此处更新:

SELECT  @countCurrent =
        SUM
        (
        CASE
        WHEN acceptable >= @MinSuccessRate 
        THEN
                1
        ELSE
                0
        END
        ),
        @countSuggested =
        SUM
        (
        CASE
        WHEN optimum * 100 >= q.answer 
        THEN
                1
        ELSE
                0
        END
        )
FROM    @report pr 
JOIN    @question q 
ON      q.intParticipantID = pr.intParticipantID
WHERE   pr.intReportID = 1

可能重复为什么称之为运行总计?这是一个简单的总数,除非你或我遗漏了什么。不,你可能是对的,这不是最好的标题,但幸运的是,它已解决如下:)