SELECT *
FROM (SELECT c.*, isnull(d .wcsl, 0) AS wcsl
FROM (SELECT a.*, isnull(a.sl * b.zgs, 0) AS zgs
FROM (SELECT a.*, isnull(b.xzlx, ' ') AS xzlx_sm
FROM (SELECT c.*, isnull(d .qym, ' ') AS qym
FROM (SELECT a.*, isnull(b.xmm, ' ') AS xmm
FROM ww_wwjl a LEFT OUTER JOIN
xmk b ON a.xm_dir = b.dir) c LEFT OUTER JOIN
ww_khdak d ON c.khdm = d .khdm) a LEFT OUTER JOIN
ww_xzlx b ON a.xzlx = b.xzlx_path) a LEFT OUTER JOIN
(SELECT wwjl_idn, SUM(gs) AS zgs
FROM ww_wwjl_gs
WHERE (sbname <> '@#$%')
GROUP BY wwjl_idn) b ON a.idn = b.wwjl_idn) c LEFT OUTER JOIN
(SELECT wwjl_idn, SUM(dhsl) AS wcsl
FROM ww_wwdh
WHERE dhrq >= '2006-01-01' AND dhrq <= '2006-12-31'
GROUP BY wwjl_idn) d ON c.idn = d .wwjl_idn) DERIVEDTBL
WHERE (cj = '机械四课') AND (wwrq >= '2006-01-01') AND (wwrq <= '2006-12-31')
ORDER BY xm_dir, th
------------------
一共六个表:
ww_wwjl是主表,与以下五个表左连接
|---->xmk (ww_wwjl.xm_dir=xmk.dir)
|---->ww_khdak (ww_wwjl.khdm=xmk.khdm)
|---->ww_xzlx (ww_wwjl.xzlx=xmk.xzlx_path)
|---->ww_wwjl_gs (SUM(gs) group by wwjl_idn | ww_wwjl.idn=ww_wwjl_gs.wwjl_idn )
|---->ww_wwdh (SUM(dhsl) group by wwjl_idn | ww_wwjl.idn=ww_wwjl_gs.wwjl_idn )
不知道有没有讲清楚~~~~