SQL计数组数

时间:2015-05-14 22:35:40

标签: sql-server group-by sql-server-2012

我有一个托盘,物品,物品数量的表:

pallet | item | qty
-------------------
  1        1     2
  1        2     4
  2        3     2
  2        5     3
  3        4     4

我需要找到计数(托盘),计数(项目),总和(数量)

count(pallets) | count(items) | sum(qty)
----------------------------------------
      3                5           15

我可以用

得到总和(数量)和数量(项目)
select count(0) as totalItems, sum(qty) as total from table

有没有办法在没有子查询的情况下获得托盘数量?

2 个答案:

答案 0 :(得分:8)

是的,请使用transition.selectAll("text") .filter(function(d) { return d.parent === focus || this.style.display === "inline"; }) .style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; }) .each("start", function(d) { if (d.parent === focus) this.style.display = "inline"; }) .each("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });

DISTINCT

答案 1 :(得分:0)

只需使用Distinct即可避免重复记录计数。

count(Distinct pallet)

您的查询

select 
      count(distinct pallet) as pallets,
      sum(qty) as Total, 
      count(item) AS [Total Items]

它将输出AS:

it will give output AS :