Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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 2017中显示子查询错误_Sql_Sql Server_Stored Procedures_Subquery_Sql Server 2017 - Fatal编程技术网

此存储过程在SQL Server 2017中显示子查询错误

此存储过程在SQL Server 2017中显示子查询错误,sql,sql-server,stored-procedures,subquery,sql-server-2017,Sql,Sql Server,Stored Procedures,Subquery,Sql Server 2017,此查询的输出是一个错误: 子查询返回了多个值。当子查询在=、!=、=或者当子查询用作表达式时 我还在SQL Server 2017中使用相同的查询实现了与此相同的过程,只是表名不同,而且它正在工作,但我不知道为什么此过程不工作。类似这样的方法应该更好: ALTER PROCEDURE daily_routine AS BEGIN INSERT INTO temp_stats (i_count, r_count, s_count, w_count, n_count, z_count, t_

此查询的输出是一个错误:

子查询返回了多个值。当子查询在=、!=、=或者当子查询用作表达式时


我还在SQL Server 2017中使用相同的查询实现了与此相同的过程,只是表名不同,而且它正在工作,但我不知道为什么此过程不工作。

类似这样的方法应该更好:

ALTER PROCEDURE daily_routine
AS
BEGIN
    INSERT INTO temp_stats (i_count, r_count, s_count, w_count, n_count, z_count, t_count, 
                            p_hour, p_location, p_location_count, 
                            p_location_percentage, p_hour_percentage, d_date)
    VALUES (
       (SELECT SUM(i_count) as i_count FROM temp_h_stats),
       (SELECT SUM(r_count) as r_count FROM temp_h_stats),
       (SELECT SUM(s_count) as s_count FROM temp_h_stats),
       (SELECT SUM(w_count) as w_count FROM temp_h_stats),
       (SELECT SUM(n_count) as n_count FROM temp_h_stats),
       (SELECT SUM(z_count) as z_count FROM temp_h_stats),
       (SELECT SUM(t_count) as t_count FROM temp_h_stats),
       (SELECT time_range AS p_hour FROM temp_h_stats 
        WHERE t_count = (SELECT MAX(t_count) FROM temp_h_stats)),
       (SELECT p_location AS p_location FROM temp_hourly_stats 
        WHERE t_count = (SELECT MAX(t_count) FROM temp_hourly_stats)),
       (SELECT p_location_count AS p_location_count FROM temp_h_stats 
        WHERE p_location_count = (SELECT MAX(p_location_count) FROM temp_h_stats)),
       (SELECT MAX(p_location_percentage) AS p_location_percentage FROM temp_h_stats),
       (SELECT (MAX(t_count) * 100) / SUM(t_count) AS p_hour_percentage FROM temp_h_stats),
       (SELECT DATEADD(DD, -1,CURRENT_TIMESTAMP) AS recorded_date))
END
您的主要问题是使用了太多的子查询,以防一次读取一个表就可以执行此操作。其中一项:

ALTER Procedure daily_routine
AS
BEGIN


DECLARE @MaxCounT_h_stats BIGINT
       ,@MaxCounT_hourly_states BIGINT
       ,@MaxLocation_Count BIGINT;

SET @MaxCounT_h_stats = (select max(t_count) from temp_h_stats);
SET @MaxCounT_hourly_states = (select max(t_count) from temp_hourly_stats);
SET @MaxLocation_Count = (select max(p_location_count) from temp_h_stats);

INSERT INTO temp_stats(i_count,r_count,s_count,w_count,n_count,z_count,t_count,p_hour,p_location,p_location_count,p_location_percentage,p_hour_percentage,d_date)
SELECT SUM(i_count) as i_count
      ,SUM(r_count) as r_count
      ,SUM(s_count) as s_count
      ,SUM(w_count) as w_count
      ,SUM(n_count) as n_count
      ,SUM(z_count) as z_count
      ,SUM(t_count) as t_count
      ,MAX(IIF(t_count = @MaxCounT_h_stats, time_range, NULL) as p_hour
      ,MAX(IIF(t_count = @MaxCounT_hourly_states, p_location, NULL) as p_location
      ,MAX(IIF(p_location_count = @MaxLocation_Count, p_location_count, NULL) as p_location_count
      ,MAX(p_location_percentage) as p_location_percentage
      ,(MAX(t_count)*100)/SUM(t_count) as p_hour_percentage
      ,DATEADD(DD, -1,CURRENT_TIMESTAMP) as recorded_date)
FROM temp_h_stats

END

返回的值比一个值多,因此出现此错误。如果您不喜欢使用my variation,只需在上述语句中添加聚合函数。

您的问题显然是获取最大值的子查询返回多个值

下面的代码假设
temp\u hourly\u stats
实际上是
temp\u h\u stats
。代码可以很容易地调整以处理两个不同的表,但是名称看起来非常相似

我建议使用单个查询和窗口功能:

(SELECT time_range as p_hour from temp_h_stats where t_count= (select max(t_count) from temp_h_stats)),
(SELECT p_location as p_location from temp_hourly_stats where t_count= (select max(t_count) from temp_hourly_stats)),
(SELECT p_location_count as p_location_count from temp_h_stats where p_location_count = (select max(p_location_count) from temp_h_stats)),
请注意,我简化了这个逻辑:

INSERT INTO temp_stats (i_count, r_count, s_count, w_count, n_count, z_count, t_count ,p_hour, p_location, p_location_count, p_location_percentage, p_hour_percentage, d_date)
    SELECT SUM(i_count) as i_count,
           SUM(r_count) as r_count,
           SUM(s_count) as s_count,
           SUM(w_count) as w_count,
           SUM(n_count) as n_count,
           SUM(z_count) as z_count
           SUM(t_count) as t_count,
           MAX(CASE WHEN seqnum_t_count = 1 THEN time_range END) as p_hour,
           MAX(CASE WHEN seqnum_t_count = 1 THEN p_location END) as p_location
           MAX(p_location_count) as p_location_count
           MAX(p_location_percentage) as p_location_percentage
           (MAX(t_count) * 100) / SUM(t_count) as p_hour_percentage
           DATEADD(day, -1, CURRENT_TIMESTAMP) as recorded_date)
    FROM (SELECT ths.*,
                 ROW_NUMBER() OVER (ORDER BY t_count DESC) as seqnum_t_count
          FROM temp_h_stats ths
         ) t;
致:


为什么从同一个表中选择这么多?这不是同一个表我从一个表中获取数据的总和并插入到另一个表中再检查一次样本数据和期望的结果将真正阐明您想要做的事情。我能应用不同的吗???@UtkarshMishra,这取决于-如果所有的值都相同,它可以工作-如果您有两个不同的值,查询将再次失败。@UtkarshMishra如果您想从此查询中获取所有值,可以使用
STRING\u AGG
-它将执行值的串联。很抱歉,不是这样,我如何在这里使用Distinct?它不将IFF作为sql Server的函数这一个很有用
  (SELECT p_location_count AS p_location_count
   FROM temp_h_stats 
   WHERE p_location_count = (SELECT MAX(p_location_count) FROM temp_h_stats)
  ),
MAX(p_location_count)