我有两个表work_table和progress_table。
work_table有以下列:
id[primary key],
department,
dept_name,
dept_code,
created_time,
updated_time
progress_table有以下列:
id[primary key],
project_id,
progress,
progress_date
我只需要上次更新的进度值就可以在表中更新了,现在我得到了重复值。
这是尝试过的代码:
select
row_number() over (order by a.dept_code asc) AS sno,
a.dept_name,
b.project_id,
p.physical_progress,
DATE(b.updated_time) as updated_date,
b.created_time
from
masters.dept_users as a,
work_table as b
LEFT JOIN
progress as p on b.id = p.project_id
order by
a.dept_name asc
它显示了具有相同id的进度的重复值如何解决?[进度值是整数,其值被馈送到表单]
重新格式化查询后,有些事情变得很清楚。。。
- 您混合了
,
和JOIN
语法(为什么!?( - 您从
masters.dept_users
表开始,但不要在描述中提及它 dept_users
和work_table
之间没有联接谓词- 您计算了一个
sno
,但没有partition by
,并且从未使用过它 - 您的查询包含上表描述中未提及的列
最重要的是,你使用了像a
和b
这样毫无意义的别名?为了他人的爱,以及你未来的自己(总有一天会读到这篇文章(,请让别名在某种程度上有意义。
你可能想要。。。
WITH
sorted_progress AS
(
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY project_id
ORDER BY progress_date DESC -- This may need to be updated_time, your question is very unclear
)
AS seq_num
FROM
progress
)
SELECT
<whatever>
FROM
masters.dept_users AS u
INNER JOIN
work_table AS w
ON w.user_id = u.id -- This is a GUESS, but you need to do SOMETHING here
LEFT JOIN
sorted_progress AS p
ON p.project_id = w.id -- Even this looks suspect, are you SURE that w.id is the project_id?
AND p.seq_num = 1
这至少说明了如何获得最新的进度记录(p.seq_num = 1
(,但其他联接是否正确,您必须自己检查两次(和三次(。