分少,好心的高手们帮忙~~
二张表,分别是Archives,Contract
表Archives
fID fNO
----------------------------
001 DA00001
002 DA00002
.........................
表Contract
fArchivesID fNO fDate
----------------------------
001 GC00001 1900-01-01
001 AJ00001 2005-05-03
001 FQ00001 2003-07-08
002 GC00002 1900-01-01
002 AJ00002 2007-03-03
002 FQ00002 1900-01-01
002 DK00002 2007-06-03
.......................
需要解释一下的是,表Contract的fNO字段的前缀字母部份只有以下几种可能:
GC
AJ
FQ
DK
不会再有其它形式
以上二张表进行关联,得出如下表样式(Archives left join Contract on Archives.fID = Contract.fArchivesiID )
a.fID a.fNO GC AJ FQ DK
------------------------------------------------------------------------
001 DA00001 GC00001||1900-01-01 AJ00001||2005-05-03 FQ00001||2003-07-08
002 DA00002 GC00002||1900-01-01 AJ00002||2007-03-03 FQ00002||1900-01-01 DK00002||2007-06-03
................
问题1:
Select a.fID,a.fNO,
(Case When left(b.fNO,2) = 'GC ' Then convert(varchar,b.fNO) + '|| ' + convert(varchar(10),b.fDate,21) Else ' ' end ) GC,
(Case When left(b.fNO,2) = 'AJ ' Then convert(varchar,b.fNO) + '|| ' + convert(varchar(10),b.fDate,21) Else &nb