Is the following correct?
"SELECT
Requires Free Membership to View
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
- What do you think about this answer? E-mail us at editor@searchDatabase.com with your feedback.
- The Best Microsoft SQL Server Web Links: tips, tutorials, scripts, and more.
- The Best SQL Web Links
- Have a SQL tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
- Ask your technical SQL questions--or help out your peers by answering them--in our live discussion forums.
- Ask the Experts yourself: Our SQL guru is waiting to answer your technical questions.
This was first published in May 2001

Join the conversationComment
Share
Comments
Results
Contribute to the conversation