2017-04-05 20 views
0
select max(total), date 
from (select sum(total) as total, date as date 
     from canteen 
     group by date) as max 

テーブルから最高のセールと日付を選びたいと思っています。SQLテーブルから日付で最高のセールを選択

このエラーが表示されます。それは 集計関数またはGROUP BY句に含まれていないため

例外、エラーコード8120は、SQLStateのS0001]列「max.date」は、選択リストに無効 です。

答えて

1

注文を使用して、データを降順で注文し、最初の行をフェッチすることができます。

あなたは、あなたがこれが最大の売上合計を達成するすべての日付を返しますROW_NUMBER()

select TOP(1) total, date 
from 
(
    select sum(total) as total, date as date 
    from canteen 
    group by date 
) as max 
Order by todal desc 
+0

ありがとうございます。出来た 。もしそれがあまりにも多くのトラブルではない私に私のクエリで間違っていることを教えてください –

+0

@モート:あなたはあなたの外側のクエリにグループを追加することを忘れてしまった。 – samithagun

+0

@Mohit:in文でグループを逃した外側のクエリでselect max(total)を使用しました。 –

0

を使用することができ、日付によって結果たい場合。 Hereはデモです。

; with x as (
    select sum(total) as total, date from canteen group by date 
) 
, y as (
    select dr = dense_rank() over(order by total desc), * 
    from x 
) 
select * from y where dr = 1 
0

あなたは最大(合計)とのすべての日付を取得したい場合は、ここではそれはあなたがあなたの外側のクエリ内で&順序でグループを追加するには忘れている

;with temp as 
(
    select sum(total) as total, date as date 
    from canteen 
    group by date 
) 
SELECT TOP 1 WITH TIES * 
FROM temp 
ORDER BY temp.total desc 
0

です。すべての売上を降順で表示するように変更しました。したがって、最高の売り上げが一番上にあります。

select max(total) total, date 
from (select sum(total) as total, date as date 
     from canteen 
     group by date) as max 
group by date 
order by total desc 
関連する問題