使用 Max 和 group by

发布于 2024-12-01 13:18:14 字数 2239 浏览 0 评论 0原文

我有以下查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余罪 2024-12-08 13:18:14

当您使用GROUP BY子句时,您必须为SELECT列表中的每一列指定是否需要组聚合对其应用运算符(例如 SUMMAX),或者是否构成组 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 the SELECT list, whether you want a group-aggregate operator (eg SUM, MAX) applied to it, or whether it is to form part of the group key - this latter is done by including it in the GROUP BY list.

Since you are SELECTing all 5 columns of your subquery a, 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 inthe GROUP BY clause.

亣腦蒛氧 2024-12-08 13:18:14
  1. 修改派生表 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

  1. Modify your derived table a to have its own GROUP BY. You've got a MAX() in there that forces you to include a GROUP 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:

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

Here's how your code should look: http://pastie.org/2428802

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文