MSQuery如何使用allias连接2个表(excel odbc)

时间:2017-09-28 10:25:37

标签: sql excel ms-query

我一直在尝试获取每个ean(id)的最后一行,因为它有几个TimeStamps(tmh),但我无法在MSQuery上使用ODBC conexions ...有人建议吗?

SELECT TMH, PESO, ALTO, ANCHO, LARGO, EAN FROM 

(SELECT TMH219 as TMH, PES219 AS PESO, ALT219 AS ALTO, ANC219 AS ANCHO, LAR219 AS LARGO, EAN219 AS EAN FROM SGAVDL.SGA21900) T1

INNER JOIN (SELECT EAN219, MAX(TMH219) FROM SGAVDL.SGA21900) T2

ON T1.EAN219 = T2.EAN219

GROUP BY  TMH, PESO, ALTO, ANCHO, LARGO, EAN

提前谢谢!

2 个答案:

答案 0 :(得分:1)

使用以下查询,您应该

SELECT T1.TMH219 as TMH, PES219 AS PESO, ALT219 AS ALTO, ANC219 AS ANCHO, LAR219 AS LARGO, T1.EAN219 AS EAN FROM 
SGAVDL.SGA21900 T1 INNER JOIN (SELECT MAX(TMH219) TMH219 FROM SGAVDL.SGA21900) T2
ON T1.TMH219=T2.TMH219

答案 1 :(得分:1)

我可以用一种简单的方式解决它,我没有被第二个表t2分组,而且我没有把TMH的相等放在ON参数中:

SELECT T2.MTMH, T1.PESO, T1.ALTO, T1.ANCHO, T1.LARGO, T1.EAN FROM 
(SELECT TMH219 as TMH, PES219 AS PESO, ALT219 AS ALTO, ANC219 AS ANCHO, LAR219 AS LARGO, EAN219 AS EAN FROM SGAVDL.SGA21900) T1
RIGHT JOIN 
     (SELECT EAN219, MAX(TMH219) AS MTMH FROM SGAVDL.SGA21900 
       WHERE DATE(TMH219) = DATE(CURRENT_DATE) GROUP BY EAN219) T2
ON T1.EAN = T2.EAN219 and t1.TMH = T2.MTMH
GROUP BY EAN, MTMH, PESO, ALTO, ANCHO, LARGO

我认为这或多或少是@Stanislovas的意思,但他很好地简化了

Thnks!