[英]SQL Server : joining a table to itself?
I'm having issues joining a table on itself, or actually on a complex query already involving this table.我在加入一个表本身时遇到问题,或者实际上在一个已经涉及这个表的复杂查询上。 Let me explain what exactly I mean by it.让我解释一下我的意思。
Currently I have this select:目前我有这个选择:
SELECT
t.maticna, t.clientNo, t.letoZr, t.letoZrChr, t.sort,
t.tipOcene, t.bonitetniModel, t.datumOtvoritve, t.DatumKLJ,
t.ocm1solor_LT, t.ocm1solo_LT, t.ocm1solor_ST, t.ocm1solo_ST
FROM
(SELECT
a.maticna, a.clientNo, a.letoZr, a.letoZrChr, a.sort,
a.tipOcene, a.bonitetniModel, a.datumOtvoritve,
im.PKModul1_LT as ocm1solor_LT, im.PDModul1_LT*100 as ocm1solo_LT,
im.PKModul1_ST as ocm1solor_ST, im.PDModul1_ST*100 as ocm1solo_ST,
im.DatumKLJ,
ROW_NUMBER() OVER (PARTITION BY a.letoZrChr ORDER BY datumOtvoritve DESC, im.DatumKLJ DESC) AS rn
FROM
#osnovna a
LEFT JOIN
airb.bm.IzhodModeli im ON a.clientNo = im.KomitentID
AND im.ReferencniDatum = a.datumOtvoritve
AND a.tipocene = im.tipocene) t
WHERE
t.rn = 1
ORDER BY
t.DatumKLJ DESC
What it does is, is returns these 5 rows:它的作用是返回这 5 行:
maticna clientNo letoZr letoZrChr sort tipOcene bonitetniModel datumOtvoritve DatumKLJ ocm1solor_LT ocm1solo_LT ocm1solor_ST ocm1solo_ST
5046432 975145 2019 ML2019 4 2 GD 2019-06-30 43900 4 0,868 3 0,565
5046432 975145 2018 ZR2018 3 0 GD 2018-12-31 43900 8 34,425 8 24,269
5046432 975145 2017 ZR2017 2 0 GD 2017-12-31 43612 5 2,622 4 1,173
5046432 975145 2016 ZR2016 1 0 GD 2016-12-31 43355 4 1,405 3 0,588
5046432 975145 2019 PL2019 5 3 GD 2019-12-31 NULL NULL NULL NULL NULL
Now, long story short, I want the select to also show the last 4 columns of the table im (ocm1solor_LT, ocm1solo_LT, ocm1solor_ST, ocm1solo_ST ), where:现在,长话短说,我希望选择还显示表 im 的最后 4 列(ocm1solor_LT、ocm1solo_LT、ocm1solor_ST、ocm1solo_ST),其中:
if tipOcene is currently 2, then I want to add to that row the 4 columns written above where tipOcene=6, and DatumKLJ, DatumOtvoritve and clientNo are the same.如果tipOcene 当前为2,那么我想将上面写的4 列添加到该行,其中tipOcene=6,并且DatumKLJ、DatumOtvoritve 和clientNo 是相同的。
if tipOcene is currently 0, then I want to add to that row the same 4 columns, but tipOcene=1.如果tipOcene 当前为0,那么我想向该行添加相同的4 列,但tipOcene=1。
and if tipOcene is 3, then I want to add to that row the same 4 columns, but with tipOcene=7.如果tipOcene 是3,那么我想向该行添加相同的4 列,但tipOcene=7。
The problem I'm having is, no matter how I try to JOIN the "im" table on this, I keep ending up with more results.我遇到的问题是,无论我如何尝试加入“im”表,我都会得到更多结果。 So if anybody has any suggestions how to make a JOIN, in the manner that it wont add any rows, but it'll only add the 4 columns written above to the end of the current rows, except for a different tipOcene, it'd be much appreciated.因此,如果有人对如何进行 JOIN 有任何建议,以它不会添加任何行的方式,但它只会将上面写的 4 列添加到当前行的末尾,除了不同的tipOcene,它会不胜感激。
I hope the instructions of what I want are at least a bit clear lol, if not, please do feel free to ask and I'll be happy to explain, since I'm really struggling with it...我希望我想要的说明至少有点清楚,哈哈,如果没有,请随时提问,我很乐意解释,因为我真的很挣扎......
谢谢基思,在 WHERE 上的案例的左连接实际上完成了这项工作,不知道我是如何错过之前的,哈哈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.