当前位置: 代码迷 >> Sql Server >> 回来字段内记录数
  详细解决方案

回来字段内记录数

热度:20   发布时间:2016-04-24 21:33:41.0
返回字段内记录数
A     B
苹果   好 
苹果   中 
苹果   差
香蕉   好
香蕉   中
香蕉   差 
香蕉   差差
栗子   好
栗子   中
西瓜   好  
西瓜   中


要返回A 列 水果的种类和 4
字段?记录数

------解决方案--------------------
----------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-05-08 00:19:31
-- Version:
--      Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86) 
-- Jun 17 2011 00:57:23 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([A] varchar(4),[B] varchar(4))
insert [huang]
select '苹果','好' union all
select '苹果','中' union all
select '苹果','差' union all
select '香蕉','好' union all
select '香蕉','中' union all
select '香蕉','差' union all
select '香蕉','差差' union all
select '栗子','好' union all
select '栗子','中' union all
select '西瓜','好' union all
select '西瓜','中'
--------------开始查询--------------------------

select COUNT(DISTINCT a)
from [huang]
----------------结果----------------------------
/* 
-----------
4
*/
  相关解决方案