SQL连接视图

时间:2011-12-15 14:54:20

标签: sql sql-server-2005

我需要创建一个视图,将多行连接成一行。

有问题的表是QotMaster,我需要加入来自NComment字段的LineType ='6'的所有行,并且我猜测要将每个引用下的它们分组到Quote字段。连接的每一行都有字段Line,从2开始,并根据引号注释的行数增加。

最终我想读一行:

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

任何帮助都会受到很大的影响。香港专业教育学院试图转储我需要连接下面的部分。

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                                 

1 个答案:

答案 0 :(得分: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,'')+@delimiter
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)

这只是我的头脑,所以可能有更好的方法。