遍历ID列表

huangapple go评论205阅读模式
英文:

Loop thru List of IDs

问题

我正在尝试弄清楚如何更改我的查询以接受多个值而不仅仅是一个@agentid值,然后循环查询每个agentid值。我猜我卡在如何声明这个以及要使用哪个循环上。任何帮助将不胜感激!

英文:

I am trying to figure out how to change my query to accept multiple values for the @agentid instead of just one and then loop through the query for each agentid. I guess I am hung-up on how to declare that and then which loop to use. Any help would be greatly appreciated!

  1. declare
  2. @Startdate datetime,
  3. @Enddate datetime,
  4. @agentid varchar(100)
  5. set @startdate = '4/17/2023'
  6. set @enddate = '8/07/2023'
  7. set @agentid = '1024971' --would like to change to ('12345','6789','98763','246810') as example and then loop through code below for each id in the list.
  8. --AS
  9. --BEGIN
  10. SET NOCOUNT ON
  11. /*
  12. DECLARE @YearStart AS datetime = '2019-11-20 00:00:00', @Startdate AS datetime = '2019-11-17 00:00:00', @Enddate AS datetime = '2019-11-25 00:00:00'
  13. */
  14. DELETE FROM tblGenesys
  15. FROM tblGenesys INNER JOIN
  16. tblUsers2 AS u ON tblGenesys.AgentID = u.GenesysLogon AND tblGenesys.AgentSummaryDate = u.ExtractDate
  17. WHERE ([AgentSummaryDate] >= @Startdate and [AgentSummaryDate] < DATEADD(dd,1,@Enddate)) AND [GenesysGroupName] = 'US' and u.employeeid = @agentid
  18. INSERT INTO [dbo].[tblGenesys]
  19. (
  20. [Username], [AgentSummaryDate], [GenesysGroupName], [AgentNameRaw], [AgentIDRaw], [LastName], [FirstName], [AgentID],
  21. AgentIDSuffix, [Account], [AccountGroup], [AccountOrganization], [Supervisor], [SVP], [JobTitle], [Client_Paid],
  22. [TotalDuration], [InboundCount], [InboundTime], [OutboundCount], [OutboundTime], [InternalCount], [InternalTime],
  23. [HoldTime], [ACWTime], [WaitTime], [DialTime], [BreakTime], FaxEmailTime, [LeadTime], [LunchTime], [Meet_TrainTime],
  24. [OtherTime], [ProjectTime], [SystemIssueTime], [NotReadyTime], [HRTime], [ManualWorkTime], [OtherChannelTime], [Transferred],
  25. [WeekOf], [DayOfWeek], [MonthOf], GenesysLogon
  26. )
  27. SELECT
  28. g.[Username], [AgentSummaryDate], [GroupName], [AgentNameRaw], [AgentIDRaw], g.[LastName], g.[FirstName], [AgentID],
  29. AgentIDSuffix, g.[Account], g.[AccountGroup], g.[AccountOrganization], g.[Supervisor], g.[SVP], g.[JobTitle], [Client_Paid],
  30. [TotalDuration], [InboundCount], [InboundTime], [OutboundCount], [OutboundTime], [InternalCount], [InternalTime],
  31. [HoldTime], [ACWTime], [WaitTime], [DialTime], [BreakTime], FaxEmailTime, [LeadTime], [LunchTime], [Meet_TrainTime],
  32. [OtherTime], [ProjectTime], [SystemIssueTime], [NotReadyTime], [HRTime], [ManualWorkTime], [OtherChannelTime], [TranMake],
  33. DATEADD(DD, 2 - DATEPART(DW, DATEADD(DD, 0, agentsummarydate)), DATEADD(DD, 0, agentsummarydate)) as WeekOf, LEFT(DATENAME(dw, CAST(agentsummarydate AS DATE)), 3)
  34. AS DayOfWeek, LEFT(DATENAME(mm, CAST(agentsummarydate AS DATE)), 3) + '-' + RIGHT(DATENAME(YY, CAST(agentsummarydate AS DATE)), 2) AS [MonthOf], g.AgentID
  35. FROM [uv_Genesys_AgentSummaryGroup_CST] as g inner join
  36. tblusers2 as u on g.AgentID = u.GenesysLogon and g.AgentSummaryDate = u.ExtractDate
  37. where [AgentSummaryDate] >= @Startdate and [AgentSummaryDate] < DATEADD(dd,1,@Enddate) and u.EmployeeID = @agentid
  38. ORDER BY [AgentSummaryDate], [LastName] , [FirstName]
  39. -- Select for presentation in report
  40. SELECT count(*) as [RecordsInsertedTable], min(D.[AgentSummaryDate]) as [FirstDate] ,min(D.[WeekOf]) as [FirstWeekOf],max(D.[AgentSummaryDate]) as [LastDate]
  41. ,max(D.[WeekOf]) as [LastWeekOf],min(D.[CreateDate]) as [FirstDBCreateDate],max(D.[CreateDate]) as [LastDBCreateDate]
  42. FROM [dbo].[tblGenesys] as D with(nolock) inner join
  43. tblUsers2 as u on D.AgentID = u.GenesysLogon and D.AgentSummaryDate = u.ExtractDate
  44. WHERE D.[AgentSummaryDate] >= @Startdate And D.[AgentSummaryDate] < DATEADD(dd,1,@Enddate) AND D.[GenesysGroupName] = 'US' and u.EmployeeID = @agentid

