东西功能以及如何订购东西串顺序

时间:2018-03-23 18:34:08

标签: sql sql-server-2008 sqltransaction

我正在尝试根据对特定列的分组将Distinct列值填充到表中,我能够将列的distinct值填充到单个String但是顺序当我比较连续三个结果的结果时,字符串不正确。

我使用SQL Query如下:

CREATE TABLE #TableB (
              AcquireNbr int
            , Working_Day Date
            , Working_Cd varchar(15)
            , Agreement_Cds varChar(60) NULL
            , Agreement_id_Qtys varChar(80) NULL
            , Agreement_Receiver_Cds varchar(80) NULL
            , Agreement_Managers varChar(80) NULL
        )
        INSERT INTO #TableB
        SELECT 
              AcquireNbr
            , Working_Day
            , Working_Cd
            , Agreement_Cds             =   STUFF((SELECT DISTINCT ', ' + LTRIM(RTRIM(Agreement_Cd))
                                            FROM #TableA b 
                                            WHERE b.Working_Cd = a.Working_Cd AND b.AcquireNbr = a.AcquireNbr AND b.Working_Day = a.Working_Day 
                                            FOR XML PATH('')), 1, 2, '')
            , Agreement_id_Qtys         =   STUFF((SELECT DISTINCT ', ' + CONVERT(varchar(10), Agreement_ID) + '_' + CONVERT(varchar(15), Agreement_Qty)
                                            FROM #TableA b 
                                            WHERE b.Working_Cd = a.Working_Cd AND b.AcquireNbr = a.AcquireNbr AND b.Working_Day = a.Working_Day
                                            FOR XML PATH('')), 1, 2, '')
            , Agreement_Receiver_Cds    =   STUFF((SELECT DISTINCT ', ' + LTRIM(RTRIM(Agreement_Receiver_Cd))
                                            FROM #TableA b 
                                            WHERE b.Working_Cd = a.Working_Cd AND b.AcquireNbr = a.AcquireNbr AND b.Working_Day = a.Working_Day
                                            FOR XML PATH('')), 1, 2, '')

        FROM 
            #TableA a
        GROUP BY 
              AcquireNbr
            , Working_Day
            , Working_Cd

数据表 TableA包含我的数据,如下所示

AcquireNbr  Working_Day Working_Cd  Agreement_ID    Agreement_Qty   Agreement_Cd    Agreement_Receiver_Cd
5           2018-03-09  ABC         1               246000          FT              E
5           2018-03-09  DEF         2               3300            FT              E1
5           2018-03-09  DEF         3               900             FT              E1
5           2018-03-09  DEF         2               3300            FT              E1
5           2018-03-09  ABC         4               0               NT              80
5           2018-03-09  GHI         5               25000           NT              80
5           2018-03-09  DEF         3               900             FT              E1

查询结果如下所示

AcquireNbr  Working_Day Working_Cd  Agreement_Cds   Agreement_id_Qtys   Agreement_Receiver_Cds
5           2018-03-09  GHI         NT              5_25000             80
5           2018-03-09  DEF         FT              3_900, 2_3300       E1
5           2018-03-09  ABC         FT, NT          1_246000, 4_0       80, E

在结果的最后一行中,AgreementId和Agreement_Receiver_Cd订单不匹配,即

  

对于协议id = 1,接收器cd是E但是在结果集中它显示   为80

并且

  

对于协议id = 4,接收器cd为80但是在结果集中显示   如E

我猜选择陈述的东西 - >获取Agreement_Receiver_Cds的子查询返回的结果与获得Agreement_id_Qtys的其他子查询的顺序不同。

必填结果

AcquireNbr  Working_Day Working_Cd  Agreement_Cds   Agreement_id_Qtys   Agreement_Receiver_Cds
5           2018-03-09  GHI         NT              5/25000             80
5           2018-03-09  DEF         FT              3/900, 2/3300       E1
5           2018-03-09  ABC         FT, NT          1/246000, 4/0       E, 80

0 个答案:

没有答案