t1
DocNo Date Description
Doc-12345-67890 15Jan19 QC-Passed
t2
DocNo Date Description
Doc-09876-21347 16Jan19 QC-Passed
t3
DocNo Date Description
12345 15Jan19 QC-Passed
09876 16Jan19 QC-Passed
Select *
from
(
select left(right(docno,len(docno)-3),5) as DocNo,Date,Description
from t1
union all
select *
from t2) as RC
inner join
t3
on
rc.docno=t3.docno
它不起作用,任何人都可以帮忙吗?
答案 0 :(得分:0)
我想你想要
select *
from ((select DocNo, Date, Description
from t1
) union all
(select DocNo, Date, Description
from t2
)
) RC inner join
t3
on left(right(rc.docno, len(rc.docno) - 3), 5) = t3.docno;
或者:
select *
from ((select left(right(rc.docno, len(rc.docno) - 3), 5) as DocNo, Date, Description
from t1
) union all
(select left(right(rc.docno, len(rc.docno) - 3), 5) as DocNo, Date, Description
from t2
)
) RC inner join
t3
on rc.docno = t3.docno;
换句话说,select
的第一个union all
中的表达式不会“携带”到第二个select
中。
答案 1 :(得分:0)
看来您的数据是t3表是驱动程序,它的docno是'Doc-'文本后t1和t2的前五个字符的主要字符。下面的tsql通过使用SubString来获取“ t1”和“ t2” DocNo:
查询:
Select *
from
(
select SubString(DocNo,5,5) as DocNo,dt,Description
from #tbl1
union all
select SubString(DocNo,5,5) as DocNo,dt,Description
from #tbl2) as RC
inner join
#tbl3
on
rc.docno=#tbl3.docno
结果:
DocNo dt Description DocNo dt Description
12345 15Jan19 QC-Passed 12345 15Jan19 QC-Passed
09876 16Jan19 QC-Passed 09876 16Jan19 QC-Passed