答案1

得分: 1

@agentid 转换为临时表。

  1. DROP TABLE IF EXISTS #AgentID
  2. SELECT
  3. A.AgentID
  4. INTO #AgentID
  5. FROM
  6. (
  7. VALUES
  8. ('1234')
  9. ,('2341')
  10. ,('3412')
  11. ,('4123')
  12. ) A(AgentID)

然后,在任何具有 AgentID 字段的表上进行内部连接以进行查询限制。

  1. FROM [uv_Genesys_AgentSummaryGroup_CST] AS G
  2. INNER JOIN tblusers2 AS U
  3. ON G.AgentID = U.GenesysLogon
  4. AND G.AgentSummaryDate = U.ExtractDate
  5. INNER JOIN #AgentID AS AI
  6. ON G.AgentID = AI.AgentID

最后,从所有的 where 语句中移除任何 AND U.AgentID = @agentid

格式要求如下:

  • From 语句将具有打开和关闭括号。 From (...)
  • 表必须被命名,并在括号内指定列名。 From (...) AS A(ColName)
  • 在 From 括号内,您将使用 VALUES (val1),(val2),...(valn)

为了使格式更容易,使用 Excel 在列A中列出您的ID。然后运行以下公式 =CONCAT("('", TEXTJOIN("'),('", TRUE(), A:A), "')")。这将将您的代理ID格式化为此方法所需的语法。

限制:
您将受到大约1000个ID或更少的限制。因此,只适用于小到中等规模的需求。

英文:

Turn @agentid into a temp table.

  1. DROP TABLE IF EXISTS #AgentID
  2. SELECT
  3. A.AgentID
  4. INTO #AgentID
  5. FROM
  6. (
  7. VALUES
  8. ('1234')
  9. ,('2341')
  10. ,('3412')
  11. ,('4123')
  12. ) A(AgentID)

Then, do an Inner Join at any table that has an AgentID field to join to and limit your query pull.

  1. FROM [uv_Genesys_AgentSummaryGroup_CST] AS G
  2. INNER JOIN tblusers2 AS U
  3. ON G.AgentID = U.GenesysLogon
  4. AND G.AgentSummaryDate = U.ExtractDate
  5. INNER JOIN #AgentID AS AI
  6. ON G.AgentID = AI.AgentID

Lastly, remove any AND U.AgentID = @agentid from all of your where statements.

The format requires the following:

  • From statement will have an open & close parentheses. From (...)
  • Table must be named, and have the column name identified inside parenthesis. From (...) AS A(ColName)
  • Inside the From parenthesis, you will use VALUES (val1),(val2),...(valn)

To make formatting easier, list your ID's in Excel using column A. Then run the formula =CONCAT("('", TEXTJOIN("'),('", TRUE(), A:A), "')"). This will format your agent ids into the required syntax for this method.

