简体   繁体   中英

Pivot Table Created From CSV File

I'm dealing with an issue where the formatting on a CSV file that we're importing needs to be"pivoted" to match the formatting required for the program we are using to process the import.

Currently we are importing the file which comes with the following format:

Account Department Jan2022 Feb2022 Mar2022
12345 Sales $456 $876 $345
98765 HR $765 $345 $344

We need the format to hold the time periods in one column which would make each account be repeated per time period. For example:

Account Department Period Amount
12345 Sales Jan2022 $456
12345 Sales Feb2022 $876
12345 Sales Mar2022 $345

We are importing this CSV using JavaScript however its basic JS as the program does not support JQuery or any other JS library. Once we import the table into our staging area using JS, we can use SQL to modify the data as well, so this could be solved with either JS or SQL.

We are using a CSV to Array function to read the CSV file for importing into staging:

function CSVToArray(strData, strDelimiter) {
  // Check to see if the delimiter is defined. If not, then default to comma.
  strDelimiter = strDelimiter || ",";

  // Create a regular expression to parse the CSV values.
  var objPattern = new RegExp(
    // Delimiters.
    "(\\" +
      strDelimiter +
      "|\\r?\\n|\\r|^)" +
      // Quoted fields.
      '(?:"([^"]*(?:""[^"]*)*)"|' +
      // Standard fields.
      '([^"\\' +
      strDelimiter +
      "\\r\\n]*))",
    "gi"
  );

  // Create an array to hold our data. Give the array a default empty first row.
  var arrData = [[]];

  // Create an array to hold our individual pattern matching groups.
  var arrMatches = null;

  // Keep looping over the regular expression matches until we can no longer find a match.
  while ((arrMatches = objPattern.exec(strData))) {
    // Get the delimiter that was found.
    var strMatchedDelimiter = arrMatches[1];

    // Check to see if the given delimiter has a length (is not the start of string) and if it matches
    // field delimiter. If id does not, then we know that this delimiter is a row delimiter.
    if (strMatchedDelimiter.length && strMatchedDelimiter !== strDelimiter) {
      // Since we have reached a new row of data, add an empty row to our data array.
      arrData.push([]);
    }

    //Now that we have our delimiter out of the way, let's check to see which kind of value we captured (quoted or unquoted).
    var strMatchedValue;

    if (arrMatches[2]) {
      // We found a quoted value. When we capture this value, unescape any double quotes.
      strMatchedValue = arrMatches[2]
        .replace(new RegExp('""', "g"), '"')
        .replace('"', "");
    } else {
      // We found a non-quoted value.
      strMatchedValue = arrMatches[3];
    }
    // Now that we have our value string, let's add it to the data array.
    arrData[arrData.length - 1].push(strMatchedValue);
  }
  // Return the parsed data.
  return arrData;
}

UNPIVOT should work for you:

/* sample data */
with t as
 (select '12345' account,
         'Sales' department,
         '$456' jan2022,
         '$876' feb2022,
         '$345' mar2022
    from dual
  union all
  select '98765' account,
         'HR' department,
         '$765' jan2022,
         '$345' feb2022,
         '$344' mar2022
    from dual)

select *
  from t
 unpivot include nulls(amount for period in(jan2022 as 'jan2022',
                                            feb2022 as 'feb2022',
                                            mar2022 as 'mar2022'));

If you have dynamic columns you gonna have bad time with this aproach - you have to generate "unpivot in clause" (that part with jan2022 as 'jan2022') on your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM