Solved

need to update table @groups

Posted on 2015-02-10
4
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 50

Expert Comment

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

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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

759 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