2011-12-15 43 views
0

我需要創建一個視圖來將多個行連接成一行。SQL連接視圖

有問題的表是QotMaster,我需要連接NComment字段中LineType ='6'的所有行,並且即時猜測將它們分組在引用字段組中。每個要連接的行都有字段Line,並從2開始並根據引號註釋的行數增加。

最後,我想單行改爲:

Quote   NComment 
00202300  Lines for quotes all listed one after the other starting at 2 and so on 

任何幫助將大大apreciated。我試圖轉儲一個部分,我需要連接在下面。

Quote Line LineType DefaultEntry ProductClass TaxCode FedSalesTax MStockCode MDescription MWarehouse MUom MDecimals MLineShipDate MCusStockCode MCusRetailPrice MMass MVolume MUserField1 NComment NCommentFromLin NCommentType NCommentTextTyp NSrvIncTotal NSrvSummary NSrvChargeType NSrvParentLine NSrvQtyFactor NSrvApplyFactor NSrvDecimalRnd NSrvDecRndFlag NSrvMinValue NSrvMaxValue NSrvMulDiv NPrtOnInv NPrtOnDel NPrtOnAck NPrtOnQuote NCommentFlag1 NCommentFlag2 NCommentFlag3 NCommentFlag4 NCommentFlag5 NCommentFlag6 NCommentPoJob TimeStamp Version Release SalesOrderLine MBomFlag MParentKitType MQtyPer MScrapPercentage MPrintComponent MComponentSeq MOptionalFlag Estimate ConfirmedFlag 
00202300 1 7 0 M20    002023000000001     Periodic inspection & test  **  0 2012-01-13 00:00:00.000         0.00000 0.000000 0.000000               0      0 0.000000  0  0.00 0.00      0 0 0 0 0 0  0x000000002414E6EE   0   0.000000 0.00 N        
00202300 2 6 0                       0 NULL         0.00000 0.000000 0.000000   Pre-Formed Windings, Sheffield     0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C0   0   0.000000 0.00         
00202300 3 6 0                       0 NULL         0.00000 0.000000 0.000000   To carry out a periodic inspection and test  0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C1   0   0.000000 0.00         
00202300 4 6 0                       0 NULL         0.00000 0.000000 0.000000   of the fixed wiring installation. This will 0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C2   0   0.000000 0.00         
00202300 5 6 0                       0 NULL         0.00000 0.000000 0.000000   consist of 100% inspection and 10% testing,  0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C3   0   0.000000 0.00         
00202300 6 6 0                       0 NULL         0.00000 0.000000 0.000000   based on previous records being available.  0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C4   0   0.000000 0.00         
00202300 7 6 0                       0 NULL         0.00000 0.000000 0.000000   The price is based on no serious defects  0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C5   0   0.000000 0.00         
00202300 8 6 0                       0 NULL         0.00000 0.000000 0.000000   being found, which may result in the testing 0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C6   0   0.000000 0.00         
00202300 9 6 0                       0 NULL         0.00000 0.000000 0.000000   being expanded to cover 25%, which would  0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C7   0   0.000000 0.00         
00202300 10 6 0                       0 NULL         0.00000 0.000000 0.000000   incur additional costs. A full report will  0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C8   0   0.000000 0.00         
00202300 11 6 0                       0 NULL         0.00000 0.000000 0.000000   be issued upon completion.      0 N     0 0.000000  0  0.00 0.00     Y 1 0 0 0 0 0  0x000000002414E6C9   0   0.000000 0.00         
+2

那麼,你有什麼嘗試,不起作用?或者你希望有人在這裏爲你做? – 2011-12-15 14:55:51

回答

0

在SQL Server中,你只需要創建一個函數來做到這些方針的東西:

CREATE FUNCTION [dbo].[mergeComments](@quote NVARCHAR(8)) 
RETURNS nvarchar(max) 
AS 
BEGIN 

DECLARE @comment nvarchar(max) 
DECLARE @delimiter nvarchar(2) 

SET @delimiter = ', ' 
SET @comment = '' 

SELECT @comment = @comment + ISNULL(NComment,'')[email protected] 
FROM QotMaster 
WHERE Quote = @quote AND Line > 1 

SET @comment = substring(@comment,1,len(@comment)-LEN(@delimiter)) 

RETURN @comment 

END 

那麼你的觀點將只是沿着

SELECT Quote, dbo.mergeComments(Quote) NQuote 
FROM (SELECT DISTINCT Quote FROM QotMaster) 
線做一些事情

這只是我的頭頂,所以可能有更好的方法來做到這一點。