简体   繁体   English

SQL Server 2000:记录的1/3(或2/3)字段长度

[英]SQL Server 2000: Length of field in 1/3 (or 2/3) of the records

Is there a simpler/cleaner way to do this using SQL Server 2000? 使用SQL Server 2000有更简单/更清晰的方法吗?

Every few days I need to do this. 我每隔几天就需要这样做。

I first look at how many records we have total: 我首先看一下我们总共有多少条记录:

SELECT COUNT(*) FROM MyTable

Then I list all the lengths of a certain field: 然后我列出某个字段的所有长度:

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

I then need to scroll down 1/3rd of the way... and note the value. 然后我需要向下滚动1/3 ...并注意值。 I then need to scroll down 2/3rds of the way... and note the value. 然后我需要向下滚动2 / 3rds ...并记下值。 And then finally the last value. 然后最后是最后一个值。

I need to find out x, y, and z: 我需要找出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

In SQL 2005 you could probably use the ranking functions for this. 在SQL 2005中,您可以使用排名函数。 In SQL 2000 I think you're stuck doing something like this. 在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)

I think you need something like this: 我想你需要这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM