见表:
表名:A
Call Session ID Sequence No Source Application
STUD-213 1 Application A
STUD-213 2 Application B
STUD-213 3 Application A
STUD-213 4 Application C
STUD-321 1 Application A
STUD-321 2 Application C
STUD-321 3 Application A
STUD-321 4 Application B
这是一个示例数据。问题是,我需要计算这些项目之间的项目数。例如,有多少 应用程序A 转到 应用程序B ?我想要的输出应该是:
Source Application End Application Count
Application A Application B 7
Application B Application C 10
Application A Application C 6
如果我在SQL中尝试这个,我认为这会很棘手。表A的记录将是巨大的,每月约1000万,所以我也考虑了性能。
您对如何处理此问题有什么建议吗?
编辑:更改了帖子标题。希望它有助于将来偶然发现这个问题的人。
答案 0 :(得分:1)
CountApp是AppA最终以AppB结束的次数,
VersionCount是从AppA到AppB所需的步骤。
希望其中一个是你所追求的。
declare @CS table (CallSessionID varchar(8), SequenceNo int, SourceApplication varchar(15))
insert into @CS
values
('STUD-213',1,'Application A'),
('STUD-213',2,'Application B'),
('STUD-213',3,'Application A'),
('STUD-213',4,'Application C'),
('STUD-321',1,'Application A'),
('STUD-321',2,'Application C'),
('STUD-321',3,'Application A'),
('STUD-321',4,'Application B')
select
mini.SourceApplication as StartApp,
maxi.SourceApplication as EndApp,
Count(*) as CountApp,
mm.VersionCount
from
(select CallSessionID, min(SequenceNo) MinSeq, Max(SequenceNo) MaxSeq, count(SequenceNo) VersionCount
from @CS
group by CallSessionID) mm
inner join @CS mini
on mini.CallSessionID = mm.CallSessionID
and mini.SequenceNo = mm.MinSeq
inner join @CS maxi
on maxi.CallSessionID = mm.CallSessionID
and maxi.SequenceNo = mm.MaxSeq
group by
mini.SourceApplication,
maxi.SourceApplication
编辑版本2
此版本查看序列中从一个应用程序到下一个应用程序的转换,然后在每次从AppA转换到AppB时进行计数。
select
c1.SourceApplication as StartApp,
c2.SourceApplication as EndApp,
Count(*) as CountApp
from
@CS c1
inner join @CS c2
on c1.CallSessionID = c2.CallSessionID
and c1.SequenceNo = (c2.SequenceNo - 1)
group by
c1.SourceApplication,
c2.SourceApplication
答案 1 :(得分:0)
结束解决我的问题。我使用循环来获取每个流程。仍然不确定这一次在生产中的表现。我尝试在2.05秒内查询250万条记录的表格。
我将把这个留在这里,以帮助将来徘徊于此问题的任何人:
DECLARE @MAXSEQNO INT
, @MINSEQNO INT
, @INCREMENT INT;
DECLARE @TABLE TABLE ([SourceApplication] VARCHAR(50)
, [EndApplication] VARCHAR(50)
, [ApplicationCount] INT);
SET @MAXSEQNO = (SELECT MAX(SequenceNo) FROM A)
SET @MINSEQNO = (SELECT MIN(SequenceNo) FROM A)
WHILE(@MINSEQNO <> @MAXSEQNO)
BEGIN
SET @INCREMENT = (SELECT @MINSEQNO + 1)
INSERT INTO @TABLE
([Sourceapplication], [EndApplication], [ApplicationCount])
SELECT A.SourceApplication AS [StartApplication]
, B.SourceApplication AS [EndApplication]
, COUNT(*) AS [ApplicationCount]
FROM A AS Main
INNER JOIN A B ON (Main.CallSessionID = B.CallSessionID)
WHERE A.ApplicationSequenceNo = @MINSEQNO
AND B.ApplicationSequenceNo = @INCREMENT
GROUP BY A.SourceApplication
, B.SourceApplication
SET @MINSEQNO = (SELECT @MINSEQNO + 1)
END
SELECT [SourceApplication]
, [EndApplication]
, SUM(ApplicationCount) AS [ApplicationCount]
FROM @TABLE
GROUP BY [SourceApplication]
, [EndApplication]