求sql查询语句写法

表1:table1
id char
1 A
1 A
1 A
1 B
1 B
2 C
2 C
2 D
3 D
我想返回的数据集合为
id char count
1 A 3
1 B 2
2 C 2
2 D 1
3 D 1
说明:count列为char列的相同值在同一id值中的个数。

第1个回答  2007-09-08
这个简单,
select id,char,count(*) as 'count' from table1
group by id,char
这样就可以得到你想要的结果了.

count(*)括号里写的是你要按哪个列计数的列名,你的表中就两列,而你就是要按这两列计数,也就是按表中所有的列计数,说白就是计算这个表中有多少条重复的数据,所以就写count(*),也可以写count(id,char) 如果是其他的表话,你想按哪些列计数,你就在count(colum name,..)括号里写上那些列的列名就可以了.
第2个回答  2020-05-02
select
用户名=username,密码=max(case
when
attribute='ietf|0'
then
val
end),日期=max(case
when
attribute='ietf|1'
then
val
end),分组=max(case
when
attribute='ietf|2'
then
val
end)from
users
t1
group
by
username不改数据库结构可以用case
判断attrtype的值,每种值取对应的val,如上所示。
第3个回答  2020-02-07
select
*
from
(select
(select
ISNULL(pname,'')
from
person
where
person.pid=a.lovepid
and
person.psex='男')
as
newpname,avg(cast(a.pbeauty
as
int))as
pbeauty
from
(select
person.pid,person.pname,person.psex,person.pbeauty,love.lovepid
from
person,love
where
person.pid=love.pid)as
a
group
by
a.lovepid)as
e
where
newpname<>''
第4个回答  2019-03-19
select
l.lovepid,avg(p.pbeauty)
as
'平均美貌程度'
from
love
as
l,person
as
p
where
l.pid
=
p.pid
and
p.psex='女'
group
by
l.lovepid
第5个回答  2007-09-08
SELECT ID, CHAR, COUNT(*) AS Expr1
FROM 表1
GROUP BY ID,CHAR
GO

在查询分析器中运行本回答被提问者采纳
相似回答