使用视图时从SQL Server获取无效列错误

时间:2018-10-12 21:01:36

标签: sql-server select left-join

我遇到以下错误:

  

第207级状态1的3行
  无效的列名“ naicscode”

当前的任务是查询名为Xyzfirms201701的表,并返回以下结果。

 area    sizeclassep    Number of worksites    employment in size class 
 -----------------------------------------------------------------------
 000000      01               46673                       48975
 000000      02               32105                       54111

我正在使用以下代码来尝试生成此代码:

;WITH sizeclasseptable AS 
(
    SELECT 
        area, naicscode, ownership, sizeclassep 
    FROM 
        (SELECT '01' AS sizeclassep, '50' AS ownership
         UNION SELECT '02' AS sizeclassep, '50' AS ownership
         UNION SELECT '03' AS sizeclassep, '50' AS ownership
         UNION SELECT '04' AS sizeclassep, '50' AS ownership
         UNION SELECT '05' AS sizeclassep, '50' AS ownership
         UNION SELECT '06' AS sizeclassep, '50' AS ownership
         UNION SELECT '07' AS sizeclassep, '50' AS ownership
         UNION SELECT '08' AS sizeclassep, '50' AS ownership
         UNION SELECT '09' AS sizeclassep, '50' AS ownership) t0
    CROSS JOIN 
        (SELECT DISTINCT area FROM xyzfirms201701) t1
)
SELECT
    '000000' AS area,
    t2.[SizeClassep],
    COUNT(*) AS [Number of Worksites],
    SUM(t2.Employment) AS [Employment In Size Class]
FROM
    sizeclasseptable
LEFT JOIN 
    xyzfirms201701 t2 ON t2.area = sizeclasseptable.area 
                      AND t2.naicscode = sizeclasseptable.naicscode
                      AND t2.ownership = sizeclasseptable.ownership
WHERE 
    t2.naicscode LIKE '11%' OR t2.naicscode LIKE '21%'
GROUP BY
    [t2.SizeClassep]
ORDER BY
    [t2.SizeClassep];

第一部分的目的是让SQL产生输出,即使数据不存在。例如,大小classep = 09有时将不存在。这表示在给定行业(naicscode)中拥有1,000名或更多雇员的公司。关于为什么我收到无效列错误的任何想法?

3 个答案:

答案 0 :(得分:2)

您只需要在交叉联接子查询中添加naicscode,因为CTE中缺少它,这就是您收到Invalid column name错误的原因。我认为naicscodexyzfirms201701表的一部分,因此只需更新以下行。

cross join ( select distinct area, naicscode from xyzfirms201701 ) t1

更新 使用别名为CTE表进行最终查询。

;with sizeclasseptable as 
(
select t1.area,
       t1.naicscode,
       t0.ownership,
       t0.sizeclassep 
from (
select '01' as sizeclassep, '50' as ownership
union select '02' as sizeclassep, '50' as ownership
union select '03' as sizeclassep, '50' as ownership
union select '04' as sizeclassep, '50' as ownership
union select '05' as sizeclassep, '50' as ownership
union select '06' as sizeclassep, '50' as ownership
union select '07' as sizeclassep, '50' as ownership
union select '08' as sizeclassep, '50' as ownership
union select '09' as sizeclassep, '50' as ownership) t0
cross join ( select distinct area, naicscode from xyzfirms201701 ) t1
)
SELECT
  '000000' AS area,
  sc.[SizeClassep],
  COUNT(*) AS [Number of Worksites],
  SUM(t2.Employment) AS [Employment In Size Class]
from sizeclasseptable sc
left join xyzfirms201701 t2 
  on t2.area=sc.area 
    and t2.naicscode=sc.naicscode
    and t2.ownership=sc.ownership
WHERE t2.naicscode like '11%' or t2.naicscode like '21%'
GROUP BY [sc.SizeClassep]
ORDER BY [sc.SizeClassep];

答案 1 :(得分:1)

您从哪里获得naiscode字段?

 ;with sizeclasseptable as (
    select area,ownership,sizeclassep 
    from (
        select '01' as sizeclassep, '50' as ownership
        union select '02' as sizeclassep, '50' as ownership
        union select '03' as sizeclassep, '50' as ownership
        union select '04' as sizeclassep, '50' as ownership
        union select '05' as sizeclassep, '50' as ownership
        union select '06' as sizeclassep, '50' as ownership
        union select '07' as sizeclassep, '50' as ownership
        union select '08' as sizeclassep, '50' as ownership
        union select '09' as sizeclassep, '50' as ownership
    ) t0
    cross join ( 
        select distinct area from xyzfirms201701 
    ) t1
)
SELECT
    '000000' AS area,
    t2.[SizeClassep],
    COUNT(*) AS [Number of Worksites],
    SUM(t2.Employment) AS [Employment In Size Class]
FROM sizeclasseptable
LEFT JOIN xyzfirms201701 t2 ON t2.area=sizeclasseptable.area 
    --AND t2.naicscode=sizeclasseptable.naicscode
    AND t2.ownership=sizeclasseptable.ownership
WHERE t2.naicscode LIKE '11%' OR t2.naicscode LIKE '21%'
GROUP BY [t2.SizeClassep]
ORDER BY [t2.SizeClassep];

如何从CTE中删除naicscode,因为它肯定在表xyzfirms201701中。

答案 2 :(得分:0)

下面的代码产生正确的答案,没有错误。主要问题是naicscode在CTE中未定义,因此需要省略。由于它不在CTE中,因此需要将其从联接中删除。如果我停在那里,我不会收到任何错误,但是给出的值不正确。他们是应有的9倍。 305个工作地点是正确的数目,但给了2745个,依此类推。从那里开始,在连接语句中添加“和t2.sizeclassep = sizeclasseptable.sizeclassep”,以防止每个sizeclassep给出305个结果。

;with sizeclasseptable as 
(
select area,ownership,sizeclassep from (
select '01' as sizeclassep, '50' as ownership
union select '02' as sizeclassep, '50' as ownership
union select '03' as sizeclassep, '50' as ownership
union select '04' as sizeclassep, '50' as ownership
union select '05' as sizeclassep, '50' as ownership
union select '06' as sizeclassep, '50' as ownership
union select '07' as sizeclassep, '50' as ownership
union select '08' as sizeclassep, '50' as ownership
union select '09' as sizeclassep, '50' as ownership) t0
cross join ( select distinct area from xyzfirms201701 ) t1
)

 SELECT
'000000' AS area,
t2.[SizeClassep],
COUNT(*) AS [Number of Worksites],
SUM(t2.Employment) AS [Employment In Size Class]
from sizeclasseptable
left join xyzfirms201701 t2 
on t2.area=sizeclasseptable.area 
and t2.ownership=sizeclasseptable.ownership
and t2.sizeclassep = sizeclasseptable.sizeclassep
WHERE t2.naicscode like '11%' or t2.naicscode like '21%'
GROUP BY
t2.SizeClassep
ORDER BY
t2.SizeClassep