select语句限制临时表

时间:2018-01-15 23:00:37

标签: sql sql-server join left-join

我在这里是一个完全的菜鸟,我确信答案是"好吧",但我还是难以接受。

我从一个表中选择并使用临时表中两个字段的组合来限制我通过将我的表与上述临时表连接而带回的记录。

以下是我的询问:

<b>select distinct company3, dept</b>
into #compdept
from fss..budget
where fyr4 = @fyrin and fmo = @fprdin
--select * from #compdept

select
          b.t_year      as fyr4,
          b.t_leac      as acct,
          b.t_cono      as company3,
          b.t_dim1      as dept,
          b.t_fdam - t_fcam       as curactamt,
          b.t_fdam * 0            as curcmpamt,
          b.t_fdam * 0            as ytdactamt,
          b.t_fdam * 0            as ytdcmpamt
    from baan5cdb..ttfgld205110 b
<b>    left join #compdept a on a.company3 = b.t_cono 
    and a.dept = b.t_dim1</b> COLLATE SQL_Latin1_General_CP1_CI_AS
    where b.t_year  = @fyrin 
      and b.t_prno  = @fprdin 

上面以粗体显示的部分我认为会将结果集限制为仅存在于临时表中的公司和部门组合。

但是,我在查询结果中得到公司110,部门029,并且临时表#compdept中不存在company3和dept的组合。

我做错了什么?

感谢。

3 个答案:

答案 0 :(得分:0)

根据您的查询,baan5cdb..ttfgld205110会返回#compdept中的所有记录和匹配的记录。

为了仅过滤来自#compdept的匹配记录,一个选项是Exists Operator。示例

    SELECT  b.t_year AS fyr4 ,
            b.t_leac AS acct ,
            b.t_cono AS company3 ,
            b.t_dim1 AS dept ,
            b.t_fdam - t_fcam AS curactamt ,
            b.t_fdam * 0 AS curcmpamt ,
            b.t_fdam * 0 AS ytdactamt ,
            b.t_fdam * 0 AS ytdcmpamt
    FROM    baan5cdb..ttfgld205110 b
    WHERE   b.t_year = @fyrin
            AND b.t_prno = @fprdin
            AND EXISTS ( SELECT 1
                             FROM   #compdept a
                             WHERE  a.company3 = b.t_cono
                                    AND a.dept = b.t_dim COLLATE SQL_Latin1_General_CP1_CI_AS )

答案 1 :(得分:0)

在第二个查询中使用内部联接到临时表

SELECT DISTINCT
      company3
    , dept INTO #compdept
FROM fss..budget
WHERE fyr4 = @fyrin
AND fmo = @fprdin
--select * from #compdept

SELECT
      b.t_year          AS fyr4
    , b.t_leac          AS acct
    , b.t_cono          AS company3
    , b.t_dim1          AS dept
    , b.t_fdam - t_fcam AS curactamt
    , b.t_fdam * 0      AS curcmpamt
    , b.t_fdam * 0      AS ytdactamt
    , b.t_fdam * 0      AS ytdcmpamt
FROM baan5cdb..ttfgld205110 b
INNER JOIN #compdept a ON a.company3 = b.t_cono
      AND a.dept = b.t_dim1 COLLATE SQL_Latin1_General_CP1_CI_AS
WHERE b.t_year = @fyrin
AND b.t_prno = @fprdin

外部联接将允许表baan5cdb..ttfgld205110中的所有行,但内部联接仅允许与#compdept匹配的行。

OR 如果打算列出临时表的每一行,即使没有匹配的行,也要反转查询中的表位:

SELECT
      b.t_year          AS fyr4
    , b.t_leac          AS acct
    , b.t_cono          AS company3
    , b.t_dim1          AS dept
    , b.t_fdam - t_fcam AS curactamt
    , b.t_fdam * 0      AS curcmpamt
    , b.t_fdam * 0      AS ytdactamt
    , b.t_fdam * 0      AS ytdcmpamt
FROM #compdept a
LEFT JOIN baan5cdb..ttfgld205110 b ON a.company3 = b.t_cono
      AND a.dept = b.t_dim1
      AND b.t_year = @fyrin
      AND b.t_prno = @fprdin COLLATE SQL_Latin1_General_CP1_CI_AS

答案 2 :(得分:0)

试试这个:

select  distinct 
        company3
        , dept
into    #compdept
from    fss..budget
where   fyr4 = @fyrin 
and     fmo = @fprdin

select  b.t_year                as fyr4,
        b.t_leac                as acct,
        b.t_cono                as company3,
        b.t_dim1                as dept,
        (b.t_fdam - t_fcam)     as curactamt,
        b.t_fdam * 0            as curcmpamt,
        b.t_fdam * 0            as ytdactamt,
        b.t_fdam * 0            as ytdcmpamt
from    #compdept a
left 
join    baan5cdb..ttfgld205110 b
        on a.company3 = b.t_cono 
        and a.dept = b.t_dim1  COLLATE SQL_Latin1_General_CP1_CI_AS
where   b.t_year  = @fyrin 
        and b.t_prno  = @fprdin