1

電話オペレーターによる 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

UPDATEコメントに記載されているように、最初に必要最小限の列で集計を行い、次に結果を装飾することをお勧めします。

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

実際には、なぜサブクエリが必要なのかわかりません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

あなたができる共通のテーブル式を使用して

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 に答える
0

に追加st.FullNameするだけGroup byです:

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

ROW_NUMBERfunction とで共通のテーブル式を使用できます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 に答える