簡體   English   中英

從 CSV 文件創建的數據透視表

[英]Pivot Table Created From CSV File

我正在處理一個問題,即我們正在導入的 CSV 文件的格式需要“透視”以匹配我們用於處理導入的程序所需的格式。

目前我們正在導入以下格式的文件:

帳戶 部門 2022 年 1 月 2022年2月 2022 年 3 月
12345 銷售量 456 美元 876 美元 345 美元
98765 人力資源 765 美元 345 美元 344 美元

我們需要將時間段保存在一列中的格式,這將使每個帳戶在每個時間段內重復。 例如:

帳戶 部門 時期 數量
12345 銷售量 2022 年 1 月 456 美元
12345 銷售量 2022年2月 876 美元
12345 銷售量 2022 年 3 月 345 美元

我們正在使用 JavaScript 導入這個 CSV,但是它的基本 JS 因為該程序不支持 JQuery 或任何其他 JS 庫。 一旦我們使用 JS 將表導入到我們的暫存區,我們也可以使用 SQL 來修改數據,所以這可以通過 JS 或 SQL 來解決。

我們正在使用 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應該為您工作:

/* 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'));

如果您有動態列,那么您將很難使用這種方法 - 您必須自己生成“unpivot in 子句”(jan2022 為 'jan2022' 的部分)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM