• <i id='mdkbH'><tr id='mdkbH'><dt id='mdkbH'><q id='mdkbH'><span id='mdkbH'><b id='mdkbH'><form id='mdkbH'><ins id='mdkbH'></ins><ul id='mdkbH'></ul><sub id='mdkbH'></sub></form><legend id='mdkbH'></legend><bdo id='mdkbH'><pre id='mdkbH'><center id='mdkbH'></center></pre></bdo></b><th id='mdkbH'></th></span></q></dt></tr></i><div id='mdkbH'><tfoot id='mdkbH'></tfoot><dl id='mdkbH'><fieldset id='mdkbH'></fieldset></dl></div>

    1. <tfoot id='mdkbH'></tfoot>
    2. <small id='mdkbH'></small><noframes id='mdkbH'>

        <bdo id='mdkbH'></bdo><ul id='mdkbH'></ul>
      1. <legend id='mdkbH'><style id='mdkbH'><dir id='mdkbH'><q id='mdkbH'></q></dir></style></legend>
      2. 创建/附加表,其中包含按不同类别分组的值的总

        时间:2023-08-20
          <bdo id='BnIWO'></bdo><ul id='BnIWO'></ul>
          <tfoot id='BnIWO'></tfoot>

        • <i id='BnIWO'><tr id='BnIWO'><dt id='BnIWO'><q id='BnIWO'><span id='BnIWO'><b id='BnIWO'><form id='BnIWO'><ins id='BnIWO'></ins><ul id='BnIWO'></ul><sub id='BnIWO'></sub></form><legend id='BnIWO'></legend><bdo id='BnIWO'><pre id='BnIWO'><center id='BnIWO'></center></pre></bdo></b><th id='BnIWO'></th></span></q></dt></tr></i><div id='BnIWO'><tfoot id='BnIWO'></tfoot><dl id='BnIWO'><fieldset id='BnIWO'></fieldset></dl></div>

              <tbody id='BnIWO'></tbody>

            <small id='BnIWO'></small><noframes id='BnIWO'>

                • <legend id='BnIWO'><style id='BnIWO'><dir id='BnIWO'><q id='BnIWO'></q></dir></style></legend>
                • 本文介绍了创建/附加表,其中包含按不同类别分组的值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个费用表,例如:

                  I have an expense table like:

                  WorkWeek  Catg  Item    Cost
                  WorkWeek1 Cat1  Item1   Price
                  WorkWeek1 Cat1  Item2   Price
                  WorkWeek1 Cat1  Item3   Price
                  WorkWeek1 Cat1  Item4   Price
                  WorkWeek1 Cat2  Item1   Price
                  WorkWeek1 Cat2  Item5   Price
                  WorkWeek1 Cat2  Item6   Price
                  WorkWeek1 Cat3  Item1   Price
                  WorkWeek1 Cat3  Item5   Price
                  .
                  .
                  WorkWeekA CatB  ItemC   Price
                  

                  这就是我现在的做法:

                  select top(1)
                       (select sum(cost) from DataTable where Catg like 'Cat1') as Cat1TotalCost
                      ,(select sum(cost) from DataTable where Catg like 'Cat2') as Cat2TotalCost
                      ,(select sum(cost) from DataTable where Catg like 'Cat3') as Cat3TotalCost
                      .
                      .
                      .
                      .
                  from DataTable where WorkWeek like 'WorkWeek1'
                  

                  如果我不使用 top 1 那么我会得到相同的总和,就像数千行一样重复.此外,我的做法只占 1 个工作周.:(

                  And If I don't use the top 1 then I get the same sums repeated over like thousands of rows. Also, my way of doing it only accounts for 1 workweek. :(

                  我想创建一个表格,其中每个工作周的总费用取决于每个类别,例如:

                  I want to create a Table with each workweeks total expense depending in each category something like :

                  WorkWeek1   Cat1TotalCost   Cat2TotalCost   Cat3TotalCost
                  WorkWeek2   Cat1TotalCost   Cat2TotalCost   Cat3TotalCost
                  .
                  .
                  

                  推荐答案

                  试试这个:

                  select
                      workweek
                      ,(select sum(cost) from DataTable where Catg = 'Cat1') as Cat1TotalCost
                      ,(select sum(cost) from DataTable where Catg = 'Cat2') as Cat2TotalCost
                      ,(select sum(cost) from DataTable where Catg = 'Cat3') as Cat3TotalCost
                      .
                      .
                      .
                      .
                  from DataTable
                  group by Workweek
                  

                  现在,您正在按工作周字段分组.此外,我将 like 更改为 = 以使其稍微快一些.

                  Now, you are grouping by the workweek field. Also, I changed the like to = to make it slightly faster.

                  这篇关于创建/附加表,其中包含按不同类别分组的值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何将点附加到 LINESTRING SQL 下一篇:mysql 选择更新

                  相关文章

                  最新文章

                • <i id='tvkKA'><tr id='tvkKA'><dt id='tvkKA'><q id='tvkKA'><span id='tvkKA'><b id='tvkKA'><form id='tvkKA'><ins id='tvkKA'></ins><ul id='tvkKA'></ul><sub id='tvkKA'></sub></form><legend id='tvkKA'></legend><bdo id='tvkKA'><pre id='tvkKA'><center id='tvkKA'></center></pre></bdo></b><th id='tvkKA'></th></span></q></dt></tr></i><div id='tvkKA'><tfoot id='tvkKA'></tfoot><dl id='tvkKA'><fieldset id='tvkKA'></fieldset></dl></div>
                  <legend id='tvkKA'><style id='tvkKA'><dir id='tvkKA'><q id='tvkKA'></q></dir></style></legend>

                  • <bdo id='tvkKA'></bdo><ul id='tvkKA'></ul>
                    <tfoot id='tvkKA'></tfoot>

                      <small id='tvkKA'></small><noframes id='tvkKA'>