MySQL:计算范围数据

时间:2012-06-21 02:26:16

标签: mysql sum where-clause

我正在使用以下查询来计算每日结果,但现在我只是简短地显示2012-06-01中的数据结果。

SELECT DATE(A.Inspection_datetime) AS Date,A.Line,
       TRIM(A.Model) AS Model, A.Lot_no, COUNT(A.Serial_number) AS Qty,
       B.name
FROM inspection_report AS A
LEFT JOIN Employee AS B
ON A.NIK=B.NIK
WHERE A.Inspection_datetime LIKE '2012-06-01%'
GROUP BY DATE(A.Inspection_datetime), TRIM(A.Model), B.name,A.Line,A.Lot_no

我的结果如下:

Date        Line       Model        Lot_no  Qty name
2012-06-01  FA 24   DDX6051BTQD     014A    4   Ben
2012-06-01  FA 24   DDX6051BTQD     014A    4   August
2012-06-01  FA 24   DDX630WBTXD     012A    8   August
2012-06-01  FA 17   KD-DV5506UPD    021A    20  Stue
2012-06-01  FA 17   KD-DV5606UD     114A    25  Carl
2012-06-01  FA 17   KD-DV5606UD     114A    3   Teo

如果我想在直到目标数据之前计算数据怎么办从DDX6051BTQD开始计算到KD-DV5506UPD。所以,我只是得到结果LIKE:

Date          Qty
2012-06-01    36

尝试:

SELECT X.DATE, SUM(X.Qty) FROM (//insert query above//)
WHERE ??????? //how to do ?

1 个答案:

答案 0 :(得分:0)

您可以尝试下面的内容;​​

SET @userValueFrom := 'DDX6051BTQD';
SET @userValueTo :='KD-DV5506UPD';

SELECT * FROM (//insert your query above//) 
where Model >= @userValueFrom and Model <= @userValueTo ;