Solved

need to update table @groups

Posted on 2015-02-10
4
59 Views
Last Modified: 2015-02-11
where i must put the update sequence
update @groups set KK= RESULT FROM THE CONSUMPTION

DECLARE @Groups TABLE ( GroupID varchar(12) ,KK varchar(100));

INSERT  INTO @Groups
VALUES  ( 2591,'' );

DECLARE @Products TABLE
    (
      ProductID NVARCHAR(255)
    );

INSERT  INTO @Products
VALUES  ( '2591-16' ),
        ( '2591-17' ),
        ( '2591-18' );

WITH    Normalized
          AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                        PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
               FROM     @Products P
               WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                          FROM      @Groups G )
             )
    SELECT  O.GroupID ,
            ( SELECT    '@-' + I.SizeID
              FROM      Normalized I
              WHERE     I.GroupID = O.GroupID
			  FOR XML PATH('')
            ) AS Sizes
    FROM    Normalized O
    GROUP BY O.GroupID;

Open in new window

DECLARE groupid CURSOR FOR select groupid from #Product_Pool_2 
OPEN groupid
FETCH NEXT FROM groupid into @groupid
WHILE @@FETCH_STATUS = 0
BEGIN
print N'Groupid'+ @groupid;

WITH    Normalized
          AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                        PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
               FROM     dbo.product_Prices P
               WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                          FROM      #Product_Pool_2 G )
             )
    SELECT  
            ( SELECT    '/' + I.SizeID
              FROM      Normalized I
              WHERE     I.GroupID = O.GroupID
                    FOR XML PATH('')
            ) AS Sizes
    FROM    Normalized O
    GROUP BY O.GroupID;


FETCH NEXT FROM groupid into @groupid
END

CLOSE groupid
DEALLOCATE groupid

Open in new window

DECLARE groupid CURSOR FOR select groupid from #Product_Pool_2 
OPEN groupid
FETCH NEXT FROM groupid into @groupid
WHILE @@FETCH_STATUS = 0
BEGIN
print N'Groupid'+ @groupid;

WITH    Normalized
          AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                        PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
               FROM     dbo.product_Prices P
               WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                          FROM      #Product_Pool_2 G )
             )
    SELECT  
            ( SELECT    '/' + I.SizeID
              FROM      Normalized I
              WHERE     I.GroupID = O.GroupID
                    FOR XML PATH('')
            ) AS Sizes
    FROM    Normalized O
    GROUP BY O.GroupID;


FETCH NEXT FROM groupid into @groupid
END

CLOSE groupid
DEALLOCATE groupid

Open in new window

DECLARE groupid CURSOR FOR select groupid from #Product_Pool_2 
OPEN groupid
FETCH NEXT FROM groupid into @groupid
WHILE @@FETCH_STATUS = 0
BEGIN
print N'Groupid'+ @groupid;

WITH    Normalized
          AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                        PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
               FROM     dbo.product_Prices P
               WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                          FROM      #Product_Pool_2 G )
             )
    SELECT  
            ( SELECT    '/' + I.SizeID
              FROM      Normalized I
              WHERE     I.GroupID = O.GroupID
                    FOR XML PATH('')
            ) AS Sizes
    FROM    Normalized O
    GROUP BY O.GroupID;


FETCH NEXT FROM groupid into @groupid
END

CLOSE groupid
DEALLOCATE groupid

Open in new window

0
Comment
Question by:ebertsys
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 40600527
Couldn't find any reference to update @groups in your code.
0
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40600532
Please complete your example:

-- Table DDL.
DECLARE @Product_Pool_2 TABLE ( GroupID INT );
DECLARE @product_Prices TABLE ( ProductID INT );

-- Insert your test date here.
---

-- Your Logic.
DECLARE groupid CURSOR
FOR
    SELECT  GroupID
    FROM    @Product_Pool_2;
DECLARE @groupid INT;

OPEN groupid;
FETCH NEXT FROM groupid INTO @groupid;
WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT N'Groupid' + @groupid;

        WITH    Normalized
                  AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                                PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
                       FROM     @product_Prices P
                       WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                                  FROM      @Product_Pool_2 G )
                     )
            SELECT  ( SELECT    '/' + I.SizeID
                      FROM      Normalized I
                      WHERE     I.GroupID = O.GroupID
                    FOR
                      XML PATH('')
                    ) AS Sizes
            FROM    Normalized O
            GROUP BY O.GroupID;


        FETCH NEXT FROM groupid INTO @groupid;
    END;

CLOSE groupid;
DEALLOCATE groupid;

-- Result: Please post the desired output of those two statements for the above sample data.
SELECT  *
FROM    @Product_Pool_2 PP;

SELECT  *
FROM    @product_Prices PP;

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40600566
You'll need to do a much better job defining what you're asking here.
Specifically, define ' RESULT FROM THE CONSUMPTION', and in the above four code blocks the bottom three appear to be identical.
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40600697
A CTE can be also used to update a column:

WITH    Normalized
                  AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                                PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
                       FROM     @product_Prices P
                       WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                                  FROM      @Product_Pool_2 G )
                     ),
SizeInfo AS (
            SELECT  ( SELECT    '/' + I.SizeID
                      FROM      Normalized I
                      WHERE     I.GroupID = O.GroupID
                    FOR
                      XML PATH('')
                    ) AS Sizes,
O.GroupID
            FROM    Normalized O
            GROUP BY O.GroupID)
UPDATE G
SET KK = SI.Size 
FROM @groups G 
INNER JOIN SizeInfo SI ON G.GroupID = SI.GroupID;

Open in new window

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question