Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql LeftJoin中MAX()值的子查询_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

Sql LeftJoin中MAX()值的子查询

Sql LeftJoin中MAX()值的子查询,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我在SQL Server中有这个查询,我想知道它是否可以缩短 我对所有列进行了一个子查询(带有GROUPBY),以获取来自表左联接的3个日期的MAX()值 我想我不能没有子查询来获取3个日期的MAX() SELECT otherCol1 ,otherCol2 ,MAX(UnsubscribedDate) AS UnsubscribedDate ,MAX(OpenedDate) AS OpenedDate ,MAX(ClickedDate) AS ClickedD

我在SQL Server中有这个查询,我想知道它是否可以缩短

我对所有列进行了一个子查询(带有GROUPBY),以获取来自表左联接的3个日期的MAX()值

我想我不能没有子查询来获取3个日期的MAX()

SELECT otherCol1
    ,otherCol2 
    ,MAX(UnsubscribedDate) AS UnsubscribedDate
    ,MAX(OpenedDate) AS OpenedDate
    ,MAX(ClickedDate) AS ClickedDate
FROM (
    SELECT otherCol1
        ,otherCol2
        ,tu.JoinDate AS UnsubscribedDate
        ,trkOp.Clicktime AS OpenedDate
        ,trkRes.Clicktime AS ClickedDate
    FROM v_members_email_occurrence vmec
    LEFT JOIN tracking_unsubscribed tu WITH (NOLOCK) ON tu.ReportId = vmec.SendingId
    LEFT JOIN tracking_opened trkOp WITH (NOLOCK) ON trkOp.ReportId = vmec.SendingId
    LEFT JOIN tracking_result trkRes WITH (NOLOCK) ON trkRes.ReportId = vmec.SendingId
    WHERE vmec.MemberId = @MemberId
    ) AS Result
GROUP BY otherCol1
        ,otherCol2

您可以不使用子查询进行聚合:

SELECT otherCol1
    ,otherCol2
    ,MAX(tu.JoinDate) AS UnsubscribedDate
    ,MAX(trkOp.Clicktime) AS OpenedDate
    ,MAX(trkRes.Clicktime) AS ClickedDate
FROM v_members_email_occurrence vmec
LEFT JOIN tracking_unsubscribed tu WITH (NOLOCK) ON tu.ReportId = vmec.SendingId
LEFT JOIN tracking_opened trkOp WITH (NOLOCK) ON trkOp.ReportId = vmec.SendingId
LEFT JOIN tracking_result trkRes WITH (NOLOCK) ON trkRes.ReportId = vmec.SendingId
WHERE vmec.MemberId = @MemberId
GROUP BY otherCol1
        ,otherCol2

您可以不使用子查询进行聚合:

SELECT otherCol1
    ,otherCol2
    ,MAX(tu.JoinDate) AS UnsubscribedDate
    ,MAX(trkOp.Clicktime) AS OpenedDate
    ,MAX(trkRes.Clicktime) AS ClickedDate
FROM v_members_email_occurrence vmec
LEFT JOIN tracking_unsubscribed tu WITH (NOLOCK) ON tu.ReportId = vmec.SendingId
LEFT JOIN tracking_opened trkOp WITH (NOLOCK) ON trkOp.ReportId = vmec.SendingId
LEFT JOIN tracking_result trkRes WITH (NOLOCK) ON trkRes.ReportId = vmec.SendingId
WHERE vmec.MemberId = @MemberId
GROUP BY otherCol1
        ,otherCol2