使用TSQL随机选择记录总和的百分比

时间:2013-03-21 12:14:44

标签: sql-server-2008 tsql

我的表格包含道路参考编号和道路长度,列RoadID (int)RoadLength (int)

大约有3000行。使用T-SQL我需要提取随机选择的道路参考及其长度,其中长度总和加起来占表中所有道路总长度的5%。这是一个年度道路调查,随机选择道路。

我正在对SQL Server 2008数据库使用T-SQL。尝试了本文http://www.sqlservercentral.com/Forums/Topic793008-149-1.aspx中关于三角形查询的一些变体,但在选择随机行时遇到了困难。我尝试使用order by newID(),但我的结果看起来不正确。

任何帮助以最有效的方式做到这一点将不胜感激。感谢

2 个答案:

答案 0 :(得分:0)

我不确定你需要接近5%的总数,但这应该会让你非常接近:

CREATE TABLE #RoadReference (RoadID INT IDENTITY, RoadLength INT)

INSERT #RoadReference (RoadLength) VALUES (CAST(RAND() * 1000 AS INT))
GO 3000

DECLARE @SampleDistance int

SELECT @SampleDistance = SUM(RoadLength) * .05 FROM #RoadReference

SELECT @SampleDistance AS FivePercentOfTotalRoadLength

SELECT RoadID, SUM(RoadLength) RoadLength
FROM (
    SELECT TOP 5 PERCENT * 
    FROM #RoadReference ORDER BY NEWID()) DataSample
GROUP BY RoadID WITH ROLLUP
ORDER BY RoadLength

答案 1 :(得分:0)

凌乱,但似乎有效

--Create a temp table and add a random number column
CREATE TABLE #Roads(ROW_NUM int, RoadID int, RoadLength int)

--Populate from zt_Roads table and add a random number field
INSERT #Roads (ROW_NUM , RoadID , RoadLength )
                    (SELECT ROW_NUMBER() OVER (ORDER BY NEWID()),
                        RoadID,
                        RoadLength
                         from zt_Roads)
go

--Calcualte 5% of the TOTAL length of ALL roads
declare @FivePercent int
SELECT  @FivePercent =  ROUND(Sum(IsNULL((RoadLength ),0))*.01,0) from zt_Roads
print 'One Percent of total length = ' 
Print @FivePercent

--Select a random sample from temp table so that the total sample length 
--is no more than 5% of all roads in table
; with RandomSample as 
(SELECT top 100 percent 
    ROW_NUM, 
    RoadID, 
    RoadLength, 
    RoadLength+
        COALESCE((Select Sum(RoadLength) from #Roads b 
        WHERE b.ROW_NUM < a.ROW_NUM),0) as RunningTotal

        From #Roads  a
        ORDER BY ROW_NUM)


Select * from RandomSample WHERE RunningTotal <@FivePercent 
Drop table #Roads