MSSQL多个加入组

时间:2016-05-05 12:41:13

标签: sql-server join

 Select TOP 50
 C1.TCKimlikNo as TCKimlikNo,
 GSSFat.FatTar as GssFatTar,
 KID.gssGidisNo,
 C1.UNVAN as HastaAdi,
 K.NO as MasterNo,
 K.HastaNO as HastaNo,
 Cast(KID.IS_TARIHI + ( 0.0417 * DatePart(Hour, KID.PostTime)) + ( 0.000695 * DatePart(Minute, KID.PostTime)) as DateTime) as TarihSaat,
 I.AD as IslemAdi,
 case IsNull(C1.CINSIYET, '') when 'K' then 'Kadın' else 'Erkek' end as Cinsiyet,
 C1.DOGUM_TAR as HastaDogTar,
 C1.EVTEL as Telefon,
 C1.GSM1 as Gsm1,
 C1.ADRES1 as Adres,
 C1.BABA_AD as BabaAdi,
 Ilcx.AD as AdresIlce,
 Ilx.AD as AdresIl,
 DatePart(Year, KID.IS_TARIHI) - IsNull(DatePart(Year, C1.DOGUM_TAR), 0) as HastaYasi,
 IsNull(K.Aciklama, '') + ' ' + IsNull(K.Aciklama2, '') + ' ' + IsNull(K.Aciklama3, '') as Aciklama,
 K.Aciklama as Aciklama_1,
 K.Aciklama2 as Aciklama_2,
 K.Aciklama3 as Aciklama_3,
 KT.AD as VezneAdi



 from KASA_ISLEM_DETAY as KID with(nolock)  
 Inner Join KASA as K with(nolock) on  KID.KASANO = K.NO 
 Inner Join CARI as C1 with(nolock) on  K.HastaNo = C1.NO 
 Inner Join ISLEM as I with(nolock) on  KID.IslemNo = I.NO 
 Left Join vw_KasaOdeme as OD with(nolock) on  KID.KASANO = OD.MasterNO 
 Left Outer Join CARI as C with(nolock) on  K.AnlasKurumNo = C.NO and C.SAHIS_KURUM = 0  
 Left Outer Join PERSONEL as P with(nolock) on  KID.DOCTOR_NO = P.NO 
 Left Outer Join (Select Max(LISTE_ISLEM_KOD) as IslemKod, ISLEM_NO from LISTE_FIYAT Group By ISLEM_NO) as LF on KID.IslemNo = LF.ISLEM_NO 
 Left Outer Join IL as Ilx with(nolock) on  C1.IL_NO = Ilx.NO 
 Left Outer Join ILCE as Ilcx with(nolock) on  C1.ILCE_NO = Ilcx.NO 
 Left Outer Join KASA_TANIM as KT with(nolock) on  K.KASA_NO = KT.No 
 Left Join vw_gssGiden as GSSProv on KID.gssGidisNo = GSSProv.TakipNo 
 Left Join vw_gssFatura as GSSFat on GSSProv.FatTakipNo = GSSFat.FatTakipNo 
 Left Outer Join Uyruk as UY with(nolock) on C1.Uyrugu = UY.NO Where K.SubeNo = 0  and GSSFat.FatTar between '05.012.2007' and '05.05.2016' and K.DELETED = 0   and (KID.IslemNo = 12155 or KID.IslemNo = 12154) and GSSFat.FatTar is not NULL and KID.gssGidisNo is not null

 GROUP BY K.HastaNo
 ORDER BY HastaNo ASC, TarihSaat DESC

我想成为独特的结果

1最近日期的结果需要获得结果

Microsof SQL Server Studio

错误代码

Msg 8120,Level 16,State 1,Line 2 专栏' CARI.TCKimlikNo'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

2 个答案:

答案 0 :(得分:0)

在GROUP BY子句中添加C1.TCKimlikNo

 Select TOP 50
 C1.TCKimlikNo as TCKimlikNo,
 GSSFat.FatTar as GssFatTar,
 KID.gssGidisNo,
 C1.UNVAN as HastaAdi,
 K.NO as MasterNo,
 K.HastaNO as HastaNo,
 Cast(KID.IS_TARIHI + ( 0.0417 * DatePart(Hour, KID.PostTime)) + ( 0.000695 * DatePart(Minute, KID.PostTime)) as DateTime) as TarihSaat,
 I.AD as IslemAdi,
 case IsNull(C1.CINSIYET, '') when 'K' then 'Kadın' else 'Erkek' end as Cinsiyet,
 C1.DOGUM_TAR as HastaDogTar,
 C1.EVTEL as Telefon,
 C1.GSM1 as Gsm1,
 C1.ADRES1 as Adres,
 C1.BABA_AD as BabaAdi,
 Ilcx.AD as AdresIlce,
 Ilx.AD as AdresIl,
 DatePart(Year, KID.IS_TARIHI) - IsNull(DatePart(Year, C1.DOGUM_TAR), 0) as HastaYasi,
 IsNull(K.Aciklama, '') + ' ' + IsNull(K.Aciklama2, '') + ' ' + IsNull(K.Aciklama3, '') as Aciklama,
 K.Aciklama as Aciklama_1,
 K.Aciklama2 as Aciklama_2,
 K.Aciklama3 as Aciklama_3,
 KT.AD as VezneAdi



 from KASA_ISLEM_DETAY as KID with(nolock)  
 Inner Join KASA as K with(nolock) on  KID.KASANO = K.NO 
 Inner Join CARI as C1 with(nolock) on  K.HastaNo = C1.NO 
 Inner Join ISLEM as I with(nolock) on  KID.IslemNo = I.NO 
 Left Join vw_KasaOdeme as OD with(nolock) on  KID.KASANO = OD.MasterNO 
 Left Outer Join CARI as C with(nolock) on  K.AnlasKurumNo = C.NO and C.SAHIS_KURUM = 0  
 Left Outer Join PERSONEL as P with(nolock) on  KID.DOCTOR_NO = P.NO 
 Left Outer Join (Select Max(LISTE_ISLEM_KOD) as IslemKod, ISLEM_NO from LISTE_FIYAT Group By ISLEM_NO) as LF on KID.IslemNo = LF.ISLEM_NO 
 Left Outer Join IL as Ilx with(nolock) on  C1.IL_NO = Ilx.NO 
 Left Outer Join ILCE as Ilcx with(nolock) on  C1.ILCE_NO = Ilcx.NO 
 Left Outer Join KASA_TANIM as KT with(nolock) on  K.KASA_NO = KT.No 
 Left Join vw_gssGiden as GSSProv on KID.gssGidisNo = GSSProv.TakipNo 
 Left Join vw_gssFatura as GSSFat on GSSProv.FatTakipNo = GSSFat.FatTakipNo 
 Left Outer Join Uyruk as UY with(nolock) on C1.Uyrugu = UY.NO Where K.SubeNo = 0  and GSSFat.FatTar between '05.012.2007' and '05.05.2016' and K.DELETED = 0   and (KID.IslemNo = 12155 or KID.IslemNo = 12154) and GSSFat.FatTar is not NULL and KID.gssGidisNo is not null

 GROUP BY K.HastaNo,C1.TCKimlikNo
 ORDER BY HastaNo ASC, TarihSaat DESC

