1

我在 SQL Server 表中有以下数据:

id  Sal
1   100
2   200

id  Wages
1   600
2   800

我希望输出如下:

id   Sal/Wages
1    100
1    600
2    200
2    800

如何在 SQL Server 中使用 SELECT 语句来做到这一点?

4

2 回答 2

2

利用UNION ALL

Select Id, sal as [sal/wages]
from table1
UNION ALL
Select Id, wages as [sal/wages]
from table2
Order by id,[sal/wages]

如果您不需要重复记录,则只需使用UNION

于 2013-09-14T09:59:31.917 回答
1

使用联合所有

select id, sal as [sal/Wages] from table1
union all
select id, wages as [sal/Wages] from table2
order by 1

请注意,我使用了union alland not union,因为 union 从结果集中删除了重复项。有时它可能有用,但我认为不适用于您的情况。

于 2013-09-14T09:58:31.800 回答