2011-12-15 42 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) 
线做一些事情

这只是我的头顶,所以可能有更好的方法来做到这一点。