4
什麼讓下面的T-SQL查詢有效的最佳途徑:集團由一個子查詢
select
count(*),
(... a subquery that returns one result ...) as [Bar]
from Foo foo
group by [Bar]
什麼讓下面的T-SQL查詢有效的最佳途徑:集團由一個子查詢
select
count(*),
(... a subquery that returns one result ...) as [Bar]
from Foo foo
group by [Bar]
SELECT COUNT(*),
(SELECT TOP 1 name
FROM sys.objects
ORDER BY object_id%number) name
FROM master..spt_values
WHERE number > 0
GROUP BY (SELECT TOP 1 name
FROM sys.objects
ORDER BY object_id%number)
給出了錯誤
不能使用聚合或子查詢 GROUP BY子句列表中用於組 的表達式。
也許別人可以回答爲什麼這是不允許的。一對夫婦的方式既有效
SELECT COUNT(*),
oa.name
FROM master..spt_values
OUTER APPLY (SELECT TOP 1 name
from sys.objects
ORDER BY object_id%number) oa
WHERE number > 0
GROUP BY oa.name
和
;WITH T AS
(
SELECT number,
(SELECT TOP 1 name
from sys.objects
ORDER BY object_id%number) name
FROM master..spt_values
WHERE number > 0
)
SELECT COUNT(*),
name
FROM T
GROUP BY name
請解釋一下什麼是查詢應該做的事情。 – 2011-05-09 09:09:26
它應該是由子查詢的結果分組。我已經簡化了它,所以你不必閱讀這麼多。 – cbp 2011-05-09 09:21:16
@cbp - 相信我,馬丁知道*那*很多。也許你可以發佈一些樣本數據和預期的輸出。它會清除事情。對於初學者來說,我看不出有什麼理由將你的辦公桌包含進去。它對我來說看起來多餘。 – 2011-05-09 09:23:54