I already searched on Google for this but still I can't get the correct solution for my problem.
All I want is to get the result in each field in where clause. Here is my 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;
If you can see the code :
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 one that was inside the IN (intYear,intYear1,intYear2,intYear3,intYear4)
they are have different year value. and i want to get the result of each of them. Is it possible to get their result 1 by 1? Because the result of that code will just add all of the data that was selected in that query.
If understood correctly you want to group the data by year. You can try this query.
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)
Perhaps all you need to do is a set of unions?
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
Once you have "unpivotted" the data into more rows and fewer columns, you can then use GROUP BY and SUM() over those rows to get "per year values"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.