2017-08-08 115 views
-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 a 
    JOIN 
     TblOgrenci o ON a.OgrNo = o.OgrNo) 
GROUP BY 
    SinifNo, OgrNo, Kalan, Ad, Soyad 

這是我的SQL查詢。但我得到這個錯誤:GROUP BY語法錯誤

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'group'.

我能做些什麼來解決這個問題?

回答

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 
+0

由於其工作 –

0

切換到代碼

join TblOgrenci o on a.OgrNo=o.OgrNo) as tbl group by SinifNo,OgrNo, 
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 
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