我有一个程序试图在每个代理的表中插入样本,每个代理的样本数根据一些计算而变化
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()
我在这里遇到的错误是
Msg 4115,Level 15,State 1,Line 4
TOP子句的参数中不允许引用“samplesize”列。仅引用外部的列 此处允许使用范围或独立表达式和子查询。
有解决方法吗?
感谢任何帮助。
提前致谢。
答案 0 :(得分: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。
答案 1 :(得分: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行,其中N来自SampleSize列。