我正在寻求在特定时间范围内从每个机构获得25条随机线路。这是我提出的一个脚本:
DECLARE @dateStart DATE,
@dateEnd DATE,
@agency varchar(8)
set @dateStart = '9/1/2016'
set @dateEnd = '9/30/2016'
IF OBJECT_ID('tempdb.dbo.#tmp_THISDATA', 'U') IS NOT NULL
DROP TABLE #tmp_THISDATA
CREATE TABLE #tmp_THISDATA(
[ACCOUNT] [varchar](30) NOT NULL,
[AGENCY_CODE] [varchar](8) NOT NULL,
[THE_DATE] [DATETIME] NOT NULL)
DECLARE agencies CURSOR FOR
SELECT DISTINCT [AGENCY_CODE]
FROM [dbo].[CORE_DATA] dial
WHERE CAST([THE_DATE] AS DATE) BETWEEN @dateStart AND @dateEnd
OPEN agencies
FETCH NEXT FROM agencies
INTO @agency
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #tmp_THISDATA
SELECT TOP 25
[ACCOUNT]
,[AGENCY_CODE]
,[THE_DATE]
FROM [dbo].[CORE_DATA] dial
WHERE CAST([THE_DATE] AS DATE) BETWEEN @dateStart AND @dateEnd
AND AGENCY_CODE = @agency
ORDER BY NEWID()
END
SELECT *
FROM #tmp_THISDATA
这可行,但运行时间很长。这是一个丑陋的数据集,并且有一些优化它的努力,但我怀疑如果这样的事情是可行的,消除循环也会加速它。
可以消除循环吗?
答案 0 :(得分:2)
您可以使用ROW_NUMBER
窗口功能
INSERT INTO #tmp_THISDATA
SELECT [ACCOUNT],
[AGENCY_CODE],
[THE_DATE]
FROM (SELECT Row_number()OVER(partition BY AGENCY_CODE ORDER BY Newid() ) AS rn,
[ACCOUNT],
[AGENCY_CODE],
[THE_DATE]
FROM [dbo].[CORE_DATA] dial
WHERE [THE_DATE] > dateadd(dd,-1,@dateStart) and [THE_DATE] <= @dateEnd) a
WHERE rn <= 25
答案 1 :(得分:1)
您也可以使用CROSS APPLY
Select Distinct A.Account
,B.Agency_Code
,B.The_Date
From [dbo].[CORE_DATA] A
Cross Apply (Select Top 25 Agency_Code,Agency_Code
From [dbo].[CORE_DATA]
Where Account=A.Account and The_Date between @dateStart AND @dateEnd
Order By NewID()
) B