Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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中,尝试根据计数结果更新表列时遇到了问题_Sql_Select_Count - Fatal编程技术网

在SQL中,尝试根据计数结果更新表列时遇到了问题

在SQL中,尝试根据计数结果更新表列时遇到了问题,sql,select,count,Sql,Select,Count,我正在建立一个生产数据统计分析表。我提取了所有不同的案例,并构建了一个计数,以找出每个独特案例在表中出现的次数 我的问题出现在尝试使用计数结果更新表的“Number”列时 这是我到目前为止的代码 TRUNCATE TABLE WeightData INSERT INTO WeightData (WeightType,WeightIncrement) SELECT DISTINCT OutboardWeightTable,OutboardAmount FROM ProductionDa

我正在建立一个生产数据统计分析表。我提取了所有不同的案例,并构建了一个计数,以找出每个独特案例在表中出现的次数

我的问题出现在尝试使用计数结果更新表的“Number”列时

这是我到目前为止的代码

TRUNCATE TABLE WeightData

INSERT  INTO WeightData (WeightType,WeightIncrement)
SELECT  DISTINCT OutboardWeightTable,OutboardAmount
FROM    ProductionData
WHERE   [Datetime] between '180821_150000' and '180822_000000' and (OutboardAmount > 0)

--UPDATE WeightData
--SET Number = (
    SELECT  COUNT(*)
    FROM    WeightData a
            inner join ProductionData b
                ON (a.WeightType = b.OutboardWeightTable) and (a.WeightIncrement = b.OutboardAmount)
    WHERE   [Datetime] between '180821_150000' and '180822_000000' and (OutboardAmount > 0)
    GROUP   BY a.WeightType,a.WeightIncrement
--)
我已经注释掉了不起作用的行,但是当我尝试运行查询时,我得到了以下消息。我已经尝试了很多不同的方法来做到这一点,但我觉得这是我的尝试

Msg 512,级别16,状态1,过程TestProcedure,第24行子查询 返回的值超过1个。当子查询 如下=,!=,=或者当子查询用作 表情

当我在注释这些行之后运行代码时,下面是表和结果屏幕


我想您只需要一个相关的子查询:

UPDATE WeightData
    SET Number = (SELECT COUNT(*)
                  FROM ProductionData pd
                  WHERE wd.WeightType = p.OutboardWeightTable and  
                        wd.WeightIncrement = pd.OutboardAmount AND
                        pd.OutboardAmount > 0 AND
                        pd.[Datetime] between '180821_150000' and '180822_000000'
                )
    FROM WeightData wd;

我将b和p修改为pd,它可以工作!!我在这上面花了很长时间,非常感谢!