我正在尝试创建一个 View ,该 View 显示第一个表的列以及第二个表的前 3 条记录(按日期排序)。
我尝试使用子表的偏移量选择特定行并连接到主表,但是当连接查询结果按日期排序时,没有
WHERE tblMain_id = ..
连接 SQL 的子句返回错误的记录。
这是 sqlfiddle 示例:sqlfiddle demo
tblMain
| id | fname | lname | salary |
+----+-------+-------+--------+
| 1 | John | Doe | 1000 |
| 2 | Bob | Ross | 5000 |
| 3 | Carl | Sagan | 2000 |
| 4 | Daryl | Dixon | 3000 |
tblSub
| id | email | emaildate | tblmain_id |
+----+-----------------+------------+------------+
| 1 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7933161117393d161c48571a1614" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-01-01 | 1 |
| 2 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="02486d6a6c42466d67302c616d6f" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-01-02 | 1 |
| 3 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="753f1a1d1b35311a10465b161a18" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-01-03 | 1 |
| 4 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="480a272a081a273b3b79662b2725" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-02-01 | 2 |
| 5 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d290bdb09280bda1a1e0fcb1bdbf" rel="noreferrer noopener nofollow">[email protected]</a> | 2018-12-01 | 2 |
| 6 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f2c0e1d032f3c0e080e01410c0002" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-10-01 | 3 |
| 7 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b4f6a7972674b4f6273646525686466" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-11-01 | 4 |
查看我想要实现的目标:
| id | fname | lname | salary | email_1 | emaildate_1 | email_2 | emaildate_2 | email_3 | emaildate_3 |
+----+-------+-------+--------+---------------+-------------+---------------+-------------+---------------+-------------+
| 1 | John | Doe | 1000 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="175d787f7957537872263974787a" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-01-01 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6ac898e88a6a28983d4c885898b" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-01-02 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e143136301e1a313b6d703d3133" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-01-03 |
查看我创建的
| id | fname | lname | salary | email_1 | emaildate_1 | email_2 | emaildate_2 | email_3 | emaildate_3 |
+----+-------+-------+--------+---------+-------------+---------------+-------------+---------------+-------------+
| 1 | John | Doe | 1000 | (null) | (null) | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="df95b0b7b19f9bb0baeef1bcb0b2" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-01-01 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b210403052b2f040e5945080406" rel="noreferrer noopener nofollow">[email protected]</a> | 2019-01-02 |
最佳答案
您可以使用条件聚合:
select m.id, m.fname, m.lname, m.salary,
max(s.email) filter (where seqnum = 1) as email_1,
max(s.emailDate) filter (where seqnum = 1) as emailDate_1,
max(s.email) filter (where seqnum = 2) as email_2,
max(s.emailDate) filter (where seqnum = 3) as emailDate_2,
max(s.email) filter (where seqnum = 3) as email_3,
max(s.emailDate) filter (where seqnum = 3) as emailDate_3
from tblMain m left join
(select s.*,
row_number() over (partition by tblMain_id order by emailDate desc) as seqnum
from tblsub s
) s
on s.tblMain_id = m.id
where m.id = 1
group by m.id, m.fname, m.lname, m.salary;
Here是一个 SQL Fiddle。
关于sql - 每行到列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58020205/