2016-07-29 311 views
1

我試圖創建一個compare,現在我只能將它作爲多個結果(兩個不同的結果)。將兩個查詢的結果合併爲一個結果SQL

enter image description here

上述兩種結果的是從兩個查詢。

我的第一個結果查詢

SELECT 
    customercode, 
    CONVERT(DATE, TransDate) transdate, 
    SUM(TotalReceivable) AS total 
FROM 
    SalesOrderHeader 
WHERE 
    CustomerCode = 'K-MDMM4' 
    AND TransDate BETWEEN '2016-07-25' AND '2016-07-30' 
GROUP BY 
    CONVERT(DATE, TransDate), customercode 

和我的第二個查詢

SELECT 
    b.OutletCode AS outlet, 
    tanggal, 
    (cash + cc + dc + flash + piutang + reject + disc50 + 
    isnull(spesial_item,0)) total 
FROM 
    transaksi a 
LEFT JOIN 
    Outlet b ON a.Outlet = b.OutletCode 
LEFT JOIN 
    area c ON b.areacode = c.areacode 
WHERE 
    b.active = 1 
    AND b.OutletCode LIKE 'K-MDMM4' 
    AND flag = 1 
    AND tanggal BETWEEN '2016-07-25' AND '2016-07-30' 
GROUP BY 
    b.OutletCode, tanggal, cash, cc, dc, flash, 
    piutang, reject, disc50, spesial_item, ba, mpm, tf, 
    ul,remarks 
ORDER BY 
    tanggal DESC 

我想這個結果。

customercode | transdate | total_tbl1 | total_tbl2 
     K-MDMM4  2016-07-25 6004050  6004050 
     K-MDMM4  2016-07-26 6880340  6880340 
     K-MDMM4  2016-07-27 5745040  5745040 
     K-MDMM4  2016-07-28 7424820  7424820 

我不能使用的jsfiddle :(。我不知道爲什麼,我無法通過查詢創建表。

從現在開始,我有這個疑問

SELECT 
    b.OutletCode AS outlet, 
    tanggal, 
    (cash + cc + dc + flash + piutang + reject + disc50 + 
    isnull(spesial_item, 0)) total, 
    SUM(d.TotalReceivable) AS total 
FROM 
    transaksi a 
LEFT JOIN 
    Outlet b ON a.Outlet = b.OutletCode 
LEFT JOIN 
    area c ON b.areacode = c.areacode 
LEFT JOIN 
    salesorderheader d ON CONVERT(DATE, a.tanggal) = CONVERT(DATE, d.transdate) 
WHERE 
    b.active = 1 
    AND b.BrandCode LIKE '%%' 
    AND b.OutletCode LIKE '%%' 
    AND flag = 1 
    AND YEAR(tanggal) = '2016' 
    AND MONTH(tanggal) = '7' 
    AND outlet = 'K-MDMM4' 
GROUP BY 
    OutletCode, tanggal, cash, cc, dc, flash, 
    piutang, reject, disc50, spesial_item, transdate, totalreceivable 
ORDER BY 
    tanggal DESC 

和結果到目前爲止我的預期結果...

回答

3

合併這兩個查詢到單個連接,並選擇

SELECT tbl1.customercode, 
     CAST(tbl1.transdate AS DATE) AS transdate, 
     tbl1.total AS total_tbl1, 
     tbl2.total AS total_tbl2 
FROM 
(
-- Query 1 
SELECT customercode,convert(date,TransDate) transdate,SUM(TotalReceivable) as total 
       FROM SalesOrderHeader 
       where CustomerCode = 'K-MDMM4' 
       and TransDate between '2016-07-25' and '2016-07-30' 
       group by convert(date,TransDate),customercode 
) AS tbl1 
INNER JOIN (
-- Query 2 
select b.OutletCode as outlet,tanggal, (cash + cc + dc + flash + piutang + reject + disc50 + 
    isnull(spesial_item,0)) total From transaksi a 
     left join Outlet b on a.Outlet = b.OutletCode 
     left join area c on b.areacode = c.areacode 
     where b.active = 1 and b.OutletCode like 'K-MDMM4' and flag = 1 and tanggal 
     between '2016-07-25' and '2016-07-30' 
     group by b.OutletCode,tanggal,cash,cc,dc,flash,piutang,reject,disc50,spesial_item,ba,mpm,tf,ul,remarks 
) AS tbl2 ON tbl2.outlet = tbl1.customercode AND CAST(tbl2.trnggal AS DATE) = CAST(tbl1.transdate AS DATE) 
order by CAST(tbl1.transdate AS DATE) DESC; 
+0

感謝男人,這是工作。 'by by'部分給我一個錯誤。我把它放在查詢的最後,錯誤消失了 – YVS1102

+0

好的地方,我更新了答案。 –

-1

我沒有在這臺電腦上安裝的數據庫,但你要找的是:

SELECT val1, val2 FROM 
(SELECT1_of_your_code AS table1) INNER JOIN 
(SELECT2_of_your_code AS table2) ON 
table1.x == table2.y 
+0

請發表評論,downvotes-這是第一次發佈的解決方案,甚至它是不是在我看來,一個複製/粘貼的解決方案,它包括所需的全部信息要解決問題描述 – Wolfgang

相關問題