表里有每个人的姓名,科目,成绩
比如
jack math 90
tom english 90
jack english 90
想输出每个人的名字和科目个数
我不熟悉全部命令,初步想法是:
select name,count(select * from table where name=NAME) from table group by name;
请问count里的NAME应该有什么参数可以表示jack这样的某个人名
------解决方案--------------------
----------------------------------------------------------------
-- Author :DBA_HuangZJ(發糞塗牆)
-- Date :2014-08-08 10:52:55
-- Version:
-- Microsoft SQL Server 2012 - 11.0.5058.0 (X64)
-- May 14 2014 18:34:29
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([姓名] varchar(4),[科目] varchar(7),[成绩] int)
insert [huang]
select 'jack','math',90 union all
select 'tom','english',90 union all
select 'jack','english',90
--------------开始查询--------------------------
select [姓名],COUNT([科目])[科目数]
from [huang]
GROUP BY [姓名]
----------------结果----------------------------
/*
姓名 科目数
---- -----------
jack 2
tom 1
*/
------解决方案--------------------
select [姓名],count(1) as [科目数]
from 表
group by [姓名]