2011-03-24 60 views
0

我是新的SQL Server 2008和tsql.I正在遷移我的查詢從oracle到SQL Server.I在一個查詢中遇到了這個問題,同時migrating.I不知道如何解決和是什麼導致了這個錯誤從PLSQL傳遞時TSQL查詢中的問題

該錯誤是多列是包含外部引用的聚合表達式 指定 。如果要彙總的 表達式包含 外部引用,則外部引用必須是表達式中引用的唯一列 。

感謝您的幫助。

完整的查詢是

SELECT MAIN.ID_MAMUL, 
     MAIN.ID_BIRIM, 
     MAIN.MAMUL_NO, 
     MAIN.DS_MAMUL, 
     MAIN.DS_BIRIM, 
     SAYIM.MT_SAYIM, 
     SAYIM.DS_TARIH, 
     (SELECT Isnull (SUM (CASE 
           WHEN TRSH.GIRIS_TARIHI > Isnull (SAYIM.DS_TARIH, 
                 DBO.To_date ('01.01.2006' 
                 , 
                 'dd.mm.yyyy')) THEN 
           dbo.F_BIRIM_CEVRIM_KATSAYI (LUM.ID_MAMUL, 
           CRMB.ID_BIRIM, 
           LUM.ID_BIRIM) * 
           TRSD.MT_MIKTAR 
           ELSE 0 
          END), 0) 
     FROM TR_STOK_HEADER TRSH, 
       TR_STOK_DETAY TRSD, 
       CR_MAMUL_BARKOD CRMB, 
       LU_MAMUL LUM 
     WHERE TRSH.ID_STOK_HEADER = TRSD.ID_STOK_HEADER 
       AND TRSD.ID_MAMUL_BARKOD = CRMB.ID_MAMUL_BARKOD 
       AND CRMB.ID_MAMUL = LUM.ID_MAMUL 
       AND LUM.ID_MAMUL = MAIN.ID_MAMUL 
       AND TRSH.ID_DEPO = 327 
       AND TRSH.ID_STOK_HAREKET_TIPI IN (6, 15) 
       AND TRSH.AKTIF = 1 
       AND TRSD.VALID = 1)   MT_ALIS, 
     (SELECT Isnull (SUM (CASE 
           WHEN TRSH.GIRIS_TARIHI > Isnull (SAYIM.DS_TARIH, 
                 DBO.To_date ('01.01.2006' 
                 , 
                 'dd.mm.yyyy')) THEN 
           DBO.F_BIRIM_CEVRIM_KATSAYI (LUM.ID_MAMUL, 
           CRMB.ID_BIRIM, 
           LUM.ID_BIRIM) * 
           TRSD.MT_MIKTAR 
           ELSE 0 
          END), 0) 
     FROM TR_STOK_HEADER TRSH, 
       TR_STOK_DETAY TRSD, 
       CR_MAMUL_BARKOD CRMB, 
       LU_MAMUL LUM 
     WHERE TRSH.ID_STOK_HEADER = TRSD.ID_STOK_HEADER 
       AND TRSD.ID_MAMUL_BARKOD = CRMB.ID_MAMUL_BARKOD 
       AND CRMB.ID_MAMUL = LUM.ID_MAMUL 
       AND LUM.ID_MAMUL = MAIN.ID_MAMUL 
       AND TRSH.ID_DEPO = 327 
       AND TRSH.ID_STOK_HAREKET_TIPI = 8 
       AND TRSH.AKTIF = 1 
       AND TRSD.VALID = 1 
       AND TRSD.FL_DURUM = 0 
       AND TRSD.FL_BIREBIR_IADE = 0) MT_NOKTA_IADE, 
     (SELECT Isnull (SUM (CASE 
           WHEN TRSH.GIRIS_TARIHI > Isnull (SAYIM.DS_TARIH, 
                 DBO.To_date ('01.01.2006' 
                 , 
                 'dd.mm.yyyy')) THEN 
          DBO.F_BIRIM_CEVRIM_KATSAYI (LUM.ID_MAMUL, 
          CRMB.ID_BIRIM, 
          LUM.ID_BIRIM) * 
         Abs 
         ( 
         TRSD.MT_MIKTAR) 
         ELSE 0 
         END), 0) 
     FROM TR_STOK_HEADER TRSH, 
       TR_STOK_DETAY TRSD, 
       CR_MAMUL_BARKOD CRMB, 
       LU_MAMUL LUM 
     WHERE TRSH.ID_STOK_HEADER = TRSD.ID_STOK_HEADER 
       AND TRSD.ID_MAMUL_BARKOD = CRMB.ID_MAMUL_BARKOD 
       AND CRMB.ID_MAMUL = LUM.ID_MAMUL 
       AND LUM.ID_MAMUL = MAIN.ID_MAMUL 
       AND TRSH.ID_DEPO = 327 
       AND TRSH.ID_STOK_HAREKET_TIPI = 9 
       AND TRSH.AKTIF = 1 
       AND TRSD.VALID = 1 
       AND TRSD.FL_DURUM = 0 
       AND TRSD.FL_BIREBIR_IADE = 0) MT_PLASIYER_IADE, 
     (SELECT Isnull (SUM (CASE 
           WHEN TRSH.GIRIS_TARIHI > Isnull (SAYIM.DS_TARIH, 
                 DBO.To_date ('01.01.2006' 
                 , 
                 'dd.mm.yyyy')) THEN 
          DBO.F_BIRIM_CEVRIM_KATSAYI (LUM.ID_MAMUL, 
          CRMB.ID_BIRIM, 
          LUM.ID_BIRIM) * 
         Abs 
         ( 
         TRSD.MT_MIKTAR) 
         ELSE 0 
         END), 0) 
     FROM TR_STOK_HEADER TRSH, 
       TR_STOK_DETAY TRSD, 
       CR_MAMUL_BARKOD CRMB, 
       LU_MAMUL LUM 
     WHERE TRSH.ID_STOK_HEADER = TRSD.ID_STOK_HEADER 
       AND TRSD.ID_MAMUL_BARKOD = CRMB.ID_MAMUL_BARKOD 
       AND CRMB.ID_MAMUL = LUM.ID_MAMUL 
       AND LUM.ID_MAMUL = MAIN.ID_MAMUL 
       AND TRSH.ID_DEPO = 327 
       AND TRSH.ID_STOK_HAREKET_TIPI = 8 
       AND TRSH.AKTIF = 1 
       AND TRSD.VALID = 1 
       AND TRSD.FL_DURUM = 0 
       AND TRSD.FL_BIREBIR_IADE = 1) MT_NOKTA_BIREBIR_IADE, 
     (SELECT Isnull (SUM (CASE 
           WHEN TRSH.GIRIS_TARIHI > Isnull (SAYIM.DS_TARIH, 
                 DBO.To_date ('01.01.2006' 
                 , 
                 'dd.mm.yyyy')) THEN 
          DBO.F_BIRIM_CEVRIM_KATSAYI (LUM.ID_MAMUL, 
          CRMB.ID_BIRIM, 
          LUM.ID_BIRIM) * 
         Abs 
         ( 
         TRSD.MT_MIKTAR) 
         ELSE 0 
         END), 0) 
     FROM TR_STOK_HEADER TRSH, 
       TR_STOK_DETAY TRSD, 
       CR_MAMUL_BARKOD CRMB, 
       LU_MAMUL LUM 
     WHERE TRSH.ID_STOK_HEADER = TRSD.ID_STOK_HEADER 
       AND TRSD.ID_MAMUL_BARKOD = CRMB.ID_MAMUL_BARKOD 
       AND CRMB.ID_MAMUL = LUM.ID_MAMUL 
       AND LUM.ID_MAMUL = MAIN.ID_MAMUL 
       AND TRSH.ID_DEPO = 327 
       AND TRSH.ID_STOK_HAREKET_TIPI = 5 
       AND TRSH.AKTIF = 1 
       AND TRSD.VALID = 1 
       AND TRSD.FL_DURUM = 0 
       AND TRSD.FL_BIREBIR_IADE = 0) MT_SATIS, 
     MAIN.MT_STOK 
