编辑:在每个代码段下添加了示例结果,并在最后添加了所需的结果。
我一直在浏览几篇文章,但似乎找不到能够准确应用的解决方案。我有一个表r
,其中包含acctnbr
,然后是modnbr
的数据。对于大多数acctnbrs
而言,只有一条记录,而得到的modnbr
为1,但是最多可以有5条记录,其中modnbrs
为1-5。
表u
没有modnbr
,因此我想在acctnbr
上进行左联接,只返回表u
中的值,最大值为{{1} } modnbr
中的acctnbr
}。
这是我的代码,未尝试连接。我需要在连接中包括另一个限定符,如下所示:
r
不出所料,无论select
r.acctnbr,
r.modnbr,
r.modtypcd,
r.moddate,
u.userfieldcd,
u.value
from loanrestructuretracking r
LEFT JOIN acctuserfield u on u.acctnbr = r.acctnbr
and u.userfieldcd = 'LOMO'
order by r.moddate desc
为何,每个u.value
都得到相同的r.acctnbr
。结果如下:
modnbr
现在,这是我尝试查找最大值+---------+--------+----------+------------+-------------+-------+
| acctnbr | modnbr | modtypcd | moddate | userfieldcd | value |
+---------+--------+----------+------------+-------------+-------+
| 1122330 | 1 | LM | 1/1/2018 | LOMO | DLQ |
| 1223340 | 1 | LM | 2/5/2018 | LOMO | DLQ |
| 2233440 | 1 | LM | 6/9/2017 | LOMO | COT |
| 2233440 | 2 | LM | 11/20/2017 | LOMO | COT |
| 2233440 | 3 | LM | 3/12/2018 | LOMO | COT |
| 3344550 | 1 | LM | 4/27/2018 | LOMO | EXT |
| 3344550 | 2 | LM | 6/2/2018 | LOMO | EXT |
+---------+--------+----------+------------+-------------+-------+
的代码,但现在我只看到modnbr
的最大值u.value
的任何实例(在我的情况下, 5。)
modnbr
这是上面脚本的结果。请注意,记录中只有一个select
r.acctnbr,
r.modnbr,
r.modtypcd,
r.moddate,
u.userfieldcd,
u.value
from loanrestructuretracking r
LEFT JOIN acctuserfield u on u.acctnbr = r.acctnbr
and u.userfieldcd = 'LOMO'
and r.modnbr = (select max(modnbr) from loanrestructuretracking)
order by r.moddate desc
,最大值为u.value
:
modnbr
最后,这是期望的结果-每个+---------+--------+----------+------------+-------------+-------+
| acctnbr | modnbr | modtypcd | moddate | userfieldcd | value |
+---------+--------+----------+------------+-------------+-------+
| 1122330 | 1 | LM | 1/1/2018 | LOMO | |
| 1223340 | 1 | LM | 2/5/2018 | LOMO | |
| 2233440 | 1 | LM | 6/9/2017 | LOMO | |
| 2233440 | 2 | LM | 11/20/2017 | LOMO | |
| 2233440 | 3 | LM | 3/12/2018 | LOMO | COT |
| 3344550 | 1 | LM | 4/27/2018 | LOMO | |
| 3344550 | 2 | LM | 6/2/2018 | LOMO | |
+---------+--------+----------+------------+-------------+-------+
的最大值modnbr
的u.value,而不是该{{1}的最大值acctnbr
的任何记录的null }}:
modnbr
我要怎么做才能使每个acctnbr
的最大+---------+--------+----------+------------+-------------+-------+
| acctnbr | modnbr | modtypcd | moddate | userfieldcd | value |
+---------+--------+----------+------------+-------------+-------+
| 1122330 | 1 | LM | 1/1/2018 | LOMO | DLQ |
| 1223340 | 1 | LM | 2/5/2018 | LOMO | DLQ |
| 2233440 | 1 | LM | 6/9/2017 | LOMO | |
| 2233440 | 2 | LM | 11/20/2017 | LOMO | |
| 2233440 | 3 | LM | 3/12/2018 | LOMO | COT |
| 3344550 | 1 | LM | 4/27/2018 | LOMO | |
| 3344550 | 2 | LM | 6/2/2018 | LOMO | EXT |
+---------+--------+----------+------------+-------------+-------+
上的联接起作用?
谢谢!
答案 0 :(得分:1)
我有:
http://www.youtube.com
http://www.facebook.com
http://www.baidu.com
http://www.yahoo.com
http://www.amazon.com
http://www.wikipedia.org
http://www.qq.com
http://www.google.co.in
http://www.twitter.com
http://www.live.com
http://www.taobao.com
http://www.bing.com
http://www.instagram.com
http://www.weibo.com
http://www.sina.com.cn
http://www.linkedin.com
http://www.yahoo.co.jp
http://www.msn.com
http://www.vk.com
http://www.google.de
http://www.yandex.ru
http://www.hao123.com
http://www.google.co.uk
http://www.reddit.com
http://www.ebay.com
http://www.google.fr
http://www.t.co
http://www.tmall.com
http://www.google.com.br
http://www.360.cn
http://www.sohu.com
http://www.amazon.co.jp
http://www.pinterest.com
http://www.netflix.com
http://www.google.it
http://www.google.ru
http://www.microsoft.com
http://www.google.es
http://www.wordpress.com
http://www.gmw.cn
http://www.tumblr.com
http://www.paypal.com
http://www.blogspot.com
http://www.imgur.com
http://www.stackoverflow.com
http://www.aliexpress.com
http://www.naver.com
http://www.ok.ru
http://www.apple.com
http://www.github.com
http://www.chinadaily.com.cn
http://www.imdb.com
http://www.google.co.kr
http://www.fc2.com
http://www.jd.com
http://www.blogger.com
http://www.163.com
http://www.google.ca
http://www.whatsapp.com
http://www.amazon.in
http://www.office.com
http://www.tianya.cn
http://www.google.co.id
http://www.youku.com
http://www.rakuten.co.jp
http://www.craigslist.org
http://www.amazon.de
http://www.nicovideo.jp
http://www.google.pl
http://www.soso.com
http://www.bilibili.com
http://www.dropbox.com
http://www.xinhuanet.com
http://www.outbrain.com
http://www.pixnet.net
http://www.alibaba.com
http://www.alipay.com
http://www.microsoftonline.com
http://www.booking.com
http://www.googleusercontent.com
http://www.google.com.au
http://www.popads.net
http://www.cntv.cn
http://www.zhihu.com
http://www.amazon.co.uk
http://www.diply.com
http://www.coccoc.com
http://www.cnn.com
http://www.bbc.co.uk
http://www.twitch.tv
http://www.wikia.com
http://www.google.co.th
http://www.go.com
http://www.google.com.ph
http://www.doubleclick.net
http://www.onet.pl
http://www.googleadservices.com
http://www.accuweather.com
http://www.googleweblight.com
http://www.answers.yahoo.com
http://www.youtube.com
http://www.facebook.com
http://www.baidu.com
http://www.yahoo.com
http://www.amazon.com
http://www.wikipedia.org
http://www.qq.com
http://www.google.co.in
http://www.twitter.com
http://www.live.com
http://www.taobao.com
http://www.bing.com
http://www.instagram.com
http://www.weibo.com
http://www.sina.com.cn
http://www.linkedin.com
http://www.yahoo.co.jp
http://www.msn.com
http://www.vk.com
http://www.google.de
http://www.yandex.ru
http://www.hao123.com
http://www.google.co.uk
http://www.reddit.com
http://www.ebay.com
http://www.google.fr
http://www.t.co
http://www.tmall.com
http://www.google.com.br
http://www.360.cn
http://www.sohu.com
http://www.amazon.co.jp
http://www.pinterest.com
http://www.netflix.com
http://www.google.it
http://www.google.ru
http://www.microsoft.com
http://www.google.es
http://www.wordpress.com
http://www.gmw.cn
http://www.tumblr.com
http://www.paypal.com
http://www.blogspot.com
http://www.imgur.com
http://www.stackoverflow.com
http://www.aliexpress.com
http://www.naver.com
http://www.ok.ru
http://www.apple.com
http://www.github.com
http://www.chinadaily.com.cn
http://www.imdb.com
http://www.google.co.kr
http://www.fc2.com
http://www.jd.com
http://www.blogger.com
http://www.163.com
http://www.google.ca
没有数据,所以不能百分百确定,但是我认为这将为您提供所需的信息。
max(r.modnbr)超过(由r.acctnbr划分),因为max_modnbr将每个acctnbr的最大modnbr追加到每一行。
然后case语句在不匹配的行上将其替换为null。
可以将整个内容写得更紧凑:
with base_qry as
(
select
r.acctnbr,
r.modnbr,
r.modtypcd,
r.moddate,
u.userfieldcd,
u.value,
max(r.modnbr ) over (partition by r.acctnbr) as max_modnbr
from loanrestructuretracking r
LEFT JOIN acctuserfield u on u.acctnbr = r.acctnbr
and u.userfieldcd = 'LOMO'
)
select
acctnbr,
modnbr,
modtypcd,
moddate,
userfieldcd,
case when modnbr = max_modnbr then value else null end as value
from base_query
order by moddate desc
答案 1 :(得分:0)
我想你想要
select . . .
from (select r.*, row_number() over (partition by r.acctnbr order by r.modnbr desc) as seqnum
from loanrestructuretracking r
) r left join
acctuserfield u
on u.acctnbr = r.acctnbr and
u.userfieldcd = 'LOMO'
where seqnum = 1
order by r.moddate desc;