[英]SQL query in where clause group by
我已經在Google上搜索了此內容,但仍然無法為我的問題找到正確的解決方案。
我想要的是在where子句的每個字段中獲取結果。 這是我的代碼:
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;
如果可以看到代碼:
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
)
)
);
在IN (intYear,intYear1,intYear2,intYear3,intYear4)
內部的那個IN (intYear,intYear1,intYear2,intYear3,intYear4)
它們具有不同的年份值。 我想得到每個人的結果。 是否有可能以1比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)
也許您需要做的就是一組工會?
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
一旦將數據“分解”為更多的行和更少的列,則可以對這些行使用GROUP BY和SUM()來獲取“每年的值”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.