Limitations:
You will be limited to approximately 1000 ids or less. So only useful for small to medium scale needs.

答案2

得分: 0

以下是翻译好的代码部分:

  1. CREATE FUNCTION [dbo].[fn_Split_String]
  2. (
  3. @List NVARCHAR(MAX),
  4. @Delimiter NVARCHAR(255)
  5. )
  6. RETURNS TABLE
  7. WITH SCHEMABINDING AS
  8. RETURN
  9. WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
  10. UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
  11. UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1),
  12. E2(N) AS (SELECT 1 FROM E1 a, E1 b),
  13. E4(N) AS (SELECT 1 FROM E2 a, E2 b),
  14. E42(N) AS (SELECT 1 FROM E4 a, E2 b),
  15. cteTally(N) AS (SELECT 0 UNION ALL SELECT TOP (DATALENGTH(ISNULL(@List,1)))
  16. ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E42),
  17. cteStart(N1) AS (SELECT t.N+1 FROM cteTally t
  18. WHERE (SUBSTRING(@List,t.N,1) = @Delimiter OR t.N = 0))
  19. SELECT Item = SUBSTRING(@List, s.N1, ISNULL(NULLIF(CHARINDEX(@Delimiter,@List,s.N1),0)-s.N1,8000))
  20. FROM cteStart s;
  21. declare
  22. @Startdate datetime,
  23. @Enddate datetime,
  24. @agentid varchar(100) = 'xx,yy,ff,dd',
  25. @x varchar(50) = 1
  26. declare @agentid_list table (id varchar(50) , RN varchar(50))
  27. insert into @agentid_list
  28. SELECT * ,ROW_NUMBER()over(order by (select 1) ) RN
  29. from [dbo].[fn_Split_String](@agentid,',')
  30. WHILE (SELECT max(RN) FROM @agentid_list) >= @x
  31. BEGIN
  32. SELECT * FROM @agentid_list where rn = @x
  33. --u.EmployeeID = (SELECT id FROM @agentid_list where rn = @x)
  34. set @x = @x +1
  35. END

注意:翻译结果已经按照您的要求只返回了代码部分,没有包含额外的内容。

英文:

something like this

  1. CREATE FUNCTION [dbo].[fn_Split_String]
  2. (
  3. @List NVARCHAR(MAX),
  4. @Delimiter NVARCHAR(255)
  5. )
  6. RETURNS TABLE
  7. WITH SCHEMABINDING AS
  8. RETURN
  9. WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
  10. UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
  11. UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1),
  12. E2(N) AS (SELECT 1 FROM E1 a, E1 b),
  13. E4(N) AS (SELECT 1 FROM E2 a, E2 b),
  14. E42(N) AS (SELECT 1 FROM E4 a, E2 b),
  15. cteTally(N) AS (SELECT 0 UNION ALL SELECT TOP (DATALENGTH(ISNULL(@List,1)))
  16. ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E42),
  17. cteStart(N1) AS (SELECT t.N+1 FROM cteTally t
  18. WHERE (SUBSTRING(@List,t.N,1) = @Delimiter OR t.N = 0))
  19. SELECT Item = SUBSTRING(@List, s.N1, ISNULL(NULLIF(CHARINDEX(@Delimiter,@List,s.N1),0)-s.N1,8000))
  20. FROM cteStart s;
  21. declare
  22. @Startdate datetime,
  23. @Enddate datetime,
  24. @agentid varchar(100) ='xx,yy,ff,dd',
  25. @x varchar(50) = 1
  26. declare @agentid_list table (id varchar(50) , RN varchar(50))
  27. insert into @agentid_list
  28. SELECT * ,ROW_NUMBER()over(order by (select 1) ) RN
  29. from [dbo].[fn_Split_String](@agentid,',')
  30. WHILE (SELECT max(RN) FROM @agentid_list) >= @x
  31. BEGIN
  32. SELECT * FROM @agentid_list where rn = @x
  33. --u.EmployeeID = (SELECT id FROM @agentid_list where rn = @x)
  34. set @x = @x +1
  35. END

huangapple
  • 本文由 发表于 2023年8月11日 02:28:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定