使用 SQL server Manager 2008 快速简单的一个,得到这个查询:
SELECT sizecode as smaller
from safestore.dbo.SpaceMan_EvoPrices
WHERE SiteId = 'derp' AND Publish = 'Y' AND SizeCode = 'herp'
HAVING PhysicalSize < MIN(PhysicalSize)
在 HAVING 子句中无效,因为它不包含在聚合函数或 GROUP BY 子句中。添加了 group by 但不确定如何将其添加到 group by 子句中才有效。正如你所知道的,我从来没有使用过Having。
有什么想法吗?
编辑:
这个查询是我试图缩短的原始查询,因为它让人头疼:
SELECT TOP 1 (SELECT TOP 1 Sizecode
FROM [safestore].[dbo].[SpaceMan_EvoPrices]
WHERE PhysicalSize < (SELECT TOP 1 PhysicalSize FROM [safestore].[dbo]. [SpaceMan_EvoPrices] WHERE SiteId = 'derp' AND Publish = 'Y' AND SizeCode = 'herp')
AND SiteId = 'derp'
ORDER BY PhysicalSize DESC) AS Smaller,
(SELECT TOP 1 Sizecode
FROM [safestore].[dbo].[SpaceMan_EvoPrices]
WHERE PhysicalSize > (SELECT TOP 1 PhysicalSize FROM [safestore].[dbo]. [SpaceMan_EvoPrices] WHERE SiteId = 'derp' AND Publish = 'Y' AND SizeCode = 'herp')
AND SiteId = 'derp'
ORDER BY PhysicalSize) AS Larger
FROM [safestore].[dbo].[SpaceMan_EvoPrices]