简体   繁体   English

Microsoft Query 中的 SQL 将 4 个选择和结果汇总到一列中

[英]SQL in Microsoft Query to join 4 selects and sum results in one column

I have an Excel table with this data:我有一个包含这些数据的 Excel 表格:

Micro |Group | Series|Group1 |Series1|Group2|Series2|Group3|Series3|
  1   |Back  |   3   |Biceps |   1   |Delts |   1   |Traps |   1   |
  1   |Chest |   4   |Triceps|   2   |Delts |   2   |      |       |
  1   |Biceps|   2   |       |       |      |       |      |       |
  1   |Cuads |   4   |Glut   |   2   |      |       |      |       |
  2   |Back  |   4   |Biceps |   2   |Delts |   2   |Traps |   2   |
  2   |Chest |   5   |Triceps|   3   |Delts |   3   |      |       |
  2   |Biceps|   3   |       |       |      |       |      |       |
  2   |Cuads |   5   |Glut   |   3   |      |       |      |       |
....

I want to generate a table witch shows amount of series of each micro by group adding Series+Series1+Series2+Series3:我想通过添加 Series+Series1+Series2+Series3 来生成一个表格,显示每个微的系列数量:

Micro | Group  | Series|
  1   | Back   |   3   |
  1   | Chest  |   4   |
  1   | Biceps |   3   |
  1   | Cuads  |   4   |
  1   | Triceps|   2   |
  1   | Delts  |   3   |
  1   | Traps  |   1   |
  2   | Back   |   4   |
  2   | Chest  |   5   |
  2   | Biceps |   5   |
  2   | Cuads  |   5   |
  2   | Triceps|   3   |
  2   | Delts  |   5   |
  2   | Traps  |   2   |

I have very basic SQL skills and do not know how to perform a query in Microsoft Query to get this table.我有非常基本的 SQL 技能,不知道如何在 Microsoft Query 中执行查询以获取此表。

Thanks!谢谢!

Start from the second column, and every two columns make a pair, there are a total of 4 pairs of them.从第二列开始,每两列做一对,一共4对。 The key point is to convert the N-column table into a table consisting of 3 columns.关键是把N列的表转换成3列组成的表。 Using esProc is much easy.使用集算器非常容易。

    A
1   =file("data.xlsx").xlsimport@w().to(2,)
2   =A1.news(~.len()\2;A1.~(1):Micro,A1.~(#*2):Group,A1.~(#*2+1):Series)
3   =A2.groups(Micro,Group;sum(Series):Series)
4   =file("result.xlsx").xlsexport@t(A3)

For more reference, see http://c.raqsoft.com/article/1609815706959更多参考见http://c.raqsoft.com/article/1609815706959

DISCLAIMER: This is about our tool esProc.免责声明:这是关于我们的工具 esProc。 It's freemium.是免费增值。

To do this with Power Query, the basic steps are要使用 Power Query 执行此操作,基本步骤是

  • Unpivot逆枢轴
  • Filter out the blanks过滤掉空白
  • Group in pairs ( Group/Series )成对Group/Series ( Group/Series )
  • Extract the data提取数据
  • split into new columns拆分为新列

Below is the M-Code.下面是M代码。 You can explore this by examining the "Applied Steps" where you can see the comments I made;您可以通过检查“应用步骤”来探索这一点,您可以在其中查看我所做的评论; and also, by clicking or double-clicking on each step, you can see what is being done.此外,通过单击或双击每个步骤,您可以看到正在执行的操作。

There may be more efficient methods of doing this, as I am a relative beginner with PQ, but this should get you started.可能有更有效的方法来做到这一点,因为我是 PQ 的相对初学者,但这应该让你开始。

BTW, the result will not be in the same order as you show, as PQ pulls things together row-wise rather than column-wise.顺便说一句,结果将与您显示的顺序不同,因为 PQ 将事物按行而不是按列合并在一起。 But you can insert a sorting option if you need the results in a particular order.但是,如果您需要按特定顺序排列的结果,则可以插入排序选项。

M Code M码

let
    Source = Excel.CurrentWorkbook(){[Name="Table14"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Micro", Int64.Type}, {"Group", type text}, {"Series", Int64.Type}, {"Group1", type text}, {"Series1", Int64.Type}, {"Group2", type text}, {"Series2", Int64.Type}, {"Group3", type text}, {"Series3", Int64.Type}}),
    
    //select Micro column and unpivot
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Micro"}, "Attribute", "Value"),

    //filter out the blanks
    #"Filtered Rows1" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> "")),

    //Split off the terminal number, so as to leave identical "former"column headers
    #"Split Column by Character Transition" = Table.SplitColumn(#"Filtered Rows1", "Attribute", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Attribute.1", "Attribute.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Character Transition",{"Attribute.2"}),

    //add index and create a series for grouping in pairs
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1, Int64.Type),
    #"Inserted Integer-Division" = Table.AddColumn(#"Added Index", "Integer-Division", each Number.IntegerDivide([Index], 2), Int64.Type),
    #"Removed Columns1" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index"}),

    //Group by the pairs
    #"Grouped Rows" = Table.Group(#"Removed Columns1", {"Integer-Division"}, {{"Grouped", each _, type table [#"Micro "=nullable number, Attribute.1=nullable text, Value=any, #"Integer-Division"=number]}}),

    //extract the Micro and Value columns from each grouped table
    //then split into new columns
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Group", each List.Combine({{List.First(Table.Column([Grouped],"Micro"))},
Table.Column([Grouped],"Value")})),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Group", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Group", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Group.1", "Group.2", "Group.3"}),

    //cleanup - set data types, remove unneeded columns
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Group.1", Int64.Type}, {"Group.2", type text}, {"Group.3", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Group.1", "Micro"}, {"Group.2", "Group"}, {"Group.3", "Series"}}),
    #"Removed Columns2" = Table.RemoveColumns(#"Renamed Columns",{"Integer-Division", "Grouped"}),

    //Group by "Group" and SUM
    #"Grouped Rows1" = Table.Group(#"Removed Columns2", {"Micro", "Group"}, {{"Series", each List.Sum([Series]), type nullable number}})
in
    #"Grouped Rows1"

在此处输入图片说明

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

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