我有关于重复行的问题。
我的目的是这样的:
你能帮我解决一下这样的查询吗?
我一直使用的代码如下:
select a.id_task1, b.id_task, b.task, b.cycle_sequence_number, c.cycle_sequence_number FROM task_table a LEFT OUTER JOIN billing_cycle b ON (a.id_task1=b.id_task) LEFT OUTER JOIN header c ON (b.cycle_sequence_number = c.cycle_sequence_number) where a.id_task1< 23 order by b.cycle_sequence_number DESC;
此致 - 三 -
抱歉,先生,我忘了发布我的代码:
select a.id_task1, b.id_task, b.task, b.cycle_sequence_number, c.cycle_sequence_number
FROM task_table a
LEFT OUTER JOIN billing_cycle b ON (a.id_task1=b.id_task)
LEFT OUTER JOIN header c ON (b.cycle_sequence_number = c.cycle_sequence_number)
where a.id_task1< 23
order by b.cycle_sequence_number DESC;
答案 0 :(得分:1)
在关系数据库中,查询的结果是关系/表('relation'和'table'是同义表达式)。因此,让我们为您的查询命名并使用group by
s:
select id_task1, id_task, task, max(b.cycle_sequence_number) as b_cycle_sequence_number, max(c.cycle_sequence_number) as c_cycle_sequence_number
from (...) t
group by id_task1, id_task, task
order by b_cycle_sequence_number
您应将b.cycle_sequence_number
解释为b_cycle_sequence_number
,将c.cycle_sequence_number
解释为c_cycle_sequence_number
,以避免重复的列名称。很抱歉没有重写您的实际代码,但我拒绝从图片中输入您的代码。
编辑:真正的代码是:
select a.id_task1, b.id_task, b.task, b.cycle_sequence_number, c.cycle_sequence_number FROM task_table a LEFT OUTER JOIN billing_cycle b ON (a.id_task1=b.id_task) LEFT OUTER JOIN header c ON (b.cycle_sequence_number = c.cycle_sequence_number) where a.id_task1< 23 order by b.cycle_sequence_number DESC;
建议:
SELECT id_task1, id_task, task, max(b.cycle_sequence_number) as b_cycle_sequence_number, max(c.cycle_sequence_number) as c_cycle_sequence_number
FROM (SELECT a.id_task1, b.id_task, b.task, b.cycle_sequence_number as b_cycle_sequence_number, c.cycle_sequence_number as c_cycle_sequence_number
FROM task_table a
LEFT OUTER JOIN billing_cycle b
ON (a.id_task1=b.id_task)
LEFT OUTER JOIN header c
ON (b.cycle_sequence_number = c.cycle_sequence_number) where a.id_task1< 23) t
GROUP BY id_task1, id_task, task
ORDER BY b_cycle_sequence_number