分别显示sql中不同列中一个表的数据

时间:2015-01-02 10:15:18

标签: sql-server-2008

我希望此表中的输出显示在下面

  Lslno                head                            Amount 
  -----                ----                            ------
  I0001             Sales(S001)                       20000.00
  I0002             Debters(D001)                     30000.00
  I0003             Loan(L001)                        5000.00
  I0004             Capital(c001)                     6000.00
  E0001             Creditors(CRD001)                 2000.00
  E0002             Expenses(EX001)                   3000.00
  E0003             PaymentForPurchase(PP001)         4000.00
  E0004             PurchasingFixedAsset(PFA001)      10000.00

输出 -

   Lslno     head            Amount   Lslno   head                            amount
   -----     ----           ------    -----   ------                         --------
   I0001    Sales(S001)     20000.00  E0001 Creditors(CRD001)               2000.00
   I0002    Debters(D001)   30000.00  E0002 Expenses(EX001)                 3000.00 
   I0003    Loan(L001)      5000.00   E0003 PaymentForPurchase(PP001)       4000.00 
   I0004    Capital(C001)   6000.00   E0004 PurchasingFixedAsset(PFA001)    10000.00

1 个答案:

答案 0 :(得分:0)

只需使用适当的过滤器将表格连接到自身。

SELECT 
  t1.*, t2.*
FROM tbl AS t1
  JOIN tbl AS t2 ON SUBSTRING(t1.LslNo, 2, 4) = SUBSTRING(t2.LslNo, 2, 4)
WHERE t1.Lslno LIKE 'I%'
  AND t2.Lslno LIKE 'E%'