1

我在下面有一个示例数据,我想在其中操作数据并生成新的数据库表。

[样本数据图像]

我想要如下图的输出:

[需要输出]

这是我用来获取数据的查询:

    CREATE TABLE #tmp_vendorauth_HK ( 
                AuthMaterialKey varchar(30) not null, 
                CustomerNumber varchar(20) null
    ) ON [PRIMARY]


INSERT INTO #tmp_vendorauth_HK  (AuthMaterialKey, CustomerNumber)
SELECT DISTINCT basic_view.SalesOrganization + '@@' + basic_view.DistributionChannel + '@@' + basic_View.Material as AuthkeyMaterial, 
                ISNULL(RTRIM(ACG.CustomerNumber), '000001') + '@@' as CustomerNumber 
FROM            V_BASIC_MTR_ATTR_HK as basic_view
LEFT OUTER JOIN V_AUTH_CUST_GROUP ACG ON basic_view.Material = ACG.Material  
--ORDER BY 1 DESC

**TRUNCATE TABLE VendorAuth_group_HK


INSERT INTO VendorAuth_group_HK (AuthMaterialKey,CustomerNumber)
SELECT      AuthMaterialKey, substring(customernumbers, 1, len(customernumbers)-1)  
FROM        #tmp_vendorauth_HK a WITH(NOLOCK)
CROSS APPLY 
(
    SELECT  LTRIM(RTRIM(CustomerNumber)) + ',' 
    FROM    #tmp_vendorauth_HK TblskuDuplicate 
    WHERE   TblskuDuplicate.AuthMaterialKey= a.AuthMaterialKey

     FOR XML PATH('')
) AS t (customernumbers)**

drop table #tmp_vendorauth_HK

注意:我使用的是 SQL Server 2000,因此我无法使用 CTE 或CROSS APPLYT-SQL 的功能

4

2 回答 2

1

在这里,我使用简单的解决方案来更新我的过程创建了一个函数来为我填充详细信息并在我的存储过程中使用。

功能详情如下

 CREATE FUNCTION dbo.fn_GET_CustomerNumbers
(
    @vcrMaterial VARCHAR(30)
)
RETURNS VARCHAR(8000)
AS 
BEGIN 

DECLARE             @vcrCustomerNumbers VARCHAR(8000)
SET         @vcrCustomerNumbers = ''

SELECT      @vcrCustomerNumbers = @vcrCustomerNumbers + ',' + CustomerNumber
FROM        tmp_vendorauth_HK 
WHERE       AuthMaterialkey = @vcrMaterial

SELECT      @vcrCustomerNumbers = STUFF(@vcrCustomerNumbers, 1,1, '')
RETURN      @vcrCustomerNumbers 

END
GO

上面的函数将返回可以在 select 语句中使用的连接数字,因为另一列可以是您的输入参数。

于 2014-01-08T10:47:36.077 回答
0

我之前处理这个问题的方法是计算出我将连续拥有的最大项目数,并在我的数据表上创建那么多自联接。以您的样本数据为例,假设您对任何 ProductID 最多有 6 个客户代码。

现在这个 sql 有点难看,但是为了模拟 CROSS APPLY,我们可以做一系列自连接和嵌套查询来创建一串客户代码。通过定位具有最大长度的字符串,您只保留您感兴趣的记录。

SELECT Nest4.ProductID,String
FROM (
    SELECT ProductID,MaxStringLength
    FROM (
        SELECT ProductID,max(stringlength) MaxStringLength
        FROM (
            SELECT ProductID,String,len(String) StringLength 
            FROM (
                SELECT 
                ProductID
                ,case when t0.CustomerCode is not null then t0.CustomerCode else '' end
                +','+case when t1.CustomerCode is not null then t1.CustomerCode else '' end
                +','+case when t2.CustomerCode is not null then t2.CustomerCode else '' end
                +','+case when t3.CustomerCode is not null then t3.CustomerCode else '' end
                +','+case when t4.CustomerCode is not null then t4.CustomerCode else '' end
                +','+case when t5.CustomerCode is not null then t5.CustomerCode else '' end
                "String"
                FROM MyTable t0 
                LEFT JOIN MyTable t1 on t0.ProductID=t1.ProductID and t0.CustomerCode<t1.CustomerCode 
                LEFT JOIN MyTable t2 on t0.ProductID=t2.ProductID and t1.CustomerCode<t2.CustomerCode 
                LEFT JOIN MyTable t3 on t0.ProductID=t3.ProductID and t2.CustomerCode<t3.CustomerCode 
                LEFT JOIN MyTable t4 on t0.ProductID=t4.ProductID and t3.CustomerCode<t4.CustomerCode 
                LEFT JOIN MyTable t5 on t0.ProductID=t5.ProductID and t4.CustomerCode<t5.CustomerCode 

            ) AS Nest1
        ) AS Nest2 GROUP BY ProductID
     )  Nest3
) Nest4

JOIN (
            SELECT ProductID,String,len(String) StringLength 
            FROM (
                SELECT 
                t0.ProductID ProductID
                ,case when t0.CustomerCode is not null then t0.CustomerCode else '' end
                +','+case when t1.CustomerCode is not null then t1.CustomerCode else '' end
                +','+case when t2.CustomerCode is not null then t2.CustomerCode else '' end
                +','+case when t3.CustomerCode is not null then t3.CustomerCode else '' end
                +','+case when t4.CustomerCode is not null then t4.CustomerCode else '' end
                +','+case when t5.CustomerCode is not null then t5.CustomerCode else '' end
                "String"
                FROM MyTable s 
                LEFT JOIN MyTable t1 on t0.ProductID=t1.ProductID and t0.CustomerCode<t1.CustomerCode 
                LEFT JOIN MyTable t2 on t0.ProductID=t2.ProductID and t1.CustomerCode<t2.CustomerCode 
                LEFT JOIN MyTable t3 on t0.ProductID=t3.ProductID and t2.CustomerCode<t3.CustomerCode 
                LEFT JOIN MyTable t4 on t0.ProductID=t4.ProductID and t3.CustomerCode<t4.CustomerCode 
                LEFT JOIN MyTable t5 on t0.ProductID=t5.ProductID and t4.CustomerCode<t5.CustomerCode 

            ) AS Nest1
        ) V2
        ON Nest4.ProductID=V2.ProductID and Nest4.MaxStringLength=V2.StringLength
于 2014-01-07T13:49:35.683 回答