2014-12-23 100 views
0

我一直在尋找答案但沒有找到。我認爲這可能是由於我不知道要搜索什麼條款。另外,這是我的第一篇文章,所以我想道歉,如果我使用不正確的格式。SQL - 將多行合併爲一個空行值,包括空值

我需要以下輸出:

Invoice | Inv Date | Created by | Destination | Goods $ | Freight | Insurance $ | 
33813..| 12 Dec ..| Jack ........| Cape Town | 250.00 ..| 50.00 ..| 10.00 ...| 
33814..| 12 Dec ..| Jenny .........| Durban ......| 5,000.00| 20.00 ..| ....| 


前5列是從使用發票列作爲指標各列建立。

然後我想添加運費和保險。這是在不同的表託管與以下佈局:

InvCostID | Invoice | Cost Code | Cost Description | Value |<br/> 
556 ..........| 33813 .| 1 ...............| Freight ...............| 50.00 | 
559 ..........| 33813 .| 2 ...............| Insurance ...........| 10.00 | 
570 ..........| 33814 .| 1 ...............| Freight ...............| 20.00 | 


的問題是,我不能只選擇列,包括了在運費和保險費是在不同的行。爲了解決這個問題,我創建了兩個「子表」。一個是成本代碼是1(因此是貨運表),另一個是成本代碼2(保險表)。

然後我只是從正確的表中選擇值。

問題:如果其中一個成本要素不存在(例如發票33814的保險),我當前的查詢將完全從結果中排除該發票。 (與上面的表,我下面的代碼只會顯示發票33813.

select 
IT.Invoice as 'Invoice', 
IT.InvDate as 'Inv Date', 
UT.UserFullName as 'Created by', 
IT.DestinationDescription as 'Destination', 
IT.USDVal as 'Goods $', 
FREIGHTLIST.Value as 'FREIGHT $', 
INSURANCELIST.Value as 'INSURANCE $' 

from InvoiceTable IT, 
UserTable UT, 
(select * from SundryCostTable where CostCode = 1) as FREIGHTLIST, 
(select * from SundryCostTable where CostCode = 2) as INSURANCELIST 

where IT.InvDate > '2014-12-01' 
and IT.UserId = UT.UserId 
and IT.Invoice = FREIGHTLIST.Invoice 
and IT.Invoice = INSURANCELIST.Invoice 

請幫助。

謝謝 尼科

(我使用SQL Server Management Studio中運行該查詢)

回答

0
select 
IT.Invoice as 'Invoice', 
IT.InvDate as 'Inv Date', 
UT.UserFullName as 'Created by', 
IT.DestinationDescription as 'Destination', 
IT.USDVal as 'Goods $', 
FREIGHTLIST.Value as 'FREIGHT $', 
INSURANCELIST.Value as 'INSURANCE $' 

from InvoiceTable IT 
join UserTable UT on IT.UserId = UT.UserId 
left join SundryCostTable as FREIGHTLIST on CostCode = 1 and IT.Invoice = FREIGHTLIST.Invoice 
left join SundryCostTable as INSURANCELIST on CostCode = 2 and IT.Invoice = INSURANCELIST.Invoice 

where IT.InvDate > '2014-12-01' 

注意的情況下,才能使用LEFT JOIN不排除IT和UT行沒有匹配FREIGHTLIST或INS URANCELIST。

看看this的帖子瞭解更多關於JOIN(s)的使用。

+0

謝謝。我一定會研究加入。 – Nico

0

您可以使用子查詢來代替Aquillo所描述的LEFT JOIN。以下查詢將處理FREIGHT和INSURANCE的NULL值。

SELECT 
IT.Invoice as 'Invoice', 
IT.InvDate as 'Inv Date', 
UT.UserFullName as 'Created by', 
IT.DestinationDescription as 'Destination', 
IT.USDVal as 'Goods $', 
ISNULL((select Top 1 Value from SundryCostTable where CostCode = 1 And SundryCostTable.Invoice=IT.Invoice),0) as 'FREIGHT $', 
ISNULL((select Top 1 Value from SundryCostTable where CostCode = 2 And SundryCostTable.Invoice=IT.Invoice),0) as 'INSURANCE $' 

from InvoiceTable IT, 
UserTable UT 

where IT.InvDate > '2014-12-01' 
and IT.UserId = UT.UserId 
+0

謝謝su8898。哪個解決方案(Join vs subquery)將成爲服務器上最重要的問題? Join顯然會佔用很多空間,但速度應該會更快。子查詢可能會慢一點,因爲它必須每行運行一個附加查詢。這個假設是否正確? – Nico

+0

如果從子查詢中刪除「ISNULL」部分,則連接和子查詢將由服務器編譯爲相同的查詢。子查詢大多被視爲連接,特別是當涉及唯一鍵時。因此,如果InvoiceTable中的INVOICE字段是主鍵,則不會預期性能/服務器負載會有任何差異。只是我注意到了一種替代方法。 – su8898