2016-03-08 80 views
1

我有兩個表(基表和數據表),我正在使用OUTER APPLY合併以獲得結果。將列顯示爲複雜Select語句中的行sql

tblBase看起來是這樣的:

+------+------+ 
| IDnu | Name | 
+------+------+ 
| 1 | abc | 
| 2 | cde | 
| 3 | efg | 
| 4 | rfl | 
+------+------+ 

tblData是這樣的:

+------+--------+--------+--------+-------------+ 
| IDNu | Price1 | Price2 | Price3 | ProductType | 
+------+--------+--------+--------+-------------+ 
| 1 |  10 |  15 |  20 | Old   | 
| 2 |  10 |  20 |  30 | Refurbished | 
| 3 |  20 |  30 |  40 | New   | 
| 1 |  20 |  15 |  20 | New   | 
| 2 |  20 |  10 |  30 | Old   | 
+------+--------+--------+--------+-------------+ 

我基於幾個標準,其計算tblData當前查詢是如下:

Select IDNu, Name, t2.PNew, t2.POld FROM tblBase as t1 
OUTER APPLY 
(
SELECT 
    SUM (CASE WHEN ProductType = 'New' THEN Price1 + Price2 ELSE 0 END) AS PNew, 
    SUM (CASE WHEN ProductType = 'Old' THEN Price2 + Price3 ELSE 0 END) AS POld 
FROM tblData 
WHERE IDNu = t1.IDNu 
GROUP BY IDNu 
) t2 

的以上查詢結果爲:

+------+------------+------+------+ 
| IDNu | Name | PNew | POld | 
+------+------------+------+------+ 
| 1 | abc  | 35 | 35 | 
| 2 | cde  | 0 | 40 | 
| 3 | efg  | 50 | 0 | 
| 4 | rfl  | NULL | NULL | 
+------+------------+------+------+ 

現在的問題是,而不是顯示在兩列PNEW和POLD,顯示他們行?像這樣:

+------+------------+-------------+-------+ 
| IDNu | Name | ProductType | Price | 
+------+------------+-------------+-------+ 
| 1 | abc  | PNew  | 35 | 
| 2 | cde  | PNew  | 0  | 
| 3 | efg  | PNew  | 50 | 
| 4 | rfl  | PNew  | NULL | 
| 1 | abc  | POld  | 35 | 
| 2 | cde  | POld  | 40 | 
| 3 | efg  | POld  | 0  | 
| 4 | rfl  | POld  | NULL | 
+------+------------+-------------+-------+ 
+0

這不是MySQL的。這是TSQL .. Stackoverflow自動將它標記爲'mysql',不知道爲什麼。 – Shanka

+1

對不起,我知道爲什麼。我修好了它。謝謝。 – Shanka

+0

如果您要提出與SQL相關的問題,請參閱此文章:[*如何在公共論壇上發佈T-SQL問題*](http://spaghettidba.com/2015/04/24/how -to-post-at-sql-question-on-a-public-forum /) –

回答

1

你可以試試這個:

SELECT tblBase.*, 'PNew' as ProductType, tblData.Price1 + tblData.Price2 as Price 
FROM tblBase left join `tblData` 
On tblBase.IDnu = tblData.IDnu AND tblData.ProductType = 'New' 
UNION ALL 
SELECT tblBase.*, 'POld' as ProductType, tblData.Price3 + tblData.Price2 as Price 
FROM tblBase left join `tblData` 
On tblBase.IDnu = tblData.IDnu AND tblData.ProductType = 'Old' 
+0

謝謝!這有助於。 – Shanka

1
select b.IDNu, b.Name, pt.ProductType, d.Price 
from 
    tblBase as b cross join (select 'New' ProductType union all select 'Old') pt 
    left outer join 
    (
     select 
      ProductType, 
      sum(case ProductType 
        when 'New' then Price1 + Price2 
        when 'Old' then Price2 + Price3 end) as Price 
     from tblData 
     where ProductType in ('New', 'Old') /* not strictly necessary but maybe faster */ 
     group by IDNu, ProductType 
    ) d 
     on d.IDNu = p.IDNu and d.ProductType = pt.ProductType 
order by b.IDNu, pt.ProductType 

或...

select 
    b.IDNu, b.Name, pt.ProductType, 
    sum(case pt.ProductType 
      when 'New' then d.Price1 + d.Price2 
      when 'Old' then d.Price2 + d.Price3 end) as Price 
from 
    tblBase as b 
    cross join 
    (select 'New' ProductType union all select 'Old') pt 
    left outer join 
    tblData d on d.IDNu = p.IDNu and d.ProductType = pt.ProductType 
group by b.IDNu, pt.ProductType 
order by b.IDNu, pt.ProductType 

在你的輸出你混了一些零個空輸出,那裏沒有數據。當然,翻譯這些空值的正常方法是使用coalesce()。你也有「PNew」和「POld」作爲你的輸出值,但我不確定這是有意的。一個簡單的case將處理輸出select子句中的那些。

1

最簡單的方法是在你的應用外表UNPIVOT:

Select 
    IDNu,Name,t2.ProductType,t2.Price 
FROM 
    tblBase as t1 
    OUTER APPLY (
     SELECT ProductType='New',SUM(Price1+Price2) AS Price 
     FROM tblData 
     WHERE IDNu=t1.IDNu AND ProductType='New' 
     UNION ALL 
     SELECT ProductType='Old',SUM(Price2+Price3) AS Price 
     FROM tblData 
     WHERE IDNu=t1.IDNu AND ProductType='Old' 
    ) t2