2017-10-24 9 views
0

私はすでにこれについてGoogleで検索しましたが、私の問題の正しい解決策を得ることができません。ここでwhere句グループによるSQLクエリ

私が欲しいのは、where句の各フィールドで結果を得ることです。彼らは別の年の値を持っているIN (intYear,intYear1,intYear2,intYear3,intYear4) の中にあった

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 
     ) 
    ) 
); 

その1:あなたは、コードを見ることができる場合

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; 

:ここに私のコードです。私はそれぞれの結果を得たいと思っています。結果を1ずつ得ることは可能ですか?そのコードの結果は、そのクエリで選択されたすべてのデータを追加するだけであるためです。

+0

タグDBMSあなたに「年の値ごとに」取得するために、これらの行の上にGROUP BYおよびSUM()を使用することができます再使用します。 (一部の製品固有の関数が使用されています) – jarlh

+2

コードの書式設定を改善してください。私が読むために横にスクロールしなければならない場合、私はただ残します。 – jarlh

+1

これはSQL Serverではありません。 'IFNULL'関数はそこに存在しません。 –

答えて

1

正しく理解されている場合は、年単位でデータをグループ化する必要があります。このクエリを試すことができます。

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

はいいいえ私はあなたがSUM(IFNULL(act.TOTALAMOUNT、0) - IFNULL(act.DISCOUNTAMNT、0))の金額を見ることができれば、毎年グループとして欲しいので、毎年各金額を取得したいこれはちょうどこれのように:intYearは2017年に等しい。その年の合計額はこの9,782,123.00のようになります。その年の2番目のintYear1合計金額を取得したいのですが、intYear1が216に等しいと言うことにしましょうその年の合計額を取得したい。私のポイントを得ましたか?助けてください –

+0

今年と前年を合計しますか? Σ平均あなたは年間の累計額を求めていますか? –

+0

はい、合計金額を年単位で欲しいです。私は毎年合計金額のそれぞれを取得したいと思います。私はあなたが私が使用するクエリを助けることができますか?実際に私はそれらの1つだけを照会する場合、私はその特定の年の合計金額を取得します。私はそれらを1つずつ質問しようとするので、毎年それぞれの結果が得られますが、うまくいかないと言って悲しいです。 –

1

多分あなたがする必要があるのは、ユニオンのセットですか?あなたはより多くの行と少ない列に「unpivotted」のデータを持っていたら

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 

、あなたはその後、「

+0

ご協力いただきありがとうございます。それでも動作しませんでした。最初のクエリのみが表示され、最初のintクエリのみが機能します。 –

+0

まだ動作しませんでした。最初のクエリの作業のみです。私は組合全体を使用しても他のものは動作しませんでした。なにか提案を? –

+1

「任意の提案」?はい! 1:**いくつかのサンプルデータを提供する** 2:**そのサンプルに基づいて**予想される結果**。データは6行(少なくとも1年に1回)しかないので、非公開のものはすべて非表示にすることができます。画像を使用せず、フォーマットされたテキストを使用し、あなたの質問に追加してください。 2つのアクションの代わりに言葉の量はありません –