GROUP BY语法错误

时间:2017-08-08 08:14:03

标签: sql tsql

SELECT 
    SinifNo, OgrNo, Kalan, Ad, Soyad, MAX(VadeGunu) 
FROM 
    (SELECT 
         o.SinifNo, a.OgrNo, o.Ad, o.Soyad, o.Sezon, o.SubeKod,
         o.TCNo, o.AnneAd, o.AnneMobil, o.BabaAd, o.BabaMobil,
         a.Tarih, a.Tutar, a.Odenen, a.Kalan,
         DATEDIFF(DAY, a.Tarih, (getdate() + 1)) AS VadeGunu 
     FROM 
         TblTahPlan a 
     JOIN
         TblOgrenci o ON a.OgrNo = o.OgrNo)
GROUP BY 
    SinifNo, OgrNo, Kalan, Ad, Soyad

这是我的SQL查询。但是我收到了这个错误:

  

Msg 156,Level 15,State 1,Line 2
  关键字' group'附近的语法不正确。

我该怎么做才能解决这个问题?

4 个答案:

答案 0 :(得分:1)

如果您格式化查询,它会变得更容易阅读,更容易发现错误:

select  SinifNo
,       OgrNo
,       Kalan
,       Ad
,       Soyad
,       MAX(VadeGunu) 
FROM    (
            SELECT  o.SinifNo,a.OgrNo,o.Ad,o.Soyad,o.Sezon,o.SubeKod,o.TCNo,o.AnneAd,o.AnneMobil,o.BabaAd,o.BabaMobil,a.Tarih,a.Tutar,a.Odenen,a.Kalan
            ,       DATEDIFF(DAY,a.Tarih,(getdate()+1)) as VadeGunu 
            FROM    TblTahPlan AS a 
            JOIN    TblOgrenci AS o 
                ON  a.OgrNo=o.OgrNo
        ) AS T1
GROUP by SinifNo,OgrNo, Kalan,Ad,Soyad

您缺少子查询的别名,以及group by之前的空格。为什么要在子查询中选择所有这些属性?为什么要使用子查询?你可以轻松地做到:

SELECT  o.SinifNo
,       a.OgrNo
,       a.Kalan
,       o.Ad
,       o.Soyad
,       MAX(DATEDIFF(DAY,a.Tarih,(getdate()+1))) as VadeGunu 
FROM    TblTahPlan a 
JOIN    TblOgrenci o 
     ON a.OgrNo=o.OgrNo
GROUP by o.SinifNo
,       a.OgrNo
,       a.Kalan
,       o.Ad
,       o.Soyad

答案 1 :(得分:0)

更改为代码

join TblOgrenci o on a.OgrNo=o.OgrNo) as tbl group by SinifNo,OgrNo, 

答案 2 :(得分:0)

不要为自己编写代码。正确对齐和格式化。

更改您的查询,如下所示。您必须为内部查询提供别名。

select 
        SinifNo,
        OgrNo, 
        Kalan,
        Ad,
        Soyad, 
        max(VadeGunu) 
  from (select 
                o.SinifNo,
                a.OgrNo,
                o.Ad,
                o.Soyad,
                o.Sezon,
                o.SubeKod,
                o.TCNo,
                o.AnneAd,
                o.AnneMobil,
                o.BabaAd,
                o.BabaMobil,
                a.Tarih,
                a.Tutar,
                a.Odenen,
                a.Kalan,
                DATEDIFF(DAY,a.Tarih,(getdate()+1)) as VadeGunu 
           from TblTahPlan a 
           join TblOgrenci o on a.OgrNo=o.OgrNo) as tblA
 group by SinifNo, OgrNo, Kalan, Ad, Soyad

答案 3 :(得分:0)

你可以这样使用

SELECT SinifNo,OgrNo, Kalan,Ad,Soyad, max(VadeGunu) 
FROM 
(SELECT o.SinifNo   As  SinifNo
,   a.OgrNo     As  OgrNo
,   o.Ad        As  Ad
,   o.Soyad     As  Soyad
,   o.Sezon     As  Sezon
,   o.SubeKod   As  SubeKod
,   o.TCNo      As  TCNo
,   o.AnneAd    As  AnneAd
,   o.AnneMobil As  AnneMobil
,   o.BabaAd    As  BabaAd
,   o.BabaMobil As  BabaMobil
,   a.Tarih     As  Tarih
,   a.Tutar     As  Tutar
,   a.Odenen    As  Odenen
,   a.Kalan     As  Kalan
,   DATEDIFF(DAY,a.Tarih,(getdate()+1)) AS VadeGunu 
FROM    TblTahPlan     a 
Inner   Join
        TblOgrenci o 
On      a.OgrNo     =   o.OgrNo )   c
GROUP BY c.SinifNo
,        c.OgrNo
,        c.Kalan
,        c.Ad
,        c.Soyad