Hitachi Vantara Pentaho Community Forums
Results 1 to 2 of 2

Thread: Group By - What is best way to Concatenate strings separated by , - but UNIQUE vals?

  1. #1

    Question Group By - What is best way to Concatenate strings separated by , - but UNIQUE vals?

    All -

    I use Group By and have many aggregate fields that contain "duplicate" values. Since I have multiple columns that will aggregrate arrays, I can not simply sort and do a unique steps.

    Example:

    hostname,column1, column2,column3
    host123,"a,b,b,c,d,d,","e,e,f,f,g","x,y,y,z"
    ...

    I would like to have the following:

    hostname,column1, column2,column3
    host123,"a,b,c,d,","e,f,g","x,y,z"

    Thanks in advance for suggestions!

    KP

  2. #2
    Join Date
    Aug 2016
    Posts
    290

    Default

    You can solve it easy using java:

    String[] row = {"host123","a,b,b,c,d,d,", "e,e,f,f,g", "x,y,y,z"};

    for(int i = 0; i < row.length; i++)
    {
    ..... String cell = row[i];
    ..... String[] allValuesInCell = cell.split(",");
    ..... Set<String> uniqueValues = new HashSet<String>();
    ..... for(int j = 0; j < allValuesInCell.length; j++)
    ..... {
    .......... String singleValueInCell = allValuesInCell[j];
    .......... uniqueValues.add(singleValueInCell);
    ..... }

    ..... String result = "";
    ..... Object[] uniqueValuesArray = uniqueValues.toArray();
    ..... for(int j = 0; j < uniqueValuesArray.length; j++)
    ..... {
    .......... if(j > 0)
    .......... {
    ............... result += ",";
    .......... }
    .......... result += (String) uniqueValuesArray[j];
    ..... }
    ..... System.out.println(result);
    }
    Last edited by Sparkles; 06-04-2018 at 02:46 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.