Question

I try to generate a new column by grouping other columns and the code below works.

 CASE 
     WHEN familyGroupPOSRef = 955 
       THEN MAX(RVCPOSId) OVER (PARTITION BY organizationID, Location_Code, transactionID) 
       ELSE -1 
 END AS toEntity

Is there a way to run this code by using a string column? Using LocationName instead of RVCPOSId before OVER statement.

CASE 
    WHEN familyGroupPOSRef = 955 
      THEN LocationName OVER (PARTITION BY organizationID, Location_Code, transactionID) 
      ELSE 'nothing' 
END AS toEntity

I could not find anything on the net so far. It does not have to be this method. I need to group organizationID, Location_Code, transactionID and display locationName for 955 in a separate column.

How it is now:

 organizationID   Code   transactionID   FGName         itemName
    10000         955    123456          Location       Liv Retail
    10000         103    123456          Internal Item  Chocolate Milkshake
    10000         103    123456          Internal Item  Strawberry Milk
    10000         103    123456          Internal Item  Milk

How I want it to be

organizationID   Code   transactionID   FGName         toEntity    itemName
10000            955    123456          Location       Liv Retail  Liv Retail
10000            103    123456          Internal Item  Liv Retail  Chocolate Milkshake
10000            103    123456          Internal Item  Liv Retail  Strawberry Milk
10000            103    123456          Internal Item  Liv Retail  Milk

A transaction can have multiple menuItems and also it has always 1 record for location which is kept like a menuItem. This is a trick. I like to create a new column to apply location name which is Liv Retail under toEntity in the sample data provided.

Please let me know if I am after something impossible. Then I will break down the stmt and connect tables with join.

Thanks in advance.

Was it helpful?

Solution

You can do this using a simple sub-query

CREATE TABLE Table1
([organizationID] int, 
 [Code] int, 
 [transactionID] int, 
 [FGName] varchar(13), 
 [itemName] varchar(19));

 INSERT INTO Table1
([organizationID],
 [Code], 
 [transactionID], 
 [FGName], 
 [itemName])
 VALUES
(10000, 955, 123456, 'Location', 'Liv Retail'),
(10000, 103, 123456, 'Internal Item', 'Chocolate Milkshake'),
(10000, 103, 123456, 'Internal Item', 'Strawberry Milk'),
(10000, 103, 123456, 'Internal Item', 'Milk');

select *,toEntity=(SELECT ITEMNAME FRom TAble1 Ti 
            where ti.TRANSACTIONID=T.TRANSACTIONID and Ti.FGNAME='Location' )
     from Table1 T  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top