2017-10-24 1 views
0

Ich suchte bereits auf Google für diese, aber immer noch kann ich nicht die richtige Lösung für mein Problem bekommen.SQL-Abfrage in Where-Klausel-Gruppe von

Alles, was ich will, ist das Ergebnis in jedem Feld in Where-Klausel zu erhalten. Hier ist mein Code:

BEGIN 
    -- INSERT INTO tmp_sr_accountsales (REFERENCENO, CUSTOMER, TransDate, SALESTYPE, STDTERMS, Amount) 
    SELECT  act.referenceno, 
      act.customer, 
      act.atdate transdate, 
      act.salestype, 
      cust.stdterms, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount2, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount3, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount4, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount5, 
      intyear              AS intyear, 
      intyear1             AS intyear2, 
      intyear2             AS intyear3, 
      intyear3             AS intyear4, 
      intyear4             AS intyear5 
    FROM  100 _actual_transaction act 
    INNER JOIN 000 _customer cust 
    ON   ( 
         act.customer = cust.customername) 
    WHERE  ( 
         act.referenceno IS NOT NULL 
      AND  act.customer LIKE thecustomer 
      AND  act.salestype LIKE thesalestype 
      AND  ( 
            year(act.atdate) IN(intyear, 
                 intyear1, 
                 intyear2, 
                 intyear3, 
                 intyear4))); 

END; 

Wenn Sie den Code sehen:

WHERE 
(
    act.REFERENCENO IS NOT NULL 
    AND act.CUSTOMER LIKE theCustomer 
    AND act.SALESTYPE LIKE theSalesType 
    AND(
     YEAR(act.ATDATE) IN(
      intYear, 
      intYear1, 
      intYear2, 
      intYear3, 
      intYear4 
     ) 
    ) 
); 

That ein, die innerhalb der IN (intYear,intYear1,intYear2,intYear3,intYear4) war sie unterschiedlichen Jahreswert. und ich möchte das Ergebnis von jedem von ihnen bekommen. Ist es möglich, ihr Ergebnis 1 zu 1 zu bekommen? Da das Ergebnis dieses Codes nur alle Daten hinzufügen wird, die in dieser Abfrage ausgewählt wurden.

+0

Tag des dbms Sie‘ wieder verwenden. (Einige produktspezifische Funktionen wurden verwendet.) – jarlh

+2

Versuchen Sie, die Codeformatierung zu verbessern. Wenn ich zum Lesen blättern muss, gehe ich einfach. – jarlh

+1

Dies ist kein SQL Server. Die 'IFNULL'-Funktion existiert dort nicht. –

Antwort

1

Wenn Sie richtig verstanden haben, möchten Sie die Daten nach Jahr gruppieren. Sie können diese Abfrage versuchen.

SELECT 
     act.REFERENCENO, 
     act.CUSTOMER, 
     act.ATDATE TransDate, 
     act.SALESTYPE, 
     cust.STDTERMS, 
     SUM(IFNULL(act.TOTALAMOUNT, 0) - IFNULL(act.DISCOUNTAMNT, 0)) AS Amount, 
     YEAR(act.ATDATE) AS intYear 
    FROM 100_actual_transaction act 
     INNER JOIN 000_customer cust ON (act.CUSTOMER = cust.CUSTOMERNAME) 
    WHERE (act.REFERENCENO IS NOT NULL 
     AND act.CUSTOMER LIKE theCustomer 
     AND act.SALESTYPE LIKE theSalesType 
     AND (YEAR(act.ATDATE)IN (intYear,intYear1,intYear2,intYear3,intYear4))); 
    GROUP BY 
     act.REFERENCENO, 
     act.CUSTOMER, 
     act.ATDATE TransDate, 
     act.SALESTYPE, 
     cust.STDTERMS, 
     YEAR(act.ATDATE) 
+0

ja richtig, ich will sie als eine Gruppe nach Jahr dann wenn Sie die SUMME sehen können (IFNULL (act.TOTALAMOUNT, 0) - IFNULL (act.DISCOUNTAMNT, 0)) AS Betrag, ich möchte die jeden Betrag von jedem Jahr erhalten also genau so: in intYear das gleiche wie 2017 .. der Gesamtbetrag in diesem Jahr ist genau so wie diese 9.782.123,00 danach möchte ich auch den 2. IntYear1 Gesamtbetrag für dieses Jahr nur sagen wir IntYear1 ist gleich 216 also ich auch möchte den Gesamtbetrag dieses Jahres erhalten. Hast du meinen Standpunkt verstanden? bitte helfen Sie uns –

+0

Möchten Sie das aktuelle Jahr mit früheren Jahren summieren? Wollen Sie kumulative Gesamtsumme nach Jahren? –

+0

