【问题标题】:How to write a SELECT DISTINCT query?如何编写 SELECT DISTINCT 查询?
【发布时间】:2017-08-28 14:48:45
【问题描述】:

我有一个类别列表,我可以使用 select distinct 语句,但很难“计算”一个类别的使用次数,我有一个名为 Description 的列,我需要创建另一个名为occurrence 的列。

Description
-------------
Arts, crafts, and collectibles
Baby
Beauty and fragrances
Books and magazines
Business to business
Clothing, accessories, and shoes
Computers, accessories, and services
Education
Electronics and telecom
Entertainment and media
Financial services and products
Food retail and service
Gifts and flowers
Government
Health and personal care
Home and garden
Nonprofit
Pets and animals
Religion and spirituality (for profit)
Retail (not elsewhere classified)
Services - other
Sports and outdoors
Toys and hobbies
Travel
Vehicle sales
Vehicle service and accessories
Books and magazines
Business to business
Clothing, accessories, and shoes
Computers, accessories, and services
Education
Electronics and telecom
Entertainment and media
Financial services and products
Food retail and service
Gifts and flowers
Government
Health and personal care
Services - other
Sports and outdoors
Toys and hobbies
Travel
Vehicle sales
Vehicle service and accessories
Books and magazines
Business to business
Clothing, accessories, and shoes
Business to business
Clothing, accessories, and shoes
Computers, accessories, and services
Education
Electronics and telecom
Entertainment and media
Financial services and products
Food retail and service

【问题讨论】:

标签: sql sql-server reporting-services


【解决方案1】:

这听起来像是一个简单的聚合。

Select
   [Description]
   ,Occurences = Count([Description])
From
   YourTable
Group By
   [Description]

【讨论】:

  • 嗯,它会按照你的要求做。也许你问的不是你需要的。为什么它不起作用?
  • 这很酷,我不知道你可以做'alias = COUNT(column)'。与 COUNT(column) 作为别名相反,equals 有区别吗?
  • 没有@Simon 但有些人有made good arguments 为什么你不应该使用AS
  • 只是为了记录,你也可以在没有as关键字的情况下执行Count(Description) Ocurrences之类的操作
  • 成功了!抱歉没有仔细看你的答案,我很感激!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 2011-10-30
  • 2021-07-06
  • 2014-05-01
  • 1970-01-01
  • 2015-08-24
  • 2014-04-05
相关资源
最近更新 更多