Soner Gönül
Soner Gönül

Reputation: 98740

SQL Query Separate Date

I have a sql good working like this.

SELECT  B.HESAP_NO, A.TEKLIF_NO1 + '/' + A.TEKLIF_NO2 AS 'TEKLIF',
B.MUS_K_ISIM, CONVERT(VARCHAR(10),A.ISL_TAR,103) AS 'TARIH',
SUM(ISNULL(CAST(A.ODENEN_ANAPARA AS FLOAT),0)+ISNULL(CAST(A.FAIZ AS FLOAT),0)+
    ISNULL(CAST(A.BSMV AS FLOAT),0)+ISNULL(CAST(A.GECIKME_FAIZ AS FLOAT),0)+
    ISNULL(CAST(A.GECIKME_BSMV AS FLOAT),0)) AS 'YATAN', 
    (CASE WHEN B.DOVIZ_KOD = 21 THEN 'EUR' WHEN B.DOVIZ_KOD = 2 THEN 'USD' WHEN B.DOVIZ_KOD = 1 THEN 'TL' END) AS 'KUR',
    D.AVUKAT, CONVERT(VARCHAR(10),C.ICRA_TAR,103) AS 'İCRA TARİHİ', CONVERT(VARCHAR(10),C.HACIZ_TAR,103) AS 'HACİZ TARİHİ'

FROM YAZ..MARDATA.BIR_TAHSIL A, YAZ..MARDATA.S_TEKLIF B, TAKIP C, AVUKAT D, P_TAKIP_SR E

WHERE A.TEKLIF_NO1 = B.TEKLIF_NO1
AND A.TEKLIF_NO2 = B.TEKLIF_NO2
AND B.HESAP_NO = D.HESAP
AND D.HESAP = A.HESAP_NO

And i add a date range with code like this.

if (txtBoxText1 != "")
    {
        strQuery = strQuery + " AND A.ISL_TAR >= @S_TARIH_B";

      dt_stb = DateTime.Parse(txtBoxText1);
      myCommand.Parameters.AddWithValue("@S_TARIH_B", dt_stb);
    }

    if (txtBoxText2 != "")
    {
        strQuery = strQuery + " AND A.ISL_TAR <= @S_TARIH_S";
      dt_sts = DateTime.Parse(txtBoxText2);
      myCommand.Parameters.AddWithValue("@S_TARIH_S", dt_sts);
    }

For the last i add GROUP BY

GROUP BY  B.HESAP_NO, A.TEKLIF_NO1 + '/' + A.TEKLIF_NO2,A.ISL_TAR,B.DOVIZ_KOD, B.HESAP_NO, B.MUS_K_ISIM, D.AVUKAT, C.ICRA_TAR, C.HACIZ_TAR

But this query gettig result for example like this (part of some)

HESAP_NO       MUS_K_ISIM     YATAN  Date
889          2M LOJİSTİK      7090   28/03/2010

Just getting one record for 28/03/2010. But in the database, in this date, there are two records.

HESAP_NO       MUS_K_ISIM     YATAN  Date
889          2M LOJİSTİK      5000   28/03/2010
889              2M LOJİSTİK      2090   28/03/2010

As you see (5000 + 2090 = 7090) adding all YATAN (number) and getting one record the top query.

What i want is, the getting all records in the same date like second result.

  HESAP_NO     MUS_K_ISIM     YATAN  Date
    889          2M LOJİSTİK      5000   28/03/2010
    889              2M LOJİSTİK      2090   28/03/2010

How can i do that?

Regars, Soner

Upvotes: 2

Views: 198

Answers (2)

Andriy M
Andriy M

Reputation: 77657

From what I see in your code and in your description, you should probably remove SUM() as nybbler has suggested and add these columns to GROUP BY:

A.ODENEN_ANAPARA, A.FAIZ, A.BSMV, A.GECIKME_FAIZ, A.GECIKME_BSMV

Upvotes: 1

nybbler
nybbler

Reputation: 4841

This is happening because of your SUM function in your SELECT:

SUM(ISNULL(CAST(A.ODENEN_ANAPARA AS FLOAT),0)+ISNULL(CAST(A.FAIZ AS FLOAT),0)+
    ISNULL(CAST(A.BSMV AS FLOAT),0)+ISNULL(CAST(A.GECIKME_FAIZ AS FLOAT),0)+
    ISNULL(CAST(A.GECIKME_BSMV AS FLOAT),0)) AS 'YATAN',

Try replacing this with:

ISNULL(CAST(A.ODENEN_ANAPARA AS FLOAT),0)+ISNULL(CAST(A.FAIZ AS FLOAT),0)+
    ISNULL(CAST(A.BSMV AS FLOAT),0)+ISNULL(CAST(A.GECIKME_FAIZ AS FLOAT),0)+
    ISNULL(CAST(A.GECIKME_BSMV AS FLOAT),0) AS 'YATAN',

Which also means you should be able to remove your GROUP BY clause. This should provide you with the results for any unique results in your tables.

Upvotes: 2

Related Questions