我正在尝试计算找到part_id的最后2个批次的每个part_id的平均时间以及所有批次的每个part_id的平均时间。
我已经设法隔离了找到的最后2个批次的part_id的平均值,但我无法将其集成到代码中,以便对每个零件ID进行计算。我收到错误未知栏' tst.part_id'在' where子句'中,我需要将该tst.part_id值传递给我的嵌套选择查询。
以下是我的小提琴:
http://sqlfiddle.com/#!9/77bea5/85
我正在使用的查询:
#include<stdio.h>
#include<stdlib.h>
#define speedpersecond 5
int main()
{
char time [5];
double distance;
printf("Please enter time in seconds");
gets(time);
distance = atoi(time) * speedpersecond;
printf("Distance is %g ", distance);
return 8585;
}
以下是我想要的内容:
SELECT tst.part_id,
AVG(tst.est_time) AS 'Average Time Overall',
(SELECT AVG(ft.avgLastMax)
FROM
(SELECT t2.avglst as 'avgLastMax', t2.numval as 'numval'
FROM (SELECT
@num:=CASE WHEN @last != tst3.batch_id
THEN @num:=(@num + 1)
ELSE @num:=@num END 'numval',
@last:=tst3.batch_id,
@name:=CASE WHEN @num > 2
THEN @name:=@name
ELSE @name:=(tst3.est_time) END 'avglst'
FROM test AS tst3,
( select @last:=0, @avg := 0, @name :=0 , @num :=0) var
/* GET AVERAGE FOR A SINGLE PART ID
WHERE tst3.part_id = 1 */
WHERE tst3.part_id = tst.part_id
ORDER BY tst3.run_id DESC) as t2
)
as ft
WHERE ft.numval <3) as 'AVG on last 2 batches'
FROM test AS tst
GROUP BY tst.part_id;
表架构:
part_id AVG on last 2 batches Average Time Overall
1 27.25 25.67
2 22.5 22.5
3 16.67 16.67
4 47.5 47.5
已编辑:通过tst3.run_id DESC更正了表和订单以获取最后一个batch_id。
答案 0 :(得分:1)
假设你的第一个结果是错误的......
sqlContext.read.jdbc(...).filter(...)
.select("ORDINAL_POSITION", "COLUMN_NAME")
.rdd
.map { case Row(i: java.math.BigDecimal, c: String) => (i, c) }
或者,更快......带变量......
SELECT a.part_id
, AVG(a.est_time)
FROM test a
JOIN
( SELECT x.part_id
, x.batch_id
FROM
( SELECT DISTINCT part_id
, batch_id
FROM test
) x
JOIN
( SELECT DISTINCT part_id
, batch_id
FROM test
) y
ON y.part_id = x.part_id
AND y.batch_id >= x.batch_id
GROUP
BY x.part_id
, x.batch_id
HAVING COUNT(*) <= 2
) b
ON b.part_id = a.part_id
AND b.batch_id = a.batch_id
GROUP
BY a.part_id;
+---------+-----------------+
| part_id | AVG(a.est_time) |
+---------+-----------------+
| 1 | 27.2500 |
| 2 | 22.5000 |
| 3 | 16.6667 |
| 4 | 47.5000 |
+---------+-----------------+
答案 1 :(得分:0)
SELECT W.*,T1.ALLAVG
FROM (
SELECT V.PART_ID, AVG(V.EST_TIME) LAST2 FROM
(
SELECT U.PART_ID,U.MAXRN,X.RUN_ID,X.EST_TIME
FROM
(
/*LAST 2*/
SELECT S.PART_ID,MAX(S.RN) MAXRN
FROM
(
/*DERIVE A ROW NUMBER*/
SELECT T.PART_ID , T.RUN_ID,
IF (CONCAT(T.PART_ID,T.RUN_ID) <> @R ,@RN:=@RN+1,@RN:=1) RN,
@R:=CONCAT(T.PART_ID,T.RUN_ID) R
FROM (SELECT @RN:=0,@P:=0,@R:=0) RN, TEST T
ORDER BY T.PART_ID,T.RUN_ID
) S
GROUP BY S.PART_ID
) U
/*USING THE MAX ABOVE GET THE LAST 2 */
JOIN
(SELECT T.PART_ID , T.RUN_ID,T.EST_TIME,
IF (CONCAT(T.PART_ID,T.RUN_ID) <> @R1 ,@RN1:=@RN1+1,@RN1:=1) RN,
@R1:=CONCAT(T.PART_ID,T.RUN_ID) R
FROM (SELECT @RN1:=0,@P1:=0,@R1:=0) RN, TEST T
ORDER BY T.PART_ID,T.RUN_ID
) X ON X.PART_ID = U.PART_ID AND (X.RN BETWEEN U.MAXRN -1 AND U.MAXRN) #AMEND THIS FOR THE LAST N REQUIRED
) V
GROUP BY V.PART_ID
) W
JOIN
/*ALL*/
(SELECT T.PART_ID,AVG(T.EST_TIME) 'ALLAVG' FROM TEST T GROUP BY T.PART_ID) T1 ON T1.PART_ID = W.PART_ID
RESULT
+---------+---------+---------+
| PART_ID | LAST2 | ALLAVG |
+---------+---------+---------+
| 1 | 32.5000 | 25.6667 |
| 2 | 22.5000 | 22.5000 |
| 3 | 17.5000 | 16.6667 |
| 4 | 47.5000 | 47.5000 |
+---------+---------+---------+