[]
SQL - Group by
Merhaba Duyuru! Sayenizde SQL öğreniyorum :)
Hesapladığım kolonları GROUP BY'a alamıyorum. Gönlüm ister ki bunları toplaya toplaya bi group by yapsın.
Koddaki amaç vadesi geçen, vade süresi 0-14 gün olan işlemleri bulup girdi-çıktıları toplama. firma ve vade bazında toplamak istiyorum bunları. SQL bana "Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause." diyor :(
Query ile ilgili başka tavsiyeniz veya öneriniz varsa da söyleyebilirsiniz.
Teşekkürler!
SELECT
"FİRMA KODU" = CLCARD.CODE,
"FİRMA ADI" = CLCARD.DEFINITION_,
"MAĞAZA KART NU" = CLCARD.CODE,
"İL" = CLCARD.CITY,
"İLÇE" = CLCARD.TOWN,
"MUHASEBE KODU" = EMUHACC.CODE,
"MUHASEBE HESAP ADI" = EMUHACC.DEFINITION_,
"GEÇMİŞ" = (CASE
WHEN DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) < GETDATE()
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"0-14" = (CASE
WHEN DATEADD(DAY, 15, GETDATE()) > DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) > GETDATE()
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"15-30" = (CASE
WHEN DATEADD(DAY, 30, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 15, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"31-45" = (CASE
WHEN DATEADD(DAY, 45, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 31, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"46-60" = (CASE
WHEN DATEADD(DAY, 60, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 46, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"61-90" = (CASE
WHEN DATEADD(DAY, 90, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 61, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"+90" = (CASE
WHEN DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) > DATEADD(DAY, 90, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) AS [BAKİYE (TL)],
ROUND(SUM((CASE CLFLINE.TRCURR WHEN 1 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) AS [BAKİYE (USD)],
ROUND(SUM((CASE CLFLINE.TRCURR WHEN 20 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) AS [BAKİYE (EUR)]
FROM LG_211_01_CLFLINE CLFLINE
LEFT OUTER JOIN LG_211_CLCARD CLCARD ON CLFLINE.CLIENTREF = CLCARD.LOGICALREF
LEFT OUTER JOIN LG_211_EMUHACC EMUHACC ON CLFLINE.CLACCREF = EMUHACC.LOGICALREF
LEFT OUTER JOIN LG_211_PAYPLANS PAYPLANS ON CLFLINE.PAYDEFREF = PAYPLANS.LOGICALREF
LEFT OUTER JOIN LG_211_PAYLINES PAYLINES ON PAYPLANS.LOGICALREF = PAYLINES.PAYPLANREF
LEFT OUTER JOIN LG_211_01_CLFICHE CLFICHE ON CLFLINE.SOURCEFREF = CLFICHE.LOGICALREF
LEFT OUTER JOIN LG_211_01_INVOICE INVOICE ON EMUHACC.LOGICALREF = INVOICE.ACCOUNTREF
GROUP BY CLCARD.CODE, CLCARD.DEFINITION_, CLCARD.CODE, CLCARD.CITY, CLCARD.TOWN, EMUHACC.CODE, EMUHACC.DEFINITION_, CLFLINE.DATE_, PAYLINES.DAY_,
CASE
WHEN DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) < GETDATE()
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 15, GETDATE()) > DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) > GETDATE()
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 30, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 15, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 45, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 31, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 60, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 46, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 90, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 61, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) > DATEADD(DAY, 90, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END
Hesapladığım kolonları GROUP BY'a alamıyorum. Gönlüm ister ki bunları toplaya toplaya bi group by yapsın.
Koddaki amaç vadesi geçen, vade süresi 0-14 gün olan işlemleri bulup girdi-çıktıları toplama. firma ve vade bazında toplamak istiyorum bunları. SQL bana "Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause." diyor :(
Query ile ilgili başka tavsiyeniz veya öneriniz varsa da söyleyebilirsiniz.
Teşekkürler!
SELECT
"FİRMA KODU" = CLCARD.CODE,
"FİRMA ADI" = CLCARD.DEFINITION_,
"MAĞAZA KART NU" = CLCARD.CODE,
"İL" = CLCARD.CITY,
"İLÇE" = CLCARD.TOWN,
"MUHASEBE KODU" = EMUHACC.CODE,
"MUHASEBE HESAP ADI" = EMUHACC.DEFINITION_,
"GEÇMİŞ" = (CASE
WHEN DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) < GETDATE()
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"0-14" = (CASE
WHEN DATEADD(DAY, 15, GETDATE()) > DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) > GETDATE()
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"15-30" = (CASE
WHEN DATEADD(DAY, 30, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 15, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"31-45" = (CASE
WHEN DATEADD(DAY, 45, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 31, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"46-60" = (CASE
WHEN DATEADD(DAY, 60, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 46, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"61-90" = (CASE
WHEN DATEADD(DAY, 90, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 61, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
"+90" = (CASE
WHEN DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) > DATEADD(DAY, 90, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END),
ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) AS [BAKİYE (TL)],
ROUND(SUM((CASE CLFLINE.TRCURR WHEN 1 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) AS [BAKİYE (USD)],
ROUND(SUM((CASE CLFLINE.TRCURR WHEN 20 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) AS [BAKİYE (EUR)]
FROM LG_211_01_CLFLINE CLFLINE
LEFT OUTER JOIN LG_211_CLCARD CLCARD ON CLFLINE.CLIENTREF = CLCARD.LOGICALREF
LEFT OUTER JOIN LG_211_EMUHACC EMUHACC ON CLFLINE.CLACCREF = EMUHACC.LOGICALREF
LEFT OUTER JOIN LG_211_PAYPLANS PAYPLANS ON CLFLINE.PAYDEFREF = PAYPLANS.LOGICALREF
LEFT OUTER JOIN LG_211_PAYLINES PAYLINES ON PAYPLANS.LOGICALREF = PAYLINES.PAYPLANREF
LEFT OUTER JOIN LG_211_01_CLFICHE CLFICHE ON CLFLINE.SOURCEFREF = CLFICHE.LOGICALREF
LEFT OUTER JOIN LG_211_01_INVOICE INVOICE ON EMUHACC.LOGICALREF = INVOICE.ACCOUNTREF
GROUP BY CLCARD.CODE, CLCARD.DEFINITION_, CLCARD.CODE, CLCARD.CITY, CLCARD.TOWN, EMUHACC.CODE, EMUHACC.DEFINITION_, CLFLINE.DATE_, PAYLINES.DAY_,
CASE
WHEN DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) < GETDATE()
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 15, GETDATE()) > DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) > GETDATE()
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 30, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 15, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 45, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 31, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 60, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 46, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, 90, GETDATE()) >= DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) AND DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) >= DATEADD(DAY, 61, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END,
CASE
WHEN DATEADD(DAY, CAST(PAYLINES.DAY_ AS int), CLFLINE.DATE_) > DATEADD(DAY, 90, GETDATE())
THEN ROUND(SUM((CASE CLFLINE.TRCURR WHEN 0 THEN (CLFLINE.TRNET-(SIGN*CLFLINE.TRNET))-(SIGN*CLFLINE.TRNET) ELSE 0 END)),2) ELSE 0 END
Select'te olan tüm kolonları group by içinde de kullanmanız gerekiyor. Hangi sql bu? Syntaxı bilemedim.
- sevilen progressive türkücü (06.06.23 20:20:05)
Microsoft sql sanırım. Hesaplama yaptığım kolonları group by’a alınca "Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause." diyor
- E. (06.06.23 21:57:21)
Caselerden önceki kolonların hepsi yok gibi görüyorum. Sayılar aynı değil. Tekrar inceler misiniz?
- sevilen progressive türkücü (06.06.23 21:59:04)
FİRMA KODU" = CLCARD.CODE,
"FİRMA ADI" = CLCARD.DEFINITION_,
"MAĞAZA KART NU" = CLCARD.CODE,
"İL" = CLCARD.CITY,
"İLÇE" = CLCARD.TOWN,
"MUHASEBE KODU" = EMUHACC.CODE,
"MUHASEBE HESAP ADI" = EMUHACC.DEFINITION_,
GROUP BY CLCARD.CODE, CLCARD.DEFINITION_, CLCARD.CODE, CLCARD.CITY, CLCARD.TOWN, EMUHACC.CODE, EMUHACC.DEFINITION_, CLFLINE.DATE_, PAYLINES.DAY_,
"FİRMA ADI" = CLCARD.DEFINITION_,
"MAĞAZA KART NU" = CLCARD.CODE,
"İL" = CLCARD.CITY,
"İLÇE" = CLCARD.TOWN,
"MUHASEBE KODU" = EMUHACC.CODE,
"MUHASEBE HESAP ADI" = EMUHACC.DEFINITION_,
GROUP BY CLCARD.CODE, CLCARD.DEFINITION_, CLCARD.CODE, CLCARD.CITY, CLCARD.TOWN, EMUHACC.CODE, EMUHACC.DEFINITION_, CLFLINE.DATE_, PAYLINES.DAY_,
- sevilen progressive türkücü (06.06.23 22:01:06)
CLFLINE.DATE_, PAYLINES.DAY_,
Bunlar selectte yok
Bunlar selectte yok
- sevilen progressive türkücü (06.06.23 22:02:41)
SELECT
"xx" = ...,
"yy" = ..,
..
şeklinde yazınca bunları seçmiyor mu select?
CLFLINE.DATE_, PAYLINES.DAY_bunları selectte işlem olarak kullanıyorum, group by yapmak için yazdım aşağıya yoksa sonuç vermiyordu
"xx" = ...,
"yy" = ..,
..
şeklinde yazınca bunları seçmiyor mu select?
CLFLINE.DATE_, PAYLINES.DAY_bunları selectte işlem olarak kullanıyorum, group by yapmak için yazdım aşağıya yoksa sonuç vermiyordu
- E. (07.06.23 09:42:06)
1