sql查询连接超过2个表

时间:2013-06-06 18:42:02

标签: sql-server inner-join

我有4张桌子:

 #table 1 columns - areacode,section_number,maint_charge
 #table 2 columns - section_number,discount,fee,total_Maint_charge
 #table 3 columns - areacode, statename
 #table 4 columns - section_number , customer

映射如下:

 #table1.areacode = #table3.areacode
 #table1.section_number = #table2.section_number

我想得到这样的东西:

 state,section_number,maint_charge,maint_charge/total_maint_charge,
 fee*(maint_charge/total_maint_charge) 

由于

2 个答案:

答案 0 :(得分:2)

SELECT state,
    table1.section_number,
    maint_charge,
    (maint_charge / total_maint_charge),
    (fee * (maint_charge / total_maint_charge))
FROM table1
    JOIN table3 ON table1.areacode=table3.areacode
    JOIN table2 on table1.section_number=table2.section_number;

答案 1 :(得分:0)

试试这个 -

SELECT 
      t3.statename
    , t1.section_number
    , t1.maint_charge
    , t1.maint_charge / t2.total_maint_charge
    , t2.fee * (t1.maint_charge / t2.total_maint_charge)
FROM #table1 t1
JOIN #table2 t2 ON t1.section_number = t2.section_number
JOIN #table3 t3 ON t1.areacode = t3.areacode