我的表结构是这样的
品牌 零售户ID 需求量
01 100 20
01 101 30
02 102 40
02 100 40
03 101 60我希望得到的结果是品牌 零售户数 总户数
01 2 3
02 2 3
03 1 3能否用1条sql文查询出来,我按照品牌分组只能得到前两列,最后的总户数表示不出来。
品牌 零售户ID 需求量
01 100 20
01 101 30
02 102 40
02 100 40
03 101 60我希望得到的结果是品牌 零售户数 总户数
01 2 3
02 2 3
03 1 3能否用1条sql文查询出来,我按照品牌分组只能得到前两列,最后的总户数表示不出来。
解决方案 »
- 第二次求助熟悉存储过程的同行!
- sql 语句 截取字段中的后面字符串。
- PowderDesigner如何修改数据库?
- sql server 2008,不能访问磁盘文件
- SQL 08 安装问题
- background:#FFFFFF none repeat scroll 0 0;后面的属性位置固定么,可以变么?
- 日志太大怎么办?
- 急:两表连接显示数据 在线等
- 我想知道一个往sql server里面添加数据得很快的方法!谢谢了!不要说用ADO ADDNEW
- 怎么启动一个Data transformation Services的一个local Packages??????
- 求助高手SQL Server建不了数据库
- SQL server2005有没有监视性能(包括空间和时间)的工具?
insert into tb values('01', '100', 20)
insert into tb values('01', '101', 30)
insert into tb values('02', '102', 40)
insert into tb values('02', '100', 40)
insert into tb values('03', '101', 60)
goselect 品牌 , count(零售户ID) 零售户数 , (select count(distinct 零售户ID) from tb) 总户数 from tb group by 品牌/*
品牌 零售户数 总户数
---------- ----------- -----------
01 2 3
02 2 3
03 1 3(所影响的行数为 3 行)
*/select 品牌 , count(distinct 零售户ID) 零售户数 , (select count(distinct 零售户ID) from tb) 总户数 from tb group by 品牌/*
品牌 零售户数 总户数
---------- ----------- -----------
01 2 3
02 2 3
03 1 3(所影响的行数为 3 行)
*/drop table tb
if object_id('tb') is not null
drop table tb
go
create table tb
(
品牌 varchar(10),
零售户ID int,
需求量 int
)
go
insert into tb
select '01',100,20 union all
select '01',101,30 union all
select '02',102,40 union all
select '02',100,40 union all
select '03',101,60
go
select 品牌,零售户数=count(distinct 零售户ID),总户数=(select count(distinct 零售户ID) from tb) from tb group by 品牌
go
/*
品牌 零售户数 总户数
---------- ----------- -----------
01 2 3
02 2 3
03 1 3(3 行受影响)
*/
01 100 20
01 101 30
02 102 40
02 100 40
03 101 60
create table tbl(
brandid varchar(4) not null,
salesid varchar(4) not null,
neednum int not null
)insert into tbl values('01','100',20)
insert into tbl values('01','101',30)
insert into tbl values('02','102',40)
insert into tbl values('02','102',40)
insert into tbl values('03','101',60)我希望得到的结果是
select
brandid as 品牌,
count(salesid)as 零售户数,
(select count(distinct salesid)from tbl)as 总户数
from tbl
group by
brandid品牌 零售户数 总户数
01 2 3
02 2 3
03 1 3
总户数=(select count(distinct 零售户ID) from tb)
from tb group by 品牌