我有以下查询:
SELECT a.* FROM
(SELECT
moncallAdd.FirstListing,
max (Dateadd(MINUTE, moncalladd.addtime,
DateAdd(Day,moncalladd.adddate,'12/31/1899'))) as AddStart,
DATEADD(MINUTE, mOnCallAdd.StartOnCallTime,
DATEADD(DAY, mOnCallAdd.StartOnCallDate, '12/31/1899')) AS OnCallStart,
DATEADD(MINUTE, mOnCallAdd.duration,
DATEADD(MINUTE, mOnCallAdd.StartOnCallTime,
DATEADD(DAY, mOnCallAdd.StartOnCallDate, '12/31/1899'))) AS OnCallEnd,
'Added' AS Activity
FROM
mdr.dbo.mOnCallAdd
WHERE DATEADD(MINUTE, mOnCallAdd.StartOnCallTime,
DATEADD(DAY, mOnCallAdd.StartOnCallDate, '12/31/1899')) < GETDATE()
AND
DATEADD(MINUTE, mOnCallAdd.duration,
DATEADD(MINUTE, mOnCallAdd.StartOnCallTime,
DATEADD(DAY, mOnCallAdd.StartOnCallDate, '12/31/1899'))) > GETDATE()
AND mOnCallAdd.SchedName = 'capital neph') a
LEFT JOIN
(SELECT
moncallDelete.FirstListing,
DATEADD(MINUTE, mOnCallDelete.StartOnCallTime,
DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899')) AS OnCallStart,
DATEADD(MINUTE, mOnCallDelete.duration,
DATEADD(MINUTE, mOnCallDelete.StartOnCallTime,
DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899'))) AS OnCallEnd,
'Deleted' AS Activity
FROM
mdr.dbo.mOnCallDelete
WHERE DATEADD(MINUTE, mOnCallDelete.StartOnCallTime,
DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899')) < GETDATE()
AND
DATEADD(MINUTE, mOnCallDelete.duration,
DATEADD(MINUTE, mOnCallDelete.StartOnCallTime,
DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899'))) > GETDATE()
AND mOnCallDelete.SchedName = 'capital neph') b
ON a.FirstListing = b.FirstListing
and a.oncallstart = b.oncallstart
and a.oncallend = b.oncallend
group by FirstListing
当我尝试运行此查询时,出现以下错误:
不明确的列名'FirstListing'。
当我尝试group by moncalladd.firstlisting
时,我收到此错误:
列前缀'moncalladd'与查询中使用的表名或别名不匹配。
当我尝试group by a.firstlisting
时,我收到此错误:
列'a.AddStart'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
我不知道我错过了什么,但显然它很简单。有人可以帮我这个吗?
答案 0 :(得分:1)
当您使用GROUP BY
子句时,您必须为SELECT
列表中的每个列指定是否需要组聚合运算符(例如{ {1}},SUM
)已应用于此,或者是否构成键组的一部分 - 后者是通过将其包含在MAX
列表中完成的。
由于您GROUP BY
了子查询SELECT
的所有5列,因此您必须说明在执行分组操作时每个列都会发生什么。
我的查询是正确的并不是很明显,所以我不能说解决方案是什么,但可能只是明确列出{{1的所有列在a
子句中。
答案 1 :(得分:0)
a
以拥有自己的GROUP BY。你的MAX()
强迫你加入GROUP BY
。请参阅我的Pastie上的第4行第16行。2.在查询的第一行和最后一行修改SELECT和GROUP BY:
SELECT a.FirstListing, a.AddStart, a.OnCallStart, a.OnCallEnd, a.Activity
FROM ---your big derived table
GROUP BY a.FirstListing, a.AddStart, a.OnCallStart, a.OnCallEnd, a.Activity
以下是您的代码的外观:http://pastie.org/2428802