SQL将SELECT语句组合成两列

时间:2011-07-23 17:03:22

标签: .net sql sql-server-2005

两个表SQL 2005表

   Trend Table
   Enter  Exit  Number 
   .01    .01     0
   .02    .02     1
   .03    .03     2
   .04    .04     3

  Orders Table
  Ticker    Enter   Exit
   EUR        0        1
   GBP        2        3

要求:从订单表中仅提供自动收报机符号的单个查询,以从趋势表生成输入和退出值。

Ticker Value将在Orders表中提供Enter和Exit Integers,它们对应于趋势表中的Number Column,它指向查询应输出的实际Decimal Enter和Exit值。

输入和退出值需要位于不同的列中。以下查询仅生成值为1列的输出(请参阅输出)

 SELECT  T.Enter
 FROM Trend AS T INNER JOIN Orders AS O ON O.Enter = T.Number
 WHERE (O.Ticker = 'EUR')
 UNION
 SELECT D.Exit
 FROM Trend AS D INNER JOIN Orders AS F ON F.Exit= D.Number
 WHERE (F.Ticker = 'EUR')

 OUTPUT:
 Exit
 0.01
 0.02

1 个答案:

答案 0 :(得分:2)

这就是你想要的吗?

SELECT Orders.Ticker, T1.Enter, T2.Exit
FROM Orders
INNER JOIN Trend T1 ON Orders.Enter = T1.Number
INNER JOIN Trend T2 ON Orders.Exit = T2.Number