简体   繁体   English

从 CSV 文件创建的数据透视表

[英]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.我正在处理一个问题,即我们正在导入的 CSV 文件的格式需要“透视”以匹配我们用于处理导入的程序所需的格式。

Currently we are importing the file which comes with the following format:目前我们正在导入以下格式的文件:

Account帐户 Department部门 Jan2022 2022 年 1 月 Feb2022 2022年2月 Mar2022 2022 年 3 月
12345 12345 Sales销售量 $456 456 美元 $876 876 美元 $345 345 美元
98765 98765 HR人力资源 $765 765 美元 $345 345 美元 $344 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 12345 Sales销售量 Jan2022 2022 年 1 月 $456 456 美元
12345 12345 Sales销售量 Feb2022 2022年2月 $876 876 美元
12345 12345 Sales销售量 Mar2022 2022 年 3 月 $345 345 美元

We are importing this CSV using JavaScript however its basic JS as the program does not support JQuery or any other JS library.我们正在使用 JavaScript 导入这个 CSV,但是它的基本 JS 因为该程序不支持 JQuery 或任何其他 JS 库。 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.一旦我们使用 JS 将表导入到我们的暂存区,我们也可以使用 SQL 来修改数据,所以这可以通过 JS 或 SQL 来解决。

We are using a CSV to Array function to read the CSV file for importing into staging:我们正在使用 CSV to Array 函数来读取 CSV 文件以导入到暂存中:

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: UNPIVOT应该为您工作:

/* 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.如果您有动态列,那么您将很难使用这种方法 - 您必须自己生成“unpivot in 子句”(jan2022 为 'jan2022' 的部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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