我有以下查询运行速度非常慢,感谢您的评论,以帮助我提高运行速度。我没有权限更改任何数据库。我有两个数据库,1和2.
数据库1具有呼叫者信息,包括CallMemberID(可能少于或超过14位,可能包括逗号),车辆属性(颜色,制造商,型号,状态,......)等。
我删除了逗号,然后将CallMemberID的前14位数字作为MemberId使用:left(CallMemberID_mod,14)作为' MemberID'
我还将车辆属性设为“车辆”。以后使用以下方法查找每个会员ID的不同车辆数量:计数(不同(b.Vehicle))为'每个会员的车辆数量' 数据库2包含有关投诉的成员的信息。它包括clubcode,CallMemberID和dait(它实际上是成员拨打电话的日期)。 我发现每位会员的投诉数量使用:COUNT(*)as' ComplaintsPerMemberId'由MemberID分组 我还使用以下方法找到了每个会员ID呼叫之间的平均天数:AvgCallTimes:
WITH CallTimesOrdered (num, id, calldate) AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY clubcode, CallMemberID ORDER BY dait DESC) AS RowNumber
,CallMemberID
,dait
FROM database1
)
,AvgCallTimes (id, timespan) AS
(
SELECT CurrentDate.id
,DATEDIFF(d, CurrentDate.calldate, PriorDate.calldate)
FROM CallTimesOrdered CurrentDate
INNER JOIN CallTimesOrdered PriorDate ON PriorDate.num = CurrentDate.num - 1 AND PriorDate.id = CurrentDate.id
)
SELECT b.MemberID
,AVG(d.timespan) AS AverageTimeBetweenCalls
,COUNT(Distinct(b.Vehicle)) AS VehicleCountPerMember
,ISNULL(vic.ComplaintsPerMemberId, 0) AS NumberOfComplaintsPerMember
FROM (
SELECT *
,ISNULL(a.VehicleColor,'') + ' ' + ISNULL(a.VehicleManufactureYearDate, '') + ' ' + ISNULL(a.VehicleLicenseStateCode, '') + ' '
+ ISNULL(a.VehicleManufactureNumber, '') +' '
+ ISNULL(a.VehicleModelNumber, '')
AS Vehicle
FROM (
SELECT c.CallMemberID
,LEFT(CallMemberID_mod, 14) AS MemberID
,c.VehicleColor
,c.VehicleManufactureYearDate
,c.VehicleLicenseStateCode
,c.VehicleManufactureNumber
,c.VehicleModelNumber
,ID
FROM (
SELECT LTRIM(REPLACE(CallMemberID, ',', '')) AS CallMemberID_mod
,*
FROM database1 WITH (nolock)
) c
WHERE LEN(CallMemberID_mod) >= 14
) a
) b
LEFT OUTER JOIN
(
SELECT tab.MemberId
,tab.ComplaintsPerMemberId
FROM (
SELECT clubcode
,MemberId
,COUNT(*) AS ComplaintsPerMemberId
FROM database2
GROUP BY MemberId
,clubcode
) tab
) vic On vic.MemberId = b.MemberId
LEFT JOIN
(
SELECT Id
,timespan
FROM AvgCallTimes
) d ON d.ID = b.CallMemberID
GROUP BY b.MemberID
,vic.ComplaintsPerMemberId
ORDER BY b.MemberID
答案 0 :(得分:0)