table_a
id       qty
001 95
002 50
004 1
005 173
006 464
007 14
018 17
027 2
038 3
039 2
042 4
046 12
050 69
057 17
062 4
083 4
116 1
119 14
127 1
900 1
table_b
001 200
002 187
004 2
005 433
006 749
007 77
010 1
012 4
018 223
027 5
038 8
039 2
042 4
044 1
046 13
050 135
057 27
062 4
083 5
093 12
099 2
110 1
116 1
119 43
120 1
126 1
127 1
900 17
我现在得到的结果是:
id       a
001 47.50%
002 26.74%
004 50.00%
005 39.95%
006 61.95%
007 18.18%
018 7.62%
027 40.00%
038 37.50%
039 100.00%
042 100.00%
046 92.31%
050 51.11%
057 62.96%
062 100.00%
083 80.00%
116 100.00%
119 32.56%
127 100.00%
900 5.88%
可是比如010,...就没有了,我想让010也显示出来 0%

解决方案 »

  1.   

    select table_b.id id,case when table_a.qty id null then 0 else table_a.qty/table_b.qty end a
    from table_b left join table_a on table_a.id=table_b.id
      

  2.   

    create table table_a(id int ,qty int)
    go
    insert into table_a select 001,95
    union select 002,50
    union select 004,1
    create table table_b(id int ,qty int)
    go
    insert into table_b select 001,100
    union select 002,200
    union select 004,200
    union select 005,200go
    select table_b.id id,isNull(table_a.qty,0)/Convert(float,table_b.qty) a
    from table_b left join table_a on table_a.id=table_b.iddrop table table_a
    drop table table_b
      

  3.   

    -----------------
    id       a
    ----------------
    1 0.95
    2 0.25
    4 0.005
    5 0
    ---------------
    (4 行受影响)
      

  4.   


    create table table_a(id int ,qty int)
    go
    insert into table_a select 001,95
    union select 002,50
    union select 004,1
    create table table_b(id int ,qty int)
    go
    insert into table_b select 001,100
    union select 002,200
    union select 004,200
    union select 005,200select a.id,cast(cast(round(isnull((b.qty+0.0)/a.qty,0)*100,2) as float) as varchar(10))+'%' as qty_p   from table_b a left join table_a b
    on a.id=b.idid          qty_p
    ----------- -----------
    1           95%
    2           25%
    4           0.5%
    5           0%(4 行受影响)
      

  5.   

    left join 或着 right join