T-SQL: 查询带回太多列
T-SQL: Query brings back too many columns
我已经 运行 处理了一个似乎带回太多列(是的,是列,不是行)的查询。声明如下
Select TOP 10 PERCENT
TempLegs.*,
TMWStateMilesByLeg.*
From
(
Select TOP 10
TempOrigin.stp_city as 'Origin Location'
from stops TempOrigin (NOLOCK)
Union
Select TOP 10
TempOrigin2.stp_city as 'Origin Location'
from stops TempOrigin2 (NOLOCK)
) As TempLegs,TMWStateMilesByLeg
当我运行这个语句时,我的结果集不是'Origin Location'的单个列,而是列出了从停靠点table开始的所有列。这是怎么回事?
我唯一能想到的就是
As TempLegs,TMWStateMilesByLeg
语句为连接的两个部分设置了别名,但我不确定,因为我从未在 AS 语句中看到两个术语。
非常感谢任何帮助。
这是旧式连接。
Select TOP 10 PERCENT /* <-- top without order by */
TempLegs.*
--,TMWStateMilesByLeg.*
From (
Select TOP 10 /* <-- top without order by */
TempOrigin.stp_city as 'Origin Location'
from stops TempOrigin --(NOLOCK)
Union /* <-- union instead of union all will return distinct results */
Select TOP 10 /* <-- top without order by */
TempOrigin2.stp_city as 'Origin Location'
from stops TempOrigin2 --(NOLOCK)
) As TempLegs --,TMWStateMilesByLeg
参考:
我已经 运行 处理了一个似乎带回太多列(是的,是列,不是行)的查询。声明如下
Select TOP 10 PERCENT
TempLegs.*,
TMWStateMilesByLeg.*
From
(
Select TOP 10
TempOrigin.stp_city as 'Origin Location'
from stops TempOrigin (NOLOCK)
Union
Select TOP 10
TempOrigin2.stp_city as 'Origin Location'
from stops TempOrigin2 (NOLOCK)
) As TempLegs,TMWStateMilesByLeg
当我运行这个语句时,我的结果集不是'Origin Location'的单个列,而是列出了从停靠点table开始的所有列。这是怎么回事?
我唯一能想到的就是
As TempLegs,TMWStateMilesByLeg
语句为连接的两个部分设置了别名,但我不确定,因为我从未在 AS 语句中看到两个术语。
非常感谢任何帮助。
这是旧式连接。
Select TOP 10 PERCENT /* <-- top without order by */
TempLegs.*
--,TMWStateMilesByLeg.*
From (
Select TOP 10 /* <-- top without order by */
TempOrigin.stp_city as 'Origin Location'
from stops TempOrigin --(NOLOCK)
Union /* <-- union instead of union all will return distinct results */
Select TOP 10 /* <-- top without order by */
TempOrigin2.stp_city as 'Origin Location'
from stops TempOrigin2 --(NOLOCK)
) As TempLegs --,TMWStateMilesByLeg
参考: