[英]SQL query in where clause group by
I already searched on Google for this but still I can't get the correct solution for my problem. 我已经在Google上搜索了此内容,但仍然无法为我的问题找到正确的解决方案。
All I want is to get the result in each field in where clause. 我想要的是在where子句的每个字段中获取结果。 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. 在
IN (intYear,intYear1,intYear2,intYear3,intYear4)
内部的那个IN (intYear,intYear1,intYear2,intYear3,intYear4)
它们具有不同的年份值。 and i want to get the result of each of them. 我想得到每个人的结果。 Is it possible to get their result 1 by 1?
是否有可能以1比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" 一旦将数据“分解”为更多的行和更少的列,则可以对这些行使用GROUP BY和SUM()来获取“每年的值”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.