0

我有一个包含道路参考号和道路长度的表格,其中包含列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(),但我的结果看起来不正确。

任何有关最有效方法的帮助将不胜感激。谢谢

4

2 回答 2

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
于 2013-03-22T16:39:19.877 回答
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
于 2013-03-21T14:00:45.590 回答