3

使用 SQL Server 2000 是否有更简单/更清洁的方法来执行此操作?

每隔几天我就需要这样做。

我先看看我们总共有多少条记录:

SELECT COUNT(*) FROM MyTable

然后我列出某个字段的所有长度:

SELECT LEN(MyText)
FROM MyTable
ORDER BY LEN(MyText) ASC

然后我需要向下滚动 1/3... 并记下值。然后我需要向下滚动 2/3... 并记下值。最后是最后一个值。

我需要找出 x、y 和 z:

 33% of the records have this field with a length under x bytes
 66% of the records have this field with a length under y bytes
100% of the records have this field with a length under z bytes
4

2 回答 2

3

在 SQL 2005 中,您可能会为此使用排名函数。在 SQL 2000 中,我认为你被困在做这样的事情。

DECLARE @RC INT 

CREATE TABLE #lengths
(
id INT IDENTITY(1,1),
[length] INT
)

INSERT INTO #lengths
SELECT LEN(MyText)
FROM MyTable
ORDER BY LEN(MyText) ASC


SET @rc= @@ROWCOUNT

SELECT [length] 
FROM #lengths 
WHERE id IN 
(@rc/3, (2*@rc)/3, @rc)
于 2010-08-23T20:07:02.027 回答
0

我认为你需要这样的东西:

SELECT
 x1.l AS Length,
 x1.n      * 1e2 / (SELECT COUNT(*) FROM MyTable) AS [Percent],
 SUM(x2.n) * 1e2 / (SELECT COUNT(*) FROM MyTable) AS CumPercent
FROM (
 SELECT LEN(MyText) AS l, COUNT(*) AS n
 FROM MyTable
 GROUP BY LEN(MyText)
) AS x1
LEFT JOIN (
 SELECT LEN(MyText) AS l, COUNT(*) AS n
 FROM MyTable
 GROUP BY LEN(MyText)
) AS x2
 ON x2.l <= x1.l
GROUP BY x1.l, x1.n
ORDER BY x1.l
于 2010-08-23T20:27:00.277 回答