SELECT
A.RegTimePartnerID AS OrganiationID,
P.PartnerName AS OrganiationName,
PUIDCount AS IndividualsTrained_count,
EventCount AS TrainingActivities_count,
SalesCount AS Salestraining_Count,
TechCount AS Technicaltraining_count,
CASE WHEN OnDemandCount = 0 THEN 100
ELSE (Onlinecount*100/OnDemandCount)
END AS LivevsOndemand
FROM
dbo.Partner P
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct UniqueID) AS PUIDCount
FROM dbo.DenormTrainingActivity(NOLOCK)
GROUP BY RegTimePartnerID) A
ON P.PartnerID = A.RegTimePartnerID AND P.PartnerID <> 0
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct EventID) AS EventCount
FROM dbo.DenormTrainingActivity(NOLOCK)
GROUP BY RegTimePartnerID) B
ON A.RegTimePartnerID = B.RegTimePartnerID
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct EventID) AS SalesCount
FROM dbo.DenormTrainingActivity(NOLOCK)
WHERE CourseTypeid = 10
GROUP BY RegTimePartnerID) C
ON A.RegTimePartnerID = C.RegTimePartnerID
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct EventID) As TechCount
FROM dbo.DenormTrainingActivity(NOLOCK)
WHERE CourseTypeid = 20
GROUP BY RegTimePartnerID) D
ON A.RegTimePartnerID = D.RegTimePartnerID
INNER JOIN
(SELECT RegTimePartnerID ,COUNT(distinct EventID) AS Onlinecount
FROM dbo.DenormTrainingActivity(NOLOCK)
WHERE EventCategory IN ('Live-ILT', 'Live-Online')
GROUP BY RegTimePartnerID) E
ON A.RegTimePartnerID = E.RegTimePartnerID
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct EventID) AS OnDemandCount
FROM dbo.DenormTrainingActivity(NOLOCK)
WHERE EventCategory NOT IN ('Live-ILT', 'Live-Online')
GROUP BY RegTimePartnerID) F
ON A.RegTimePartnerID = F.RegTimePartnerID
答案 0 :(得分:1)
类似的东西(我确定有拼写错误):
SELECT
A.RegTimePartnerID AS OrganiationID,
P.PartnerName AS OrganiationName,
(
SELECT COUNT(DISTINCT dta.[UniqueID])
FROM dbo.[DenormTraningActivity](NOLOCK) AS dta
WHERE dta.[RegTimePartnerID] = P.[PartnerID]
) AS PUIDCount,
(
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTraningActivity](NOLOCK) AS dta
WHERE dta.[RegTimePartnerID] = P.[PartnerID]
) AS EventCount,
(
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[CourseTypeid] = 10
AND dta.[RegTimePartnerId] = P.[PartnerID]
) AS SalesCount,
(
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[CourseTypeid] = 20
AND dta.[RegTimePartnerID] = P.[PartnerID]
) AS TechCount,
CASE
WHEN (
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[EventCategory] IN ('Live-ILT', 'Live-Online')
AND dta.[RegTimePartnerID] = P.[PartnerID]
) = 0 THEN 100
ELSE (
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[EventCategory] IN ('Live-ILT', 'Live-Online')
AND dta.[RegTimePartnerID] = P.[PartnerID]
)*100/
(
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[EventCategory] NOT IN ('Live-ILT', 'Live-Online')
AND dta.[RegTimePartnerID] = P.[PartnerID]
)
END AS LivevsOndemand
FROM
dbo.Partner P
WHERE P.[PartnerID] <> 0
我在想,嵌套子查询可能比INNER JOIN / COUNT / GROUPBY便宜。可能不会,因为必须为Partner表中的每条记录评估每个子查询。