TSQL:将旧的where子句转换为Join语法

时间:2012-10-02 23:52:12

标签: tsql join

我有这个问题,我想把它放到'现代连接语法'

SELECT  
    t.acct_order, 
    c1.acct_desc, 
    c1.position, 
    c1.end_pos,
    Budget = sum(((g.data_set-1)/2)*(g.debits - g.credits)),
    Actual = sum(((g.data_set-3)/-2)*(g.debits - g.credits)) 

FROM    
    glm_chart c1, 
    glm_chart c2, 
    glh_deptsum g, 
    #TEMPTABLE t

WHERE   
        g.acct_uno=c2.acct_uno  
    and c1.acct_code = t.acct_code  
    and c2.position between c1.position and c1.end_pos  
    and g.debits-g.credits<>0 
    and c1.book=1  
    and g.data_set in (1, 3)  
    and (g.period between 201201 and 201212) 

GROUP   by  t.acct_order, 
            c1.acct_desc, 
            c1.position,
            c1.end_pos 

ORDER   by  t.acct_order 

这就是我所要做的,但正如你所看到的,我似乎无法识别表格glh_deptsum(g)到C1或T

的连接
SELECT  
    t.acct_order, 
    c1.acct_desc, 
    c1.position, 
    c1.end_pos,
    sum(((g.data_set-1)/2)*(g.debits - g.credits))  AS Budget,
    sum(((g.data_set-3)/-2)*(g.debits - g.credits)) AS Actual, 


FROM         #TEMPTABLE T
INNER JOIN  glm_chart c1 ON c1.acct_code = t.acct_code
INNER JOIN  glh_deptsum g <---- HELP WHAT GOES HERE ---------
INNER JOIN  glm_chart c2 ON c2.position between c1.position and c1.end_pos AND g.acct_uno=c2.acct_uno


WHERE   
    g.debits - g.credits <> 0 
    AND c1.book=1  
    AND g.data_set in (1, 3) 
    AND (g.period between 201201 and 201212) 

GROUP BY t.acct_order, 
            c1.acct_desc, c1.position,c1.end_pos 

ORDER BY t.acct_order 

有人能让我知道我做错了吗?

1 个答案:

答案 0 :(得分:2)

看起来应该是这样的:

SELECT t.acct_order, 
    c1.acct_desc, 
    c1.position, 
    c1.end_pos,
    Budget = sum(((g.data_set-1)/2)*(g.debits - g.credits)),
    Actual = sum(((g.data_set-3)/-2)*(g.debits - g.credits)) 
FROM #TEMPTABLE t
INNER JOIN glm_chart c1
  ON t.acct_code = c1.acct_code
INNER JOIN glm_chart c2
  ON c2.position between c1.position and c1.end_pos  
INNER JOIN glh_deptsum g
  ON c2.acct_uno = g.acct_uno
WHERE g.debits-g.credits<>0 
    and c1.book=1  
    and g.data_set in (1, 3)  
    and (g.period between 201201 and 201212) 
GROUP by  t.acct_order, 
            c1.acct_desc, 
            c1.position,
            c1.end_pos 
ORDER   by  t.acct_order 
相关问题