2016-05-29 4 views
-1

私は複数回行っていますが、動作していないようです。これは可能ですか?私はあなたが原因getdate()関数にSQL Serverを使用していると仮定してい共通テーブル式(CTE)内でSUM()関数を実行する方法

With PrintsPerDayPerEmp(ID,[Month],[Day],[Year], [Number Of Prints]) 
AS 
(Select ID, 
    datepart(month,Agent_Local_Time) as [Month], 
    datepart(day,Agent_Local_Time) as [Day], 
    datepart(year,Agent_Local_Time) as [Year], 
    count(RowID) as [Number of Prints] 
From Table1 
Where Agent_Local_Time >= (getdate()-90) 
Group by datepart(day,Agent_Local_Time), 
    datepart(month,Agent_Local_Time), 
    datepart(year,Agent_Local_Time), 
    ID 
) 
select * 
from PrintsPerDayPerEmp 
where [Number Of Prints] > (sum([Number Of Prints])/90)*3 
order by ID desc 
+0

だから、まさにあなたは私たちが行うことを期待して何をすべきか、私達にあなたの 'goes'を表示? – sagi

+0

申し訳ありません:)追加しました。 – RoflWaffle17

+0

私はこの論理がここでは明らかに列全体を1つの数値として表していると思いますが、見つかった各IDの合計を表示したいと思います。 – RoflWaffle17

答えて

0

:以下は私の現在の試みは何かということです。

なぜCTEを使用しようとしているのかわかりません。それはまったく必要ないようです。

私の推測では、sum(count(RowID))が全体の合計になると予想していますが、それは集計関数の仕組みではありません。 GROUP BY句は、すべての集約関数に影響します。

私の推測では、これはあなたが望むものです。

Select Id, 
    datepart(month,Agent_Local_Time) as 'Month', 
    datepart(day,Agent_Local_Time) as 'Day', 
    datepart(year,Agent_Local_Time) as 'Year', 
    count(RowID) as 'Number Of Prints', 
    count(RowID) over() as 'Print Sum' 
From Table1 
Where Agent_Local_Time >= (getdate()-90) 
Group by datepart(day,Agent_Local_Time), 
    datepart(month,Agent_Local_Time), 
    datepart(year,Agent_Local_Time), 
    Id 
Order by Id desc 

またはこの:それは、OVER()句凝集を制御し、全体の合計を取得するために使用しています

Select Id, 
    datepart(month,Agent_Local_Time) as 'Month', 
    datepart(day,Agent_Local_Time) as 'Day', 
    datepart(year,Agent_Local_Time) as 'Year', 
    count(RowID) as 'Number Of Prints', 
    count(RowID) over (partition by Id) as 'Print Sum' 
From Table1 
Where Agent_Local_Time >= (getdate()-90) 
Group by datepart(day,Agent_Local_Time), 
    datepart(month,Agent_Local_Time), 
    datepart(year,Agent_Local_Time), 
    Id 
Order by Id desc 

をしかし、いくつかのデータと期待される結果なしで、それは言うことは非常に困難です。

+0

皆様、お手伝いをしてくれてありがとうございます。休日と一緒に長い週末、私はこれに応じて/これに対応することができませんでした。しかし、基本的に、私が見つけようとしている結果は次のとおりです。 1.毎日の印刷枚数が>過去の90日間のIDの印刷枚数の3倍を超える場合、すべてのIDとその日プリント枚数を返します。 今、私のクエリは、ID、月/日/年、過去90日以内に見つかったすべてのIDの印刷枚数を返しています...問題は、90日平均を得ることができない場合です。特定の日の印刷枚数は平均よりも3倍多くなります。 これは理にかなっていますか? – RoflWaffle17

0

まず、列エイリアスには一重引用符を使用しないでください。文字列と日付の定数には単一引用符を使用してください。 SQL Serverでは、正しいエスケープ文字は二重引用符または角カッコです。

第2に、group byはすべて間違っています。

私は、これはidは、従業員IDであると仮定すると、基本的にあなたがしたいクエリだと思う:

With PrintsPerDayPerEmp([Id],[Month],[Day],[Year], [Number Of Prints], [Print Sum]) 
AS (
     Select Id, datepart(month, Agent_Local_Time) as [Month], 
      datepart(day, Agent_Local_Time) as [Day], 
      datepart(year, Agent_Local_Time) as [Year], 
      count(RowID) as [Number of time per Day], 
      sum(count(RowID)) over() as [Print Sum] 
     From Table1 
     Where Agent_Local_Time >= (getdate() - 90) 
     Group by datepart(day, Agent_Local_Time), 
       datepart(month, Agent_Local_Time), 
       datepart(year, Agent_Local_Time), 
       Id 
    ) 
select * 
from PrintsPerDayPerEmp 
order by Id desc; 
+0

私の最新の試みは、コードの元の投稿に編集されました。 – RoflWaffle17

+0

@ RoflWaffle17。 。 。クエリのロジックを変更しました。'where'節で集約関数を使うことはできません。 'where'を削除し、質問に答えます。別の必要がある場合は、別の質問をしてください。 –

関連する問題