1

我有一个查询,计算电话接线员每天的查询次数

SELECT 
   [OperatorID],
   DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) AS CreationDate,
   COUNT(*) AS EnquiryCount
FROM 
   [Enquiries] AS e
GROUP BY 
   [OperatorID], DATEADD(day, DATEDIFF(day, 0, CreationDate), 0)

但是,我想将Operator表添加到此查询中,以便检索操作员名称

SELECT [OperatorID]
  ,[FirstName]
  ,[LastName]
FROM [Operators]

如果我在 group by 子句之前内部加入 2 个表:

SELECT [OperatorID]
  , DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) AS CreationDate
  , COUNT(*) AS EnquiryCount
  , st.FullName
FROM [Enquiries] AS e

INNER JOIN

(SELECT     OperatorID, FirstName + ' ' + LastName AS FullName 
FROM          dbo.Operators
WHERE      (Role = 'Operator')) AS o ON e.OperatorID = o.OperatorID

GROUP BY [OperatorID], DATEADD(day, DATEDIFF(day, 0, CreationDate), 0)

我收到以下错误:

列 'o.FullName' 在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。

我该如何解决这个问题?

4

5 回答 5

2

As比您可以安全地添加到 group by 子句OperatorID更独特:FullNameFullName

SELECT [OperatorID]
  , DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) AS CreationDate
  , COUNT(*) AS EnquiryCount
  , st.FullName
FROM [Enquiries] AS e

INNER JOIN

(SELECT     OperatorID, FirstName + ' ' + LastName AS FullName 
FROM          dbo.Operators
WHERE      (Role = 'Operator')) AS o ON e.OperatorID = o.OperatorID

GROUP BY [OperatorID], DATEADD(day, DATEDIFF(day, 0, CreationDate), 0), st.FullName

更新如评论中所述,我建议首先按所需的最少列数进行聚合,然后装饰结果。

SELECT si.[OperatorID], si.CreationDate, si.EnquiryCount, st.FullName
FROM

(
    SELECT [OperatorID]
      , DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) AS CreationDate
      , COUNT(*) AS EnquiryCount
    FROM [Enquiries]
    GROUP BY [OperatorID], DATEADD(day, DATEDIFF(day, 0, CreationDate), 0)
) AS si

INNER JOIN

(
    SELECT     OperatorID, FirstName + ' ' + LastName AS FullName 
    FROM          dbo.Operators
    WHERE      (Role = 'Operator')
) AS st

   ON si.OperatorID = st.OperatorID

其实我不确定,为什么你需要 subquery Operators。如果没有必要,则可以简化解决方案:

SELECT si.[OperatorID], si.CreationDate, si.EnquiryCount,
    st.FirstName + ' ' + st.LastName AS FullName
FROM

(
    SELECT [OperatorID]
      , DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) AS CreationDate
      , COUNT(*) AS EnquiryCount
    FROM [Enquiries]
    GROUP BY [OperatorID], DATEADD(day, DATEDIFF(day, 0, CreationDate), 0)
) AS si

INNER JOIN

Operators AS st
  ON si.OperatorID = st.OperatorID and st.Role = 'Operator'
于 2012-07-27T09:24:12.857 回答
1

请尝试,我没有测试过!希望这能解决错误

SELECT [OperatorID], CONCAT(FirstName,LastName) AS FullName , DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) AS CreationDate , COUNT(*) AS EnquiryCount , st.FullName FROM [Enquiries] AS e INNER JOIN dbo.Operators ON dbo.Operators.OperatorID = e.OperatorID AND (Role = 'Operator') GROUP BY [OperatorID], DATEADD(day, DATEDIFF(day, 0, CreationDate), 0)

于 2012-07-27T09:26:22.880 回答
0

只需添加st.FullNameGroup by

GROUP BY 
   [OperatorID], 
   DATEADD(day, DATEDIFF(day, 0, CreationDate), 0), 
   st.FullName
于 2012-07-27T09:23:55.970 回答
0

您可以使用带有ROW_NUMBER函数和的公用表表达式COUNT(*)OVER

WITH cte AS
(
   SELECT e.[OperatorID]
  , DATEADD(day, DATEDIFF(day, 0, e.CreationDate), 0) AS CreationDate
  , o.FirstName + ' ' + o.LastName AS FullName 
  , RN = ROW_NUMBER()OVER(PARTITION BY [o.OperatorID], DATEADD(day, DATEDIFF(day, 0, e.CreationDate), 0) ORDER BY e.CreationDate DESC)
  , EnquiryCount = COUNT(*)OVER(PARTITION BY [o.OperatorID], DATEADD(day, DATEDIFF(day, 0, e.CreationDate), 0))
   FROM [Enquiries] AS e
   INNER JOIN  dbo.Operators o ON e.OperatorID = o.OperatorID
   WHERE Role = 'Operator'
)
SELECT * FROM cte
WHERE RN = 1
于 2012-07-27T09:27:40.057 回答
0

使用公用表表达式,您可以做到

with cte as (
SELECT  
   [OperatorID], 
   DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) AS CreationDate, 
   COUNT(*) AS EnquiryCount 
FROM  
   [Enquiries] AS e 
GROUP BY  
   [OperatorID], DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) 
)
SELECT
 cte.OperatorI,cte.CreationDate,o.FirstName + ' ' + o.LastName AS FullName   
FROM
 cte inner join dbo.Operators o
 on o.Role = 'Operator' and e.OperatorID = o.OperatorID  
于 2012-07-27T09:53:22.567 回答