我想通过SQL Server中的存储过程检索总行数和所有值。我有类似下面的东西。但是,它提到它不包含在聚合函数或GROUP BY
子句中。感谢任何回复。感谢。
ALTER PROCEDURE abc.testing
@facility varchar(50),
@date datetime
AS
SELECT count(*), *
FROM ss_table
WHERE _start = @date
And fid IN (SELECT id
FROM ss_facilities
WHERE name = @facility)
答案 0 :(得分:1)
试试这个
SELECT SS.cnt, ss_table.*
FROM ss_table
cross apply (SELECT count(*) cnt
FROM ss_table
WHERE _start = @date
And fid IN (SELECT id
FROM ss_facilities
WHERE name = @facility)
) ss
WHERE _start = @date
And fid IN (SELECT id
FROM ss_facilities
WHERE name = @facility)
答案 1 :(得分:1)
使用像count这样的聚合函数时需要一个group子句。如果ss_table有一个id列,请在其上执行count函数。
它会像这样工作:
SELECT count(id), <explicitly define column names except the id here>
FROM ss_table
WHERE _start = @date
And fid IN (SELECT id FROM ss_facilities WHERE name = @facility)
group by <enter every column except the id from ss_table here>
应该适合你。
答案 2 :(得分:0)
您无法有效地混合和匹配聚合查询和非聚合查询 - 将它们拆分为两个单独的存储过程。
(正在显示此特定错误消息,因为为了返回聚合查询中的列,每列必须位于“分组依据”字段中,或者应用了聚合函数(“max”,“count” “等等。在此查询中,”*“不符合这些条件。)