ja, ich möchte die Gesamtmenge nach Jahren. Ich möchte jeden Betrag in jedem Jahr bekommen. Kannst du mir helfen, welche Frage ich verwenden werde? eigentlich, wenn ich nur 1 von ihnen abfrage, werde ich die Gesamtmenge dieses bestimmten Jahres bekommen. Ich versuche, sie 1 zu 1 abzufragen, damit ich jedes Ergebnis in jedem Jahr bekomme, aber traurig zu sagen, dass es nicht funktioniert hat. –

1

Vielleicht ist alles, was Sie tun müssen, eine Reihe von Gewerkschaften?

SELECT  intyear             AS Yr, 
      act.referenceno, 
      act.customer, 
      act.atdate transdate, 
      act.salestype, 
      cust.stdterms, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount 
    FROM  100 _actual_transaction act 
    INNER JOIN 000 _customer cust ON act.customer = cust.customername) 
    WHERE act.referenceno IS NOT NULL 
      AND  act.customer LIKE thecustomer 
      AND  act.salestype LIKE thesalestype 
      AND  year(act.atdate) = intyear 

UNION ALL 

    SELECT  intyear1             AS Yr, 
      act.referenceno, 
      act.customer, 
      act.atdate transdate, 
      act.salestype, 
      cust.stdterms, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount 
    FROM  100 _actual_transaction act 
    INNER JOIN 000 _customer cust ON act.customer = cust.customername) 
    WHERE act.referenceno IS NOT NULL 
      AND  act.customer LIKE thecustomer 
      AND  act.salestype LIKE thesalestype 
      AND  year(act.atdate) = intyear1 

UNION ALL 

    SELECT  intyear2             AS Yr, 
      act.referenceno, 
      act.customer, 
      act.atdate transdate, 
      act.salestype, 
      cust.stdterms, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount 
    FROM  100 _actual_transaction act 
    INNER JOIN 000 _customer cust ON act.customer = cust.customername) 
    WHERE act.referenceno IS NOT NULL 
      AND  act.customer LIKE thecustomer 
      AND  act.salestype LIKE thesalestype 
      AND  year(act.atdate) = intyear2 

UNION ALL 

    SELECT  intyear3             AS Yr, 
      act.referenceno, 
      act.customer, 
      act.atdate transdate, 
      act.salestype, 
      cust.stdterms, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount 
    FROM  100 _actual_transaction act 
    INNER JOIN 000 _customer cust ON act.customer = cust.customername) 
    WHERE act.referenceno IS NOT NULL 
      AND  act.customer LIKE thecustomer 
      AND  act.salestype LIKE thesalestype 
      AND  year(act.atdate) = intyear3 

UNION ALL 

    SELECT  intyear4             AS Yr, 
      act.referenceno, 
      act.customer, 
      act.atdate transdate, 
      act.salestype, 
      cust.stdterms, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount 
    FROM  100 _actual_transaction act 
    INNER JOIN 000 _customer cust ON act.customer = cust.customername) 
    WHERE act.referenceno IS NOT NULL 
      AND  act.customer LIKE thecustomer 
      AND  act.salestype LIKE thesalestype 
      AND  year(act.atdate) = intyear4 

UNION ALL 

    SELECT  intyear5             AS Yr, 
      act.referenceno, 
      act.customer, 
      act.atdate transdate, 
      act.salestype, 
      cust.stdterms, 
      Ifnull(act.totalamount, 0)- Ifnull(act.discountamnt, 0) AS amount 
    FROM  100 _actual_transaction act 
    INNER JOIN 000 _customer cust ON act.customer = cust.customername) 
    WHERE act.referenceno IS NOT NULL 
      AND  act.customer LIKE thecustomer 
      AND  act.salestype LIKE thesalestype 
      AND  year(act.atdate) = intyear5 

Sobald Sie „unpivotted“ die Daten in mehr Zeilen und weniger Spalten haben, können Sie dann GROUP BY und SUM() über die Zeilen verwenden, um „pro Jahr Werte“

+0

danke für Ihre Hilfe. aber es hat immer noch nicht funktioniert. Es wird nur die erste Abfrage angezeigt, nur die erste intYear-Abfrage funktioniert. –

+0

es hat immer noch nicht funktioniert .. nur die erste Abfrage funktioniert. die anderen haben nicht funktioniert, sogar ich benutze alle. irgendein Vorschlag? –

+1

"Irgendwelche Vorschläge"? JA! 1: ** liefern einige Beispieldaten ** 2: dann ** ein ** erwartetes Ergebnis ** basierend auf dieser Probe. Es sind möglicherweise nur 6 oder so viele Datenzeilen (mindestens eine pro Jahr), Sie können alles ausblenden, was privat ist.Verwenden Sie keine Bilder, verwenden Sie formatierten Text, HINZUFÜGEN SIE IHRE FRAGE. Keine Anzahl von Wörtern wird diese 2 Aktionen ersetzen –

Verwandte Themen