类型前的进货,退货是根据单号 'JH' 还是  'Th'来 确定。 
同时要求按 jdate时间范围查找, 可以按gysb.gysbh查找。

解决方案 »

  1.   

    你想用那个sql server, 你想用sql来直接出报表吗?
    那个不是标准的sql, 需要按的sql来做. 如PL/sql 或 ms sql会有分别.
      

  2.   

    sql server:select type as 类型,jhdh as 单号,dj as 单价,sl as 数量,ze as 总额
    from
    (
    select j.gysbh,'进货' as type,jhdh,dj,sl,dj*sl as ze,OrderType as 2
    from jhmxb m,jhdb j
    where m.jhdh=j.jhdh
    union all
    select t.gysbh,'退货' as type,thdh,dj,sl,dj*sl as ze,OrderType as 3
    from thmxb m,thdb t
    where m.thdh=t.thdh
    union all
    select a.gysbh,'小计' as type,null as thdh,null as dj,null as sl,sum(ze) as ze,OrderType as 4
    from (
    select j.gysbh,sum(dj*sl) as ze
    from jhmxb m,jhdb j
    where m.jhdh=j.jhdh
    group by j.gysbh
    union all
    select t.gysbh,sum(dj*sl) as ze
    from thmxb m,thdb t
    where m.thdh=t.thdh
    group by t.gysbh
    ) as a
    union all
    select gysbh,gysname as type,null as thdh,null as dj,sl,null as ze,OrderType as 1
    from gysb
    union all
    select gysbh,null as type,null as thdh,null as dj,sl,null as ze,OrderType as 5
    from gysb
    ) as t
    orser by gysbh,OrderType
    注:最好能用存储过程,要不以句sql不好理解。
      

  3.   

    各位,首先我想声明一下,我的确对sql不太熟悉,这个问题对我来说是很难,在csdn会做这题的人大有人在, 问问题嘛, 我想你也想找一个标题来吸引人家的注意,对吧! 大家都自己有事, 况且抽出时间来帮人解决问题, 
      

  4.   

    经过测试,更正好多错误:
    select type as 类型,jhdh as 单号,dj as 单价,sl as 数量,ze as 总额
    from
    (
    select j.gysbh,'进货' as type,j.jhdh,dj,sl,dj*sl as ze, 2 as OrderType
    from jhmxb m,jhdb j
    where m.jhdh=j.jhdh
    union all
    select t.gysbh,'退货' as type,t.thdh,dj,sl,dj*sl as ze, 3 as OrderType
    from thmxb m,thdb t
    where m.thdh=t.thdh
    union all
    select gysbh,'小计' as type,null as thdh,null as dj,null as sl,sum(ze) as ze, 4 as OrderType
    from (
    select j.gysbh,sum(dj*sl) as ze
    from jhmxb m,jhdb j
    where m.jhdh=j.jhdh
    group by j.gysbh
    union all
    select t.gysbh,sum(dj*sl) as ze
    from thmxb m,thdb t
    where m.thdh=t.thdh
    group by t.gysbh
    ) as a
    group by gysbh
    union all
    select gysbh,gysname as type,null as thdh,null as dj,null as sl,null as ze,1 as OrderType
    from gysb
    union all
    select gysbh,null as type,null as thdh,null as dj,null as sl,null as ze, 5 as OrderType
    from gysb
    ) as t
    order by gysbh,OrderType