使用 Max 和 group by
我有以下查询:
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 子句中。
我不知道我错过了什么,但显然这很简单。谁能帮我解决这个问题吗?
I have the following query:
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
and when I try to run this query I get the following error:
Ambiguous column name 'FirstListing'.
and when I try group by moncalladd.firstlisting
, I get this error:
The column prefix 'moncalladd' does not match with a table name or alias name used in the query.
and when I try to group by a.firstlisting
I get this error:
Column 'a.AddStart' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I don't know what I'm missing, but obviously it's something simple. Can anyone please help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
GROUP BY
子句时,您必须为SELECT
列表中的每一列指定是否需要组聚合对其应用运算符(例如SUM
、MAX
),或者是否构成组 key 的一部分 - 后者是通过包含来完成的它位于GROUP BY
列表中。由于您要
SELECT
子查询a
的所有 5 列,因此您必须说明执行分组操作时每一列会发生什么情况。对我来说,您的查询是否正确并不是立即显而易见的,因此我不能说出解决方案是什么,但它可能只是显式列出
aGROUP BY
子句中的 code>。The moment you use a
GROUP BY
clause, you have to specify, for every column in theSELECT
list, whether you want a group-aggregate operator (egSUM
,MAX
) applied to it, or whether it is to form part of the group key - this latter is done by including it in theGROUP BY
list.Since you are
SELECT
ing all 5 columns of your subquerya
, you have to say what is to happen to every single one, when the grouping operation is performed.It's not immediately obvious to me that your query is correct, so I can't say what the solution is, but it may be simply a metter of explicitly listing all the columns of
a
intheGROUP BY
clause.a
以拥有自己的 GROUP BY。那里有一个MAX()
,它强制您包含一个GROUP BY
。请参阅我的 Pastie 上的第 4 行至第 16 行。2.修改查询第一行和最后一行的 SELECT 和 GROUP BY:
代码应如下所示: http:// Pastie.org/2428802
a
to have its own GROUP BY. You've got aMAX()
in there that forces you to include aGROUP BY
. See Lines 4 thorugh 16 on my Pastie.2.Modify your SELECT and GROUP BY on the first and last lines of your query:
Here's how your code should look: http://pastie.org/2428802