如何组合临时表来获取正确的数据?

时间:2017-03-02 01:34:58

标签: mysql join

这是第一个临时表

    CREATE TABLE #tempvesvoy
(
    scn VARCHAR(10),
    vsl_name VARCHAR(150),
    act_arr_dt_tm DATETIME,
    act_dept_dt_tm DATETIME,
    del_remarks VARCHAR(150)
)

INSERT INTO #tempvesvoy


SELECT t1.scn, t1.vsl_name, t1.act_arr_dt_tm, t1.act_dept_dt_tm, t1.del_remarks
FROM vesvoy t1
WHERE ((@move_type= 'ATA' AND MONTH(t1.act_arr_dt_tm) = @month AND YEAR(t1.act_arr_dt_tm)= @year) OR
(@move_type= 'ATD' AND MONTH(t1.act_dept_dt_tm) = @month AND YEAR(t1.act_dept_dt_tm)= @year))

第二个临时表

    CREATE TABLE #TempVesselInvoice
(

    inv_num_vsl VARCHAR(10),
    sum_vsl_inv DECIMAL(12,2)
)

INSERT INTO #TempVesselInvoice

SELECT SUM(CAST(t1.inv_amt as decimal(12,2)))as sum_vsl_inv, t1.inv_num as inv_num_vsl
FROM pbosinvoice t1
LEFT JOIN pbosinvoiceitem t2 ON t1.id = t2.master_id
WHERE t1.inv_type = 'I01'
GROUP BY t1.inv_num, t2.scn

我得到的结果就是这个

SCN         vsl_name    act_arr_dt_tm  act_dept_dt_tm   del_remarks   sum_vsl_inv   inv_num_vsl
16120043    Asd         2016-12-21     00:00:00.000     NULL          NULL            NULL  
16120040    UNI AMPLE   2016-12-17     00:06:00.000     NULL          NULL            NULL  

问题是一些数据,有1个scn并且可以从temp表2中有很多inv_num_vsl如何根据scn显示所有inv_num_vsl。我使用左连接,但结果显示为null。

0 个答案:

没有答案