我一直在尝试在第4张桌子上加入4个带有case语句的表 不幸的是,我的数据翻了一倍,并不是每张桌子上的最新数据。请帮忙。
QUERY:
SELECT
d.serial_no, d.dummy_serial ,
a.model_id, b.model_id, c.model_id, d.model_id,
MAX(a.create_date), MAX(b.create_date), MAX(c.create_date), MAX(d.create_date)
FROM table1 as a
JOIN table2 as b
ON a.serial_no = b.serial_no
JOIN table3 as c
ON b.serial_no = c.serial_no
JOIN table4 as d
ON c.serial_no =
(CASE WHEN
a.model_id = 'LB31' AND
b.model_id = 'LB31' AND
c.model_id = 'LB31' AND
d.model_id = 'LB31'
THEN d.dummy_serial
ELSE d.serial_no END );
我想要什么
答案 0 :(得分:1)
查询缺少group by
子句。所有未汇总的列都应归入select
。
SELECT
d.serial_no, d.dummy_serial ,
a.model_id, b.model_id, c.model_id, d.model_id,
MAX(a.create_date), MAX(b.create_date), MAX(c.create_date), MAX(d.create_date)
FROM table1 as a
JOIN table2 as b
ON a.serial_no = b.serial_no
JOIN table3 as c
ON b.serial_no = c.serial_no
JOIN table4 as d
ON c.serial_no =
(CASE WHEN
a.model_id = 'LB31' AND
b.model_id = 'LB31' AND
c.model_id = 'LB31' AND
d.model_id = 'LB31'
THEN d.dummy_serial
ELSE d.serial_no END)
group by d.serial_no, d.dummy_serial, a.model_id, b.model_id, c.model_id, d.model_id