Ask the Expert

Is the following correct?

Is the following correct?
"SELECT

    Requires Free Membership to View

D.SEVK,(SUM(D.MIKTAR) - SUM(D.SEVK)) AS KALAN,S.CARI_KODU,S.TARIHI,D.STOK_KODU,D.SATISSIPARIS_NO,ST.ADI,D.MIKTAR,D.FIYATI, (D.MIKTAR*D.FIYATI) AS TUTAR,ST.BIRIM_KODU, D.INDIRIMORANI FROM KALE02.STOK ST, KALE02.SATISSIPARIS S, KALE02.SATISSIPARISDETAY D WHERE S.NO=D.SATISSIPARIS_NO AND D.STOK_KODU=ST.KODU GROUP BY D.STOK_KODU,D.SATISSIPARIS_NO"


No. Whenever you want to do an aggregate function like SUM(), this requires a GROUP BY, which you have, but the GROUP BY must include all columns mentioned in the SELECT list that are not involved in an aggregate function. Therefore, what you should try is

SELECT D.SEVK
     , (SUM(D.MIKTAR)-SUM(D.SEVK)) AS KALAN
     , S.CARI_KODU
     , S.TARIHI
     , D.STOK_KODU
     , D.SATISSIPARIS_NO
     , ST.ADI
     , D.MIKTAR
     , D.FIYATI
     , (D.MIKTAR*D.FIYATI) AS TUTAR
     , ST.BIRIM_KODU
     , D.INDIRIMORANI
  FROM KALE02.STOK ST
     , KALE02.SATISSIPARIS S
     , KALE02.SATISSIPARISDETAY D
 WHERE S.NO=D.SATISSIPARIS_NO
   AND      D.STOK_KODU=ST.KODU
 GROUP BY
       D.SEVK
     , S.CARI_KODU
     , S.TARIHI
     , D.STOK_KODU
     , D.SATISSIPARIS_NO
     , ST.ADI
     , D.MIKTAR
     , D.FIYATI
     , ST.BIRIM_KODU
     , D.INDIRIMORANI

Notice that (D.MIKTAR*D.FIYATI) AS TUTAR is not included in the GROUP BY because the individual columns are.

By the way, this may not give you the results you really want, but it is the correct way to code your GROUP BY.

For More Information


This was first published in May 2001

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: