【问题标题】:making simple sql query count制作简单的 sql 查询计数
【发布时间】:2013-03-13 08:55:16
【问题描述】:

我有这样的数据库结构>

ID |  Party_Code |  Trade_Qty | Market_Rate
-------------------------------------------    
 1     8070            5           10.50
 2     8745            15          80.35
 3     8070            6           45.60

这只是示例数据。实际上这个表包含 40000 行。

从这个示例中我们可以看到Party_Code 列可以有重复值。

我正在尝试查找 countdistinct 派对代码。

为此,我尝试了以下两个失败的查询:

select count(distinct(Party_Code)) from tradeFile

select distinct(count(Party_Code)) from tradeFile

这两个查询都失败了。

我想知道我哪里出错了?

这样的查询怎么写?

【问题讨论】:

  • 它正在工作sqlfiddle.com/#!3/6efbd/1
  • 确实有效。而且它在逻辑上也很好。但它对 SQLServer 2005 没有任何显示[0]
  • 不要使用 distinct 你只需要 count(partycodes) group by partycodes 说像 select count(partycode) group by partycode
  • @TonyShih :以前它向我显示 0,但现在有了这个查询,它什么也没显示。

标签: sql database sql-server-2005


【解决方案1】:
select count(distinct Party_Code) from tradeFile

【讨论】:

  • count(*) 返回什么?
【解决方案2】:

使用 Count 函数时,最好与 Group By 子句一起使用:

select count(*) as Quantity from tradeFile Group By Party_Code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    相关资源
    最近更新 更多