日期:2014-05-16  浏览次数:20758 次

关于SQL语句
表一(sql_b):
 



表二(sql_a):



想查询表二中saleno字段为DAVID LEE相对应的custno_wtdw字段中的公司对碰表一中的custno字段且表二中的creditdate字段时间在2013.02.01至2014.02.01之间且having sum(ARtotal)-sum(factemoney)>1的数据,用了以下语句,好像有些不适合条件的都出来了,

select custno_wtdw from sql_a T where exists (select custno from sql_b where custno=T.custno_wtdw and creditdate between '2013.02.01' and '2014.02.01' group by custno having sum(ARtotal)-sum(factemoney)>1) and  saleno='DAVID LEE' group by custno_wtdw order by custno_wtdw




------解决方案--------------------
--TRY


SELECT
    custno_wtdw
FROM
    sql_a AS a
INNER join ( SELECT
                custno
             FROM
                sql_b
             WHERE
                 creditdate BETWEEN '2013.02.01' AND '2014.02.01'
             GROUP BY
                custno
             HAVING
                SUM(ARtotal)-SUM(factemoney)>1 )  as b on a.custno_wtdw=custno AND a.saleno='DAVID LEE'
GROUP BY
    a.custno_wtdw
ORDER BY
    a.custno_wtdw