将字符串解析为多个列

huangapple go评论89阅读模式
英文:

Parse String To Multiple Columns

问题

以下是翻译好的部分:

  1. 我有一个格式为"32.49|33|30|100|20.5|21|18|100|33.39|33|30|100|1.4"的字段。我尝试将其拆分为13个不同的列。这个查询在某种程度上可以工作,但当我尝试在Power BI中运行它时,“Cross Apply”连接失败,以及我尝试将数据旋转到列的任何尝试都失败。
  2. 我希望找到一个可以在Power BI中很好运行并将分隔字段拆分为列的解决方案。非常感谢任何帮助!

希望这对您有所帮助。如果您有任何其他问题,请随时提出。

英文:

I have a field formatted as "32.49|33|30|100|20.5|21|18|100|33.39|33|30|100|1.4". I am trying to split it out as 13 different columns. This query works somewhat, but when I try to run it in Power BI, the "Cross Apply" join fails, as well as any attempt I have made to pivot the data to columns.

  1. SELECT
  2. DataMessage.MessageDate
  3. , DataMessage.Data
  4. , Sensor.SensorID
  5. , CSNet.CSNetID
  6. , Sensor.SensorName
  7. , Application.ApplicationName
  8. , value
  9. FROM
  10. [corp--monnit1\monnitsql].[Enterprise1].[dbo].[DataMessage] LEFT OUTER JOIN
  11. [corp--monnit1\monnitsql].[Enterprise1].[dbo].[Sensor] ON DataMessage.SensorID = Sensor.SensorID RIGHT OUTER JOIN
  12. [corp--monnit1\monnitsql].[Enterprise1].[dbo].[CSNet] ON Sensor.CSNetID = CSNet.CSNetID LEFT OUTER JOIN
  13. [corp--monnit1\monnitsql].[Enterprise1].[dbo].[Application] ON Sensor.ApplicationID = Application.ApplicationID
  14. CROSS APPLY
  15. STRING_SPLIT(DataMessage.Data,'|')
  16. WHERE
  17. CHARINDEX('|',DataMessage.Data)>0
  18. AND Application.ApplicationName ='ThreePhaseCurrentMeter500'
  19. AND Csnet.CSNetID = 10
  20. AND DataMessage.MessageDate > '03/29/2023'
  21. AND DataMessage.Data != '0'

I am hoping to find a solution that will work well with Power BI and will split the delimited field into columns. Any help is greatly appreciated!

答案1

得分: 1

请尝试以下基于JSON的解决方案。它适用于SQL Server 2016及更高版本。

SQL

  1. -- DDL and sample data population, start
  2. DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, tokens VARCHAR(100));
  3. INSERT @tbl (tokens) VALUES
  4. ('32.49|33|30|100|20.5|21|18|100|33.39|33|30|100|1.4');
  5. -- DDL and sample data population, end
  6. SELECT t.ID
  7. , Col1 = JSON_VALUE(j,'$[0]')
  8. , Col2 = JSON_VALUE(j,'$[1]')
  9. , Col3 = JSON_VALUE(j,'$[2]')
  10. , Col4 = JSON_VALUE(j,'$[3]')
  11. , Col5 = JSON_VALUE(j,'$[4]')
  12. FROM @tbl AS t
  13. CROSS APPLY (VALUES ('["' + REPLACE(STRING_ESCAPE(tokens,'json'),'|','","') + '"]')) AS t1(j);

Output

ID Col1 Col2 Col3 Col4 Col5
1 32.49 33 30 100 20.5

在实际查询中,请进行以下修改:

  • 替换

    CROSS APPLY
    STRING_SPLIT(DataMessage.Data,'|')

    为以下内容:

    CROSS APPLY (VALUES ('["' + REPLACE(STRING_ESCAPE(DataMessage.Data,'json'),'|','","') + '"]')) AS t1(j)

  • 添加到 SELECT 子句中:

    Col1 = JSON_VALUE(j,'$[0]')
    , Col2 = JSON_VALUE(j,'$[1]')
    , Col3 = JSON_VALUE(j,'$[2]')
    , Col4 = JSON_VALUE(j,'$[3]')
    , Col5 = JSON_VALUE(j,'$[4]')

英文:

Please try the following JSON based solution. It will work starting from SQL Server 2016 onwards.

SQL

  1. -- DDL and sample data population, start
  2. DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, tokens VARCHAR(100));
  3. INSERT @tbl (tokens) VALUES
  4. ('32.49|33|30|100|20.5|21|18|100|33.39|33|30|100|1.4');
  5. -- DDL and sample data population, end
  6. SELECT t.ID
  7. , Col1 = JSON_VALUE(j,'$[0]')
  8. , Col2 = JSON_VALUE(j,'$[1]')
  9. , Col3 = JSON_VALUE(j,'$[2]')
  10. , Col4 = JSON_VALUE(j,'$[3]')
  11. , Col5 = JSON_VALUE(j,'$[4]')
  12. FROM @tbl AS t
  13. CROSS APPLY (VALUES ('["' + REPLACE(STRING_ESCAPE(tokens,'json'),'|','","') + '"]')) AS t1(j);

Output

ID Col1 Col2 Col3 Col4 Col5
1 32.49 33 30 100 20.5

In your real life query please make the following modification:

  • Replace

    CROSS APPLY
    STRING_SPLIT(DataMessage.Data,'|')

with the following:

  1. CROSS APPLY (VALUES ('["' + REPLACE(STRING_ESCAPE(DataMessage.Data,'json'),'|','","') + '"]')) AS t1(j)
  • Add to the SELECT clause:

    Col1 = JSON_VALUE(j,'$[0]')
    , Col2 = JSON_VALUE(j,'$[1]')
    , Col3 = JSON_VALUE(j,'$[2]')
    , Col4 = JSON_VALUE(j,'$[3]')
    , Col5 = JSON_VALUE(j,'$[4]')

huangapple
  • 本文由 发表于 2023年3月31日 22:54:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定