答案 1 :(得分:0)

一般情况下,summaxminavgcount等所有字段均未汇总(SELECTGROUP BYSELECT等。 {1}}指令应该在GROUP BY中。由于SELECT DISTINCT中的所有项目都未汇总,因此您应将所有项目都放在Select DISTINCT TOP 50 C1.TCKimlikNo as TCKimlikNo, GSSFat.FatTar as GssFatTar, KID.gssGidisNo, C1.UNVAN as HastaAdi, K.NO as MasterNo, K.HastaNO as HastaNo, Cast(KID.IS_TARIHI + ( 0.0417 * DatePart(Hour, KID.PostTime)) + ( 0.000695 * DatePart(Minute, KID.PostTime)) as DateTime) as TarihSaat, I.AD as IslemAdi, case IsNull(C1.CINSIYET, '') when 'K' then 'Kadın' else 'Erkek' end as Cinsiyet, C1.DOGUM_TAR as HastaDogTar, C1.EVTEL as Telefon, C1.GSM1 as Gsm1, C1.ADRES1 as Adres, C1.BABA_AD as BabaAdi, Ilcx.AD as AdresIlce, Ilx.AD as AdresIl, DatePart(Year, KID.IS_TARIHI) - IsNull(DatePart(Year, C1.DOGUM_TAR), 0) as HastaYasi, IsNull(K.Aciklama, '') + ' ' + IsNull(K.Aciklama2, '') + ' ' + IsNull(K.Aciklama3, '') as Aciklama, K.Aciklama as Aciklama_1, K.Aciklama2 as Aciklama_2, K.Aciklama3 as Aciklama_3, KT.AD as VezneAdi from KASA_ISLEM_DETAY as KID with(nolock) Inner Join KASA as K with(nolock) on KID.KASANO = K.NO Inner Join CARI as C1 with(nolock) on K.HastaNo = C1.NO Inner Join ISLEM as I with(nolock) on KID.IslemNo = I.NO Left Join vw_KasaOdeme as OD with(nolock) on KID.KASANO = OD.MasterNO Left Outer Join CARI as C with(nolock) on K.AnlasKurumNo = C.NO and C.SAHIS_KURUM = 0 Left Outer Join PERSONEL as P with(nolock) on KID.DOCTOR_NO = P.NO Left Outer Join (Select Max(LISTE_ISLEM_KOD) as IslemKod, ISLEM_NO from LISTE_FIYAT Group By ISLEM_NO) as LF on KID.IslemNo = LF.ISLEM_NO Left Outer Join IL as Ilx with(nolock) on C1.IL_NO = Ilx.NO Left Outer Join ILCE as Ilcx with(nolock) on C1.ILCE_NO = Ilcx.NO Left Outer Join KASA_TANIM as KT with(nolock) on K.KASA_NO = KT.No Left Join vw_gssGiden as GSSProv on KID.gssGidisNo = GSSProv.TakipNo Left Join vw_gssFatura as GSSFat on GSSProv.FatTakipNo = GSSFat.FatTakipNo Left Outer Join Uyruk as UY with(nolock) on C1.Uyrugu = UY.NO Where K.SubeNo = 0 and GSSFat.FatTar between '05.012.2007' and '05.05.2016' and K.DELETED = 0 and (KID.IslemNo = 12155 or KID.IslemNo = 12154) and GSSFat.FatTar is not NULL and KID.gssGidisNo is not null ORDER BY HastaNo ASC, TarihSaat DESC 中。看看this问题。

但是,对于你想要的,最好使用"3_1470300029;44_62648640,8207444;46_7168,80036952867"而不是聚合,如下面的代码:

function(data) {
  alert(data);
  var dataArray = data.split(";");
  for (var i = 0; i < dataArray.length; i++) {
    dataArray[i] = dataArray[i].split("_");
  }
 for (var i = 0; i < dataArray.length; i++) {
    var waarde = dataArray[i];
    alert(waarde[0] + " " + waarde[1]);
    $('#totaal' + waarde[0]).html("€ " + waarde[1]);
    $("#inputNumber" + waarde[0]).val(waarde[1]);
 }