0

我有以下查询:

SELECT TOP 10 
    EnrolledDate,
    DATEDIFF(MINUTE, EnrolledTimeUtc, EnrollmentCompleteTimeUtc) as Enroll
FROM StudentEnrollment
WHERE EnrolledTimeUtc IS NOT NULL


SELECT
    AVG(DATEDIFF(MINUTE, EnrolledTimeUtc, EnrollmentCompleteTimeUtc)) as AvgEnrollmentTime,
    STDEV(DATEDIFF(MINUTE, EnrolledTimeUtc, EnrollmentCompleteTimeUtc)) as  EnrollmentStDev 
FROM StudentEnrollment
WHERE EnrolledTimeUtc IS NOT NULL

我正在尝试组合这些查询,以便我可以在一个表中查看数据。有可能做到吗?

我尝试将它们结合使用

SELECT (SELECT... ), (SELECT...)

但由于行数不匹配上述脚本不起作用。

第一个查询返回如下内容:

EnrolledDate                Enroll
2013-04-24 23:17:59.613     1
2013-04-24 23:18:01.753     5
2013-04-24 23:18:03.517     10
2013-04-24 23:18:05.703     8

第二个查询返回:

AvgEnrollmentTime EnrollmentStDev
19                22.1125   

我希望这两个表结合起来,所以它应该显示如下:

EnrolledDate                Enroll       AvgEnrollmentTime EnrollmentStDev
2013-04-24 23:17:59.613     1            19                22.1125
2013-04-24 23:18:01.753     5
2013-04-24 23:18:03.517     10
2013-04-24 23:18:05.703     8

使用 Hiren 的脚本后,我让它像这样工作:

EnrolledDate                Enroll       AvgEnrollmentTime EnrollmentStDev
2013-04-24 23:17:59.613     1            19                22.1125
2013-04-24 23:18:01.753     5            Null              Null
2013-04-24 23:18:03.517     10           Null              Null
2013-04-24 23:18:05.703     8            Null              Null 

但是有可能让它看起来像这样吗?

EnrolledDate                Enroll       AvgEnrollmentTime EnrollmentStDev
2013-04-24 23:17:59.613     1            19                22.1125
2013-04-24 23:18:01.753     5            19                22.1125
2013-04-24 23:18:03.517     10           19                22.1125
2013-04-24 23:18:05.703     8            19                22.1125
4

1 回答 1

0

这很容易做到。您必须对其使用完全外连接并使用行号连接两个表。您可以执行以下查询:

SELECT A.EnrolledDate , A.Enroll ,B.AvgEnrollmentTime , B.EnrollmentStDev
FROM
(
    SELECT TOP 10
    EnrolledDate,
    DATEDIFF(MINUTE, EnrolledTimeUtc, EnrollmentCompleteTimeUtc) as Enroll
    FROM StudentEnrollment
    WHERE EnrolledTimeUtc IS NOT NULL
) AS A
,
(
    SELECT
    AVG(DATEDIFF(MINUTE, EnrolledTimeUtc, EnrollmentCompleteTimeUtc)) as AvgEnrollmentTime,
    STDEV(DATEDIFF(MINUTE, EnrolledTimeUtc, EnrollmentCompleteTimeUtc)) as  EnrollmentStDev 
    FROM StudentEnrollment
    WHERE EnrolledTimeUtc IS NOT NULL
) AS B 
于 2013-07-03T19:02:14.713 回答