EXISTS 不引入子查询时,select 列表中只能指定一个表达式。 - SQL 服务器
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. - SQL Server
我正在尝试创建一个存储过程,我使用 SQL 服务器编写了这段代码,但出现错误
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
不知道怎么解决
CREATE PROCEDURE Calculus
AS
BEGIN
SELECT
-- **** Media (Average) **** --
(SELECT CAST(AVG(CAST(CC AS NUMERIC)) AS VARCHAR(MAX))
FROM CHALLENGE AS Media),
-- **** Mediana **** --
(SELECT CASE WHEN COUNT(CC) % 2 = 0
THEN (SELECT TOP 1 ((SELECT TOP 1 CC
FROM (SELECT TOP 50 PERCENT CC FROM CHALLENGE ORDER BY CC ASC) AS X
ORDER BY CC DESC)
+ (SELECT TOP 1 CC
FROM (SELECT TOP 50 PERCENT CC FROM CHALLENGE ORDER BY CC DESC) AS Y
ORDER BY CC ASC)) / 2)
ELSE (SELECT TOP 1 CC
FROM (SELECT TOP 50 PERCENT CC FROM CHALLENGE ORDER BY CC) AS X
ORDER BY CC DESC)
END AS Mediana
FROM CHALLENGE),
-- **** Moda **** --
(SELECT TOP 1 CC, COUNT(CC) AS Veces
FROM CHALLENGE
GROUP BY CC
ORDER BY COUNT(CC)) AS Moda,
-- **** Min **** --
MIN(CC) AS Minimo,
-- **** Max **** --
MAX(CC) AS Maximo
FROM
CHALLENGE
END
为了清晰起见,您应该从重构存储过程开始。
下面的示例供参考
CREATE PROCEDURE Calculus
AS
BEGIN
DECLARE @Media varchar(max)
DECLARE @Mediana varchar(max)
DECLARE @topbottom numeric
DECLARE @bottomtop numeric
DECLARE @Moda varchar(max)
SELECT
@Media= Cast(Avg(Cast (CC AS numeric)) AS varchar(max))
FROM CHALLENGE
SELECT
TOP 50 PERCENT CC
INTO #TOP
FROM CHALLENGE
ORDER BY CC ASC
SELECT
TOP 50 PERCENT CC
INTO #BOTTOM
FROM CHALLENGE
ORDER BY CC DESC
SELECT
TOP 1 @topbottom=CAST(CC as numeric)
FROM #TOP X
ORDER BY CC DESC
SELECT
TOP 1 @bottomtop=CAST(CC as numeric)
FROM #BOTTOM Y
ORDER BY CC ASC
SELECT @Mediana=
CASE
WHEN Count(CC) % 2 = 0 THEN (@bottomtop+@topbottom)/2
ELSE @topbottom
END
FROM CHALLENGE
SELECT TOP 1
@Moda= CC
FROM
(
SELECT CC,Count(CC) AS Veces
FROM CHALLENGE
GROUP BY CC
)
ORDER BY Veces
SELECT
Media=@Media,
Mediana=@Mediana,
Moda= @Moda,
Minimo=Min(CC),
Maximo=Max(CC)
FROM CHALLENGE
drop table #top
drop table #bottom
END
这部分查询导致错误。
这是一个子查询,您正在尝试从中 return 2 列(这会导致错误)。
TOP 1 CC
和 COUNT(CC)
(SELECT TOP 1 CC
,COUNT(CC) AS Veces
FROM CHALLENGE
GROUP BY CC
ORDER BY COUNT(CC)) AS Moda
不确定要建议什么,因为您肯定是唯一知道您打算从这里得到什么的人。
您应该更改查询并将每个子查询放入(OUTER APPLY)join
像这样:
CREATE PROCEDURE Calculus
AS
BEGIN
SELECT
MediaTableName.MediaFieldName
FROM
CHALLENGE
OUTER APPLY
(SELECT CAST(AVG(CAST(CC AS NUMERIC)) AS VARCHAR(MAX)) as MediaFieldName
FROM CHALLENGE AS t1
WHERE t1.IDentity = CHALLENGE.IDentity /* this line create join condition and its up to you how use it*/
) as MediaTableName
END
对于每个子查询使用一个外部 aplly 连接
您可以删除 where 子句行
我正在尝试创建一个存储过程,我使用 SQL 服务器编写了这段代码,但出现错误
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
不知道怎么解决
CREATE PROCEDURE Calculus
AS
BEGIN
SELECT
-- **** Media (Average) **** --
(SELECT CAST(AVG(CAST(CC AS NUMERIC)) AS VARCHAR(MAX))
FROM CHALLENGE AS Media),
-- **** Mediana **** --
(SELECT CASE WHEN COUNT(CC) % 2 = 0
THEN (SELECT TOP 1 ((SELECT TOP 1 CC
FROM (SELECT TOP 50 PERCENT CC FROM CHALLENGE ORDER BY CC ASC) AS X
ORDER BY CC DESC)
+ (SELECT TOP 1 CC
FROM (SELECT TOP 50 PERCENT CC FROM CHALLENGE ORDER BY CC DESC) AS Y
ORDER BY CC ASC)) / 2)
ELSE (SELECT TOP 1 CC
FROM (SELECT TOP 50 PERCENT CC FROM CHALLENGE ORDER BY CC) AS X
ORDER BY CC DESC)
END AS Mediana
FROM CHALLENGE),
-- **** Moda **** --
(SELECT TOP 1 CC, COUNT(CC) AS Veces
FROM CHALLENGE
GROUP BY CC
ORDER BY COUNT(CC)) AS Moda,
-- **** Min **** --
MIN(CC) AS Minimo,
-- **** Max **** --
MAX(CC) AS Maximo
FROM
CHALLENGE
END
为了清晰起见,您应该从重构存储过程开始。
下面的示例供参考
CREATE PROCEDURE Calculus
AS
BEGIN
DECLARE @Media varchar(max)
DECLARE @Mediana varchar(max)
DECLARE @topbottom numeric
DECLARE @bottomtop numeric
DECLARE @Moda varchar(max)
SELECT
@Media= Cast(Avg(Cast (CC AS numeric)) AS varchar(max))
FROM CHALLENGE
SELECT
TOP 50 PERCENT CC
INTO #TOP
FROM CHALLENGE
ORDER BY CC ASC
SELECT
TOP 50 PERCENT CC
INTO #BOTTOM
FROM CHALLENGE
ORDER BY CC DESC
SELECT
TOP 1 @topbottom=CAST(CC as numeric)
FROM #TOP X
ORDER BY CC DESC
SELECT
TOP 1 @bottomtop=CAST(CC as numeric)
FROM #BOTTOM Y
ORDER BY CC ASC
SELECT @Mediana=
CASE
WHEN Count(CC) % 2 = 0 THEN (@bottomtop+@topbottom)/2
ELSE @topbottom
END
FROM CHALLENGE
SELECT TOP 1
@Moda= CC
FROM
(
SELECT CC,Count(CC) AS Veces
FROM CHALLENGE
GROUP BY CC
)
ORDER BY Veces
SELECT
Media=@Media,
Mediana=@Mediana,
Moda= @Moda,
Minimo=Min(CC),
Maximo=Max(CC)
FROM CHALLENGE
drop table #top
drop table #bottom
END
这部分查询导致错误。
这是一个子查询,您正在尝试从中 return 2 列(这会导致错误)。
TOP 1 CC
和 COUNT(CC)
(SELECT TOP 1 CC ,COUNT(CC) AS Veces FROM CHALLENGE GROUP BY CC ORDER BY COUNT(CC)) AS Moda
不确定要建议什么,因为您肯定是唯一知道您打算从这里得到什么的人。
您应该更改查询并将每个子查询放入(OUTER APPLY)join
像这样:
CREATE PROCEDURE Calculus
AS
BEGIN
SELECT
MediaTableName.MediaFieldName
FROM
CHALLENGE
OUTER APPLY
(SELECT CAST(AVG(CAST(CC AS NUMERIC)) AS VARCHAR(MAX)) as MediaFieldName
FROM CHALLENGE AS t1
WHERE t1.IDentity = CHALLENGE.IDentity /* this line create join condition and its up to you how use it*/
) as MediaTableName
END
对于每个子查询使用一个外部 aplly 连接 您可以删除 where 子句行