查询返回太多结果
Query returning too many results
SQL 查询 return 预期 a.id = 366
的 29 个结果
select a.name, c.name, MAX(B.date), MAX(b.renew_date) as MAXDATE
from boson_course c
inner join boson_coursedetail b on (c.id = b.course_id)
inner join boson_coursedetail_attendance d on (d.coursedetail_id = b.id)
inner join boson_employee a on (a.id = d.employee_id)
where a.id = 366
GROUP BY a.name, c.name
order by MAX(b.renew_date), MAX(b.date) desc;
SQL 代码下方 returns 34 个结果,两个不同的 Provides 提供相同课程的多个结果。我知道这些额外的结果是因为我将 e.name
添加到要 return 的列表中。但所需要的只是具有最新日期和提供商名称的 29 个条目。
select a.name, c.name, e.name, MAX(B.date), MAX(b.renew_date) as MAXDATE
from boson_course c
inner join boson_coursedetail b on (c.id = b.course_id)
inner join boson_coursedetail_attendance d on (d.coursedetail_id = b.id)
inner join boson_employee a on (a.id = d.employee_id)
inner join boson_provider e on b.provider_id = e.id
where a.id = 366
GROUP BY a.name, c.name, e.name
order by MAX(b.renew_date), MAX(b.date) desc;
任何人都可以将此代码修改为 return 每个课程具有 MAX(renew_date)
的单个 DISTINCT 提供商名称。
尝试使用 distinct on
:
select distinct on (a.name, c.name, e.name), a.name, c.name, e.name,
B.date, b.renew_date as MAXDATE
from boson_course c
inner join boson_coursedetail b on (c.id = b.course_id)
inner join boson_coursedetail_attendance d on (d.coursedetail_id = b.id)
inner join boson_employee a on (a.id = d.employee_id)
inner join boson_provider e on b.provider_id = e.id
where a.id = 366
ORDER BY a.name, c.name, e.name, B.date desc
order by MAX(b.renew_date), MAX(b.date) desc;
这个returns正好一个行每个不同的组合(a.name, c.name)
:
最新的 renew_date
.
其中,最新的date
(可能与全局max(date)
不同!)。
其中,字母顺序第一的e.name
:
SELECT DISTINCT ON (a.name, c.name)
a.name AS a_name, c.name AS c_name, e.name AS e_name
, b.renew_date, b.date
FROM boson_course c
JOIN boson_coursedetail b on c.id = b.course_id
JOIN boson_coursedetail_attendance d on d.coursedetail_id = b.id
JOIN boson_employee a on a.id = d.employee_id
JOIN boson_provider e on b.provider_id = e.id
WHERE a.id = 366
ORDER BY a.name, c.name
, b.renew_date DESC NULLS LAST
, b.date DESC NULLS LAST
, e.name;
结果按a_name
、c_name
排序在前。如果您需要 原始排序顺序 ,请将其包装在子查询中:
SELECT *
FROM (<query from above>) sub
ORDER BY renew_date DESC NULLS LAST
, date DESC NULLS LAST
, a_name, c_name, e_name;
DISTINCT ON
的解释:
- Select first row in each GROUP BY group?
为什么 DESC NULL LAST
?
- PostgreSQL sort by datetime asc, null first?
旁白:不要使用基本类型名称,例如 date
广告列名称。此外,name
几乎从来都不是一个好名字。如您所见,我们必须使用别名来使此查询有用。关于命名约定的一些一般建议:
- How to implement a many-to-many relationship in PostgreSQL?
SQL 查询 return 预期 a.id = 366
select a.name, c.name, MAX(B.date), MAX(b.renew_date) as MAXDATE
from boson_course c
inner join boson_coursedetail b on (c.id = b.course_id)
inner join boson_coursedetail_attendance d on (d.coursedetail_id = b.id)
inner join boson_employee a on (a.id = d.employee_id)
where a.id = 366
GROUP BY a.name, c.name
order by MAX(b.renew_date), MAX(b.date) desc;
SQL 代码下方 returns 34 个结果,两个不同的 Provides 提供相同课程的多个结果。我知道这些额外的结果是因为我将 e.name
添加到要 return 的列表中。但所需要的只是具有最新日期和提供商名称的 29 个条目。
select a.name, c.name, e.name, MAX(B.date), MAX(b.renew_date) as MAXDATE
from boson_course c
inner join boson_coursedetail b on (c.id = b.course_id)
inner join boson_coursedetail_attendance d on (d.coursedetail_id = b.id)
inner join boson_employee a on (a.id = d.employee_id)
inner join boson_provider e on b.provider_id = e.id
where a.id = 366
GROUP BY a.name, c.name, e.name
order by MAX(b.renew_date), MAX(b.date) desc;
任何人都可以将此代码修改为 return 每个课程具有 MAX(renew_date)
的单个 DISTINCT 提供商名称。
尝试使用 distinct on
:
select distinct on (a.name, c.name, e.name), a.name, c.name, e.name,
B.date, b.renew_date as MAXDATE
from boson_course c
inner join boson_coursedetail b on (c.id = b.course_id)
inner join boson_coursedetail_attendance d on (d.coursedetail_id = b.id)
inner join boson_employee a on (a.id = d.employee_id)
inner join boson_provider e on b.provider_id = e.id
where a.id = 366
ORDER BY a.name, c.name, e.name, B.date desc
order by MAX(b.renew_date), MAX(b.date) desc;
这个returns正好一个行每个不同的组合(a.name, c.name)
:
最新的 renew_date
.
其中,最新的date
(可能与全局max(date)
不同!)。
其中,字母顺序第一的e.name
:
SELECT DISTINCT ON (a.name, c.name)
a.name AS a_name, c.name AS c_name, e.name AS e_name
, b.renew_date, b.date
FROM boson_course c
JOIN boson_coursedetail b on c.id = b.course_id
JOIN boson_coursedetail_attendance d on d.coursedetail_id = b.id
JOIN boson_employee a on a.id = d.employee_id
JOIN boson_provider e on b.provider_id = e.id
WHERE a.id = 366
ORDER BY a.name, c.name
, b.renew_date DESC NULLS LAST
, b.date DESC NULLS LAST
, e.name;
结果按a_name
、c_name
排序在前。如果您需要 原始排序顺序 ,请将其包装在子查询中:
SELECT *
FROM (<query from above>) sub
ORDER BY renew_date DESC NULLS LAST
, date DESC NULLS LAST
, a_name, c_name, e_name;
DISTINCT ON
的解释:
- Select first row in each GROUP BY group?
为什么 DESC NULL LAST
?
- PostgreSQL sort by datetime asc, null first?
旁白:不要使用基本类型名称,例如 date
广告列名称。此外,name
几乎从来都不是一个好名字。如您所见,我们必须使用别名来使此查询有用。关于命名约定的一些一般建议:
- How to implement a many-to-many relationship in PostgreSQL?