2

我有一个程序尝试在每个代理的表中插入样本,每个代理的样本数根据某些计算而变化

Declare @samplesize int
Declare @Top int
set @samplesize=0

;WITH DataToInsert AS
(
    Select AgentID, Surveys, LOB,(case when day(getdate())<4 then 3 else (day(getdate())) - (Surveys*3) end) SampleSize from Current_Agent_SurveyCount_HSI Where surveys<8 
)

--Insert Into Survey_Source_New (LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, Cycle, [TimeStamp])  
--Select top 5 ss.LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, Cycle, [TimeStamp]  
--From Survey_source_Level1 ss
--inner join DataToInsert du on ss.AgentZID=du.agentID
--where flag is null and du.samplesize>6
--order by newid()

Insert Into Survey_Source_New (LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, Cycle, [TimeStamp])  
Select top (@Top) @Top=du.samplesize,ss.LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, Cycle, [TimeStamp]  
From Survey_source_Level1 ss
inner join DataToInsert du on ss.AgentZID=du.agentID
where flag is null and du.samplesize<7
order by newid()

我在这里得到的错误是

消息 4115,级别 15,状态 1,第 4 行
TOP 子句的参数中不允许引用列“samplesize”。此处仅允许引用外部范围内的列或独立表达式和子查询。

有解决方法吗?

任何帮助表示赞赏。

提前致谢。

4

2 回答 2

2

你可以row_number()用来做基本上相同的事情:

WITH DataToInsert AS
(
    Select AgentID, Surveys, LOB,(case when day(getdate())<4 then 3 else (day(getdate())) - (Surveys*3) end) SampleSize from Current_Agent_SurveyCount_HSI Where surveys<8 
)
Insert Into Survey_Source_New (LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, Cycle, [TimeStamp])  
    select LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, Cycle, [TimeStamp]
    from (Select ss.LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, Cycle, [TimeStamp],
                 row_number() over (order by newid()) as seqnum
          From Survey_source_Level1 ss inner join
               DataToInsert du on ss.AgentZID=du.agentID
          where flag is null and du.samplesize<7
         ) t
    where seqnum <= du.sample_size

您也许可以简化一下,但我不知道flag是来自 du 还是 ss。

于 2012-12-24T20:31:41.580 回答
0

我对同一个问题有一个额外的场景。我对现有解决方案进行了一些更改,场景是:我每天都会得到一个代理列表,并且必须向每个代理发送一组随机调查,计数取决于每个代理的一些计算。

--Get Agent List
;WITH AgentsList AS    
(    
    Select AgentID, Surveys, LOB,(case when day(getdate())<4 then 3 else (day(getdate())) - (Surveys*3) end) SampleSize     
    from Current_Agent_SurveyCount_FIOS Where surveys<8     
)
--Get All the Surveys for each Agent
, AgentSurveys AS
(
    select ss.LOB, ss.CenterName, ss.CallDate, ss.AgentZid, ss.TN, ss.Ticket, ss.RecordingID, ss.Cycle, ss.[TimeStamp],ss.Flag,AL.samplesize 
    from Survey_Source_Level1_Sri ss
    inner join AgentsList AL on ss.AgentZID=AL.agentID
    where flag is null 
    GROUP BY ss.LOB, ss.CenterName, ss.CallDate, ss.AgentZid, ss.TN, ss.Ticket, ss.RecordingID, ss.Cycle, ss.[TimeStamp],ss.Flag,AL.samplesize
)
--Mark random ranking for each survey
Select LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, Cycle, [TimeStamp],samplesize,    
             rank() over (partition by agentzid order by newid()) as seqnum    
      From AgentSurveys
      group by LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, Cycle, [TimeStamp],samplesize      
      order by agentzid,seqnum,tn

现在从最后一个查询中我得到一个像

在此处输入图像描述

我需要 N 来自 SampleSize 列的每个组的 N 行数。

于 2012-12-26T05:44:58.437 回答