按两列分组,每行显示总计

时间:2013-07-25 04:15:27

标签: sql sql-server group-by sum

以下是列表数据。

Code   ItemCount   Type      Amount 
----------------------------------------
B001    1          Dell         10.00
B001    1          Dell         10.00
B001    1          Apple        10.00
B001    2          Apple        20.00
B001    2          Apple        20.00
B114    1          Apple        30.50
B114    1          Apple        10.00

我需要一个结果按代码,按类型和总计ItemCount进行分组,并获得每行Amount的总计。

这可能吗?

Code   ItemCount    Type      Amount 
----------------------------------------
B001    2          Dell          20.00
B001    5          Apple         50.00
B114    2          Apple         40.50 

6 个答案:

答案 0 :(得分:26)

请尝试:

SELECT
    Code,
    SUM(ItemCount) ItemCount,
    Type,
    SUM(Amount) Amount
FROM
    YourTable
GROUP BY Code, Type
ORDER BY Code

答案 1 :(得分:4)

这看起来像是家庭作业。

(我发誓当我第一次看到这个问题时,我认为这被标记为MySQL,但标题清楚地显示了MS SQL)

对于MySQL,此查询将返回指定的结果集:

SELECT t.Code
     , SUM(t.ItemCount) AS ItemCount
     , t.Type
     , s.Amount AS Amount
  FROM mytable t
 CROSS
  JOIN ( SELECT SUM(r.Amount) AS Amount
           FROM mytable r
       ) s 
 GROUP
    BY t.Code
     , t.Type
 ORDER BY t.Code ASC, t.Type DESC

对于其他数据库,删除For MySQL中列别名周围的反引号。

如果您需要保留大小写,对于Oracle,标识符用双引号括起来。对于SQL Server,标识符用方括号括起来。对于MySQL,标识符包含在反引号中。

答案 2 :(得分:2)

您可以尝试这个更简单的解决方案:

      <form name="myForm" id="myForm">
        <label>Select Amount</label>
        <select id="box1" type="select" oninput="calculate()" />
          <option value="choose" selected>Choose</option>
          <option value="15000">$15,000</option>
          <option value="20000">$20,000</option>
          <option value="25000">$25,000</option>
          <option value="30000">$30,000</option>
          <option value="35000">$35,000</option>
        </select>
        <label>Select Type</label>
          <select id="box2" type="select" oninput="calculate()" />
            <option value="x" selected>Choose</option>
            <option value=".21">1</option>
            <option value=".40">2</option>
          </select>
          <input class="button" type="submit" value="Submit" id="multiply">

        <p>
           <strong>here are the results:</strong> 
        </p>
        <h3>
            <strong>$<span id="result2"></span></strong> a week
        </h3>
      </form>

   <script>
   $(document).ready(function(){
   $('#multiply').click(function(event){


   event.preventDefault();
    var n1=$('#box1').val();
    var n2=$('#box2').val();
    var result=Math.round(n1*n2 /52);


    $('#resultholder2').fadeIn(200);
    $('#number1').append(n1);
    $('#number2').append(n2);
    $('#result2').append(result);
     });
     });
     </script>

理解'分组'会很方便

答案 3 :(得分:1)

您可以尝试此查询:

SELECT Code,SUM(ItemCount) AS ItemCount,Type,SUM(Amount) AS Amount
FROM
    table
GROUP BY Code, Type

这会给你正确的结果。您需要按CodeType分组,而不是ItemCount

答案 4 :(得分:0)

您提供的金额与样本数据不同,但这适用于样本数据值:

SELECT Code, SUM(ItemCount) AS ItemCount, [Type], SUM(Amount) AS Amount 
FROM dbo.TestSubs GROUP BY Code,[Type] ORDER BY Code

答案 5 :(得分:0)

如果我的理解是正确的,每行的实际总数是itemcount和金额的乘积,那么您可以使用下面的代码。如果不使用@Abu的代码。

;WITH cte AS
(
    SELECT Code, ItemCount, Type, Amount, ItemCount * Amount AS TotalAmount FROM <Table>
)
SELECT 
    Code,
    SUM(ItemCount),
    Type,
    SUM(TotalAmount)
FROM cte
GROUP BY Code, Type