1

我有一个INNER JOIN运行良好的查询,我想要LEFT JOIN另一个SELECT包含 WHERE 子句的查询/语句。我无法加入这两个查询。它应该链接wt.tkinit = t.tkinit 你能建议我错过什么吗?

SELECT c.clnum, m.mmatter, ot.tkinit AS 'otkinit', wt.tkinit AS 'wtikint', t.tkrt01,
SUM(mt.mthrwkdb) AS 'whrs2010',
FROM client c, matter m, timekeep ot, timekeep wt, mattimhs mt, periodt p, timerate t
WHERE c.clnum = m.mclient
AND m.mmatter = mt.mtmatter
GROUP BY c.clnum, m.mmatter, ot.tkinit, wt.tkinit

SELECT t.tkinit, t.tkrt01
FROM timerate t
INNER JOIN (
SELECT tkinit, max(tkeffdate) as max_effdate 
FROM timerate WHERE DATEPART(year, tkeffdate) = '2012'
GROUP BY tkinit) mt ON mt.tkinit = t.tkinit AND mt.max_effdate = t.tkeffdate
4

2 回答 2

1

您是否尝试过以下操作:

SELECT fields from (SELECT1) s1 
  LEFT JOIN 
  (SELECT2) s2 
  on s1.wt.tkinit = s2.t.tkinit.

不要忘记在 wt_tkinit 和 t_tkinit 之类的选择中为 wt.tkinit 和 t.tkinit 取正常名称,因为您不能使用双重别名。

于 2013-07-22T18:45:52.877 回答
0
SELECT temp2.tkinit, temp2.tkrt01, temp.clnum, temp.mmatter, temp.otkinit, temp.tkrt01, temp.whrs2010
FROM
    ( SELECT c.clnum, m.mmatter, ot.tkinit AS 'otkinit', wt.tkinit AS 'wtikint', t.tkrt01,
    SUM(mt.mthrwkdb) AS 'whrs2010',
    FROM client c, matter m, timekeep ot, timekeep wt, mattimhs mt, periodt p, timerate t
    WHERE c.clnum = m.mclient
    AND m.mmatter = mt.mtmatter
    GROUP BY c.clnum, m.mmatter, ot.tkinit, wt.tkinit ) temp
LEFT JOIN 
    SELECT t.tkinit, t.tkrt01
    FROM timerate t
    INNER JOIN (
    SELECT tkinit, max(tkeffdate) as max_effdate 
    FROM timerate WHERE DATEPART(year, tkeffdate) = '2012'
    GROUP BY tkinit) mt ON mt.tkinit = t.tkinit AND mt.max_effdate = t.tkeffdate ) temp2
ON temp.wtikint = temp2.tkinit

Be careful of your field names, but this may be a start

于 2013-07-22T18:55:43.087 回答