0

我对 SQL Server 很陌生,我正在尝试这样做......不知道我做错了什么

这是我的架构:

CREATE TABLE [Product_AGGREGATION](
[Product_ID] [int] NULL,
    [ITERATION] [int] NULL,
    [EQUIPMENT_USED] [varchar](8) NULL,
    [MODEL_USED] [varchar](25) NULL,
    [SERIAL_NUMBER] [varchar](14) NULL
) 

CREATE TABLE [Product_MERGE](
    [Product_ID] [int] NULL,    
    [EQUIPMENT_USED] [varchar](8) NULL,
    [MODEL_USED] [varchar](25) NULL,
    [SERIAL_NUMBER] [varchar](14) NULL
) 

INSERT INTO [dbo].[PRODUCT_AGGREGATION]                         
     VALUES
           (6,1,'X10', '302000','005'),
           (6,7,'X10' ,'310355','007'),
           (3,1,'A12' ,'NULL','NULL'),
           (3,4,'A10' ,'35205','002'),
         (7,2,'A10' ,'37545','002'),
         (7,4,'A10' ,'NULL','005')                      

这是我正在使用的存储过程

ALTER PROCEDURE [dbo].[SP_ProductMerge]
BEGIN
    DECLARE @v_product_id          INT
    DECLARE @v_iteration           INT
    DECLARE @v_equip_used          VARCHAR(8)
    DECLARE @v_model_used          VARCHAR(25)
    DECLARE @v_serial_num          VARCHAR(14)
    DECLARE @c_Pid             INT
    DECLARE @c_iteration           INT
    DECLARE @c_equip_used          VARCHAR(8)
    DECLARE @c_model_used          VARCHAR(25)
    DECLARE @c_serial_num          VARCHAR(14)

    DECLARE  c_PID CURSOR FOR
    SELECT DISTINCT Product_id
        FROM   [Product_AGGREGATION]
        ORDER BY Product_id
    OPEN c_PID

    FETCH NEXT FROM c_PID INTO @c_Pid
    WHILE(@@FETCH_STATUS = 0)
    BEGIN
        SET @v_product_id         = @c_Pid
        SET @v_equip_used           = NULL
        SET @v_model_used           = NULL
        SET @v_serial_num          = NULL


        DECLARE c_ProductMerge CURSOR FOR
        SELECT iteration, EQUIPMENT_USED, MODEL_USED, SERIAL_NUMBER
            FROM   [Product_AGGREGATION]
            WHERE  Product_id = @v_product_id
            ORDER BY iteration
        OPEN c_ProductMerge
        FETCH NEXT FROM c_ProductMerge INTO @c_iteration, @c_equip_used, @c_model_used, @c_serial_num
        WHILE(@@FETCH_STATUS = 0)
        BEGIN
            IF RTRIM(LTRIM(@c_equip_used)) IS NOT NULL AND @v_equip_used IS NULL
            BEGIN
                SET @v_equip_used = @c_equip_used
            END
            IF RTRIM(LTRIM(@c_model_used)) IS NOT NULL AND @v_model_used IS NULL
            BEGIN


                 SET @v_model_used = @c_model_used
            END
            IF RTRIM(LTRIM(@c_serial_num)) IS NOT NULL AND @v_serial_num IS NULL
            BEGIN
                SET @v_serial_num = @c_serial_num
            END


             FETCH NEXT FROM c_ProductMerge INTO @c_iteration, @c_equip_used, @c_model_used, @c_serial_num
        END
        CLOSE c_ProductMerge
        DEALLOCATE c_ProductMerge
        INSERT INTO [dbo].Product_merge
            VALUES (
                @v_product_id, @v_equip_used, @v_model_used, @v_serial_num
               )
     FETCH NEXT FROM c_PID INTO @c_Pid
    END
    CLOSE c_PID
    DEALLOCATE c_PID

 END

GO

产品合并的预期结果

 6 , 'X10', '302000' , '005'
 3, 'A12', '35205', '002'
 7,'A10'.'37545' , '002'

我没有得到产品 3 的预期结果。此外,在没有光标或至少使用一个光标而不是嵌套光标的情况下,我需要帮助。

上面的 SQLFiddle

4

1 回答 1

0

第一,'NULL'不等于NULL。根据您的数据,您需要将代码替换为IS NOT NULLWITH <> 'NULL'

我认为你需要的可以用这个查询代替。

SELECT
  a.Product_ID
  ,EQUIPMENT_USE =(SElECT TOP 1 p.EQUIPMENT_USED FROM Product_AGGREGATION p WHERE p.Product_ID = a.Product_ID AND p.EQUIPMENT_USED <> 'NULL' ORDER BY ITERATION)   
  ,MODEL_USED =(SElECT TOP 1 p.MODEL_USED FROM Product_AGGREGATION p WHERE p.Product_ID = a.Product_ID AND p.MODEL_USED <> 'NULL' ORDER BY ITERATION)   
  ,SERIAL_NUMBER =(SElECT TOP 1 p.SERIAL_NUMBER FROM Product_AGGREGATION p WHERE p.Product_ID = a.Product_ID AND p.SERIAL_NUMBER <> 'NULL' ORDER BY ITERATION)   
FROM Product_AGGREGATION a
GROUP BY a.Product_ID

sql 小提琴演示

于 2013-11-04T20:40:23.143 回答