FROM (SELECT MA.ID_MAMUL, 
       MA.MAMUL_NO, 
       MA.DS_MAMUL, 
       BR.ID_BIRIM, 
       BR.DS_BIRIM_KISA DS_BIRIM, 
       (DBO.F_BIRIM_CEVRIM_KATSAYI (MA.ID_MAMUL, MB.ID_BIRIM, 
       MA.ID_BIRIM) * 
       DS.MT_MIKTAR) MT_STOK 
     FROM CR_DEPO_STOK DS, 
       CR_MAMUL_BARKOD MB, 
       LU_MAMUL MA, 
       LU_BIRIM BR 
     WHERE DS.ID_MAMUL_BARKOD = MB.ID_MAMUL_BARKOD 
       AND MB.ID_MAMUL = MA.ID_MAMUL 
       AND MA.ID_BIRIM = BR.ID_BIRIM 
       AND DS.ID_DEPO = 327 
       AND DS.FL_DURUM = 0) MAIN 
     LEFT JOIN (SELECT MA.ID_MAMUL, 
         SS.DS_TARIH, 
         SUM (DBO.F_BIRIM_CEVRIM_KATSAYI (MA.ID_MAMUL, 
           MB.ID_BIRIM, 
           MA.ID_BIRIM) 
           * 
           SD.MT_SAYIM) MT_SAYIM 
        FROM (SELECT S.ID_DEPO, 
           B.ID_MAMUL, 
           MAX (S.DS_TARIH) DS_TARIH 
          FROM TR_STOK_SAYIM S, 
           TR_STOK_SAYIM_DETAY D, 
           CR_MAMUL_BARKOD B 
          WHERE S.ID_STOK_SAYIM = D.ID_STOK_SAYIM 
           AND D.ID_MAMUL_BARKOD = B.ID_MAMUL_BARKOD 
           AND D.FL_ONAY = 1 
           AND S.ID_DEPO = 327 
          GROUP BY S.ID_DEPO, 
            B.ID_MAMUL) SON_SAY, 
         TR_STOK_SAYIM SS, 
         TR_STOK_SAYIM_DETAY SD, 
         CR_MAMUL_BARKOD MB, 
         LU_MAMUL MA 
        WHERE SS.ID_STOK_SAYIM = SD.ID_STOK_SAYIM 
         AND SD.ID_MAMUL_BARKOD = MB.ID_MAMUL_BARKOD 
         AND MB.ID_MAMUL = MA.ID_MAMUL 
         AND SS.ID_DEPO = 327 
         AND SS.ID_DEPO = SON_SAY.ID_DEPO 
         AND SS.DS_TARIH = SON_SAY.DS_TARIH 
         AND MA.ID_MAMUL = SON_SAY.ID_MAMUL 
         AND SD.FL_ONAY = 1 
        GROUP BY MA.ID_MAMUL, 
          SS.DS_TARIH) SAYIM 
     ON MAIN.ID_MAMUL = SAYIM.ID_MAMUL 
ORDER BY 3, 
      2 
+2

嘗試將錯誤縮小到較小的部分,這對於解析人類來說太過分了 – Andomar 2011-03-24 09:13:17

回答

1

我永遠不會寫這麼長的查詢,因爲它是多一個人來解析由@Andomar指出。如果您逐個瀏覽連接的不同「部分」,您可能會發現錯誤。