这句分类汇总的语句如何写

sljz 2009-08-04 02:33:35
销售表a:

序号 名称 类别 数量
-------------------------
1 货1 1 20
2 货2 1 33
3 货3 2 10
4 货4 2 10
5 货5 2 20
6 货6 3 90

类别表b:

序号 名称
-------------
1 PC
2 Notebook
3 Mainboard


a.类别 = b.序号

****************************************************

想要实现统计:

类别 销售次数 销售数量
---------------------------------
PC 2 55
Notebook 3 40
Mainboard 1 90
...全文
79 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2009-08-04
  • 打赏
  • 举报
回复



declare @a table (序号 int,名称 nvarchar(10),类别 int,数量 int)
insert into @a select 1,'货1',1,20
union all select 2,'货2',1,33
union all select 3,'货3',2,10
union all select 4,'货4',2,10
union all select 5,'货4',2,20
union all select 6,'货4',3,90
declare @b table (序号 int,名称 nvarchar(10))
insert into @b select 1,'pc'
union all select 2,'notebook'
union all select 3,'mainboard'
select b.名称,销售次数=COUNT(类别),销售数量=SUM(数量) from @a a right join @b b on a.类别=b.序号
group by b.名称,b.序号 order by b.序号


名称 销售次数 销售数量
---------- ----------- -----------
pc 2 53
notebook 3 40
mainboard 1 90

(3 行受影响)
SQL77 2009-08-04
  • 打赏
  • 举报
回复
declare @ta table(序号 int,  名称 varchar(10) , 类别 int,   数量 int)
insert @ta select 1 ,'货1', 1 , 20
insert @ta select 2 ,'货2', 1 , 33
insert @ta select 3 ,'货3', 2 , 10
insert @ta select 4 ,'货4', 2 , 10
insert @ta select 5 ,'货5', 2 , 20
insert @ta select 6 ,'货6', 3 , 90

declare @tb table (序号 int, 名称 varchar(20) )

insert @tb select 1 ,'PC'
insert @tb select 2 ,'Notebook'
insert @tb select 3 ,'Mainboard'


SELECT
B.名称 AS 类别,次数,数量
FROM

(SELECT 类别,COUNT(类别)次数,SUM(数量)AS 数量 FROM @ta GROUP BY 类别) AS A

JOIN

@tb B

ON A.类别=B.序号

类别 次数 数量
-------------------- ----------- -----------
PC 2 53
Notebook 3 40
Mainboard 1 90

(所影响的行数为 3 行)



--GROUP BY B.名称 ,数量
叶子 2009-08-04
  • 打赏
  • 举报
回复


declare @销售表 table (序号 int,名称 varchar(3),类别 int,数量 int)
insert into @销售表
select 1,'货1',1,20 union all
select 2,'货2',1,33 union all
select 3,'货3',2,10 union all
select 4,'货4',2,10 union all
select 5,'货5',2,20 union all
select 6,'货6',3,90


declare @类别表 table (序号 int,名称 varchar(9))
insert into @类别表
select 1,'PC' union all
select 2,'Notebook' union all
select 3,'Mainboard'

select 名称 as 类别,[count] as 销售次数,[sum] as 销售数量 from @类别表 a
left join (
select 类别,count(类别) as [count],sum(数量) as [sum] from @销售表 group by 类别
) b
on a.序号=b.类别
/*
类别 销售次数 销售数量
--------- ----------- -----------
PC 2 53
Notebook 3 40
Mainboard 1 90
*/



--小F-- 2009-08-04
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-08-04 14:42:02
----------------------------------------------------------------
--> 测试数据:[ta]
if object_id('[ta]') is not null drop table [ta]
create table [ta]([序号] int,[名称] varchar(3),[类别] int,[数量] int)
insert [ta]
select 1,'货1',1,20 union all
select 2,'货2',1,33 union all
select 3,'货3',2,10 union all
select 4,'货4',2,10 union all
select 5,'货5',2,20 union all
select 6,'货6',3,90
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([序号] int,[名称] varchar(9))
insert [tb]
select 1,'PC' union all
select 2,'Notebook' union all
select 3,'Mainboard'
--------------开始查询--------------------------
select
b.名称 as 类别,
count(a.类别) as 销售次数,
sum(a.数量) as 销售数量
from
ta a ,tb b
where
a.类别 = b.序号
group by
b.名称
----------------结果----------------------------
/*类别 销售次数 销售数量
--------- ----------- -----------
Mainboard 1 90
Notebook 3 40
PC 2 53

(所影响的行数为 3 行)

*/
feixianxxx 2009-08-04
  • 打赏
  • 举报
回复

declare @ta table(序号 int, 名称 varchar(10) , 类别 int, 数量 int)
insert @ta select 1 ,'货1', 1 , 20
insert @ta select 2 ,'货2', 1 , 33
insert @ta select 3 ,'货3', 2 , 10
insert @ta select 4 ,'货4', 2 , 10
insert @ta select 5 ,'货5', 2 , 20
insert @ta select 6 ,'货6', 3 , 90

declare @tb table (序号 int, 名称 varchar(20) )

insert @tb select 1 ,'PC'
insert @tb select 2 ,'Notebook'
insert @tb select 3 ,'Mainboard'

select
类别=b.名称,
销售次数=isnull(COUNT(*),0),
销售数量=isnull(SUM(数量),0)
from @tb b left join @ta a on b.序号=a.类别
group by b.名称
/*
(1 行受影响)
类别 销售次数 销售数量
-------------------- ----------- -----------
Mainboard 1 90
Notebook 3 40
PC 2 53

*/
SQL77 2009-08-04
  • 打赏
  • 举报
回复
SELECT 
B.名称 AS 类别,COUNT(A.类别) AS 次数,数量
FROM

(SELECT 类别,SUM(数量)AS 数量 FROM A GROUP BY 类别) AS A

JOIN

B

ON A.类别=B.序号
soft_wsx 2009-08-04
  • 打赏
  • 举报
回复
select b.名称 类别,
count(*) 销售次数,
sum(数量)销售数量
from ta a ,tb b where a.类别 = b.序号
group by b.名称


--where,inner join on,left join on很可以
sdhdy 2009-08-04
  • 打赏
  • 举报
回复


declare @ta table(序号 int, 名称 varchar(10) , 类别 int, 数量 int)
insert @ta select 1 ,'货1', 1 , 20
insert @ta select 2 ,'货2', 1 , 33
insert @ta select 3 ,'货3', 2 , 10
insert @ta select 4 ,'货4', 2 , 10
insert @ta select 5 ,'货5', 2 , 20
insert @ta select 6 ,'货6', 3 , 90

declare @tb table (序号 int, 名称 varchar(20) )

insert @tb select 1 ,'PC'
insert @tb select 2 ,'Notebook'
insert @tb select 3 ,'Mainboard'

select 类别=b.名称,销售次数=count(a.类别),销售数量=sum(a.数量)
from @ta a
left join @tb b on a.类别 = b.序号
group by b.名称

/*
类别 销售次数 销售数量
-------------------- ----------- -----------
Mainboard 1 90
Notebook 3 40
PC 2 53

(所影响的行数为 3 行)
*/
水族杰纶 2009-08-04
  • 打赏
  • 举报
回复
set nocount on
declare @a table(序号 int,名称 nvarchar(10),类别 int, 数量 int)
-------------------------
insert @a select 1 ,'货1', 1 , 20
insert @a select 2 ,'货2' , 1 , 33
insert @a select 3 , '货3' , 2 , 10
insert @a select 4 , '货4' , 2 , 10
insert @a select 5 , '货5' ,2 , 20
insert @a select 6, '货6' ,3, 90
declare @b table(序号 int,名称 varchar(10))
-------------
insert @b select 1 , 'PC'
insert @b select 2 , 'Notebook'
insert @b select 3, 'Mainboard'
select b.名称 类别,
count(*) 销售次数,
sum(数量)销售数量
from @a a ,@b b where a.类别 = b.序号
group by b.名称
/*
类别 销售次数 销售数量
---------- ----------- -----------
Mainboard 1 90
Notebook 3 40
PC 2 53
*/
--小F-- 2009-08-04
  • 打赏
  • 举报
回复

select
b.名称 as 类别,
count(a.类别) as 销售次数,
sum(a.数量) as 销售数量
from
a,b
where
a.类别 = b.序号
group by
b.名称
feixianxxx 2009-08-04
  • 打赏
  • 举报
回复
select
类别=b.名称,
销售次数=isnull(COUNT(*),0),
销售数量=isnull(SUM(数量),0)
from b left join a on b.序号=a.类别
group by b.名称
--小F-- 2009-08-04
  • 打赏
  • 举报
回复
select 
b.序号 as 类别,
count(a.类别) as 销售次数,
sum(a.数量) as 销售数量
from
a,b
where
a.类别 = b.序号
group by
b.序号
中国风 2009-08-04
  • 打赏
  • 举报
回复
select b.名称 as 类别,count(*) as 销售次数,sum(数量) as 销售数量
from a inner join b on a.类别=b.序号
group by b.名称
水族杰纶 2009-08-04
  • 打赏
  • 举报
回复
select b.名称 类别,
count(*) 销售次数,
sum(数量)销售数量
from ta a ,tb b where a.类别 = b.序号
group by b.名称
sdhdy 2009-08-04
  • 打赏
  • 举报
回复
select 类别=b.name,销售次数=count(a.类别),销售数量=sum(a.数量) from a 
left join b on a.类别 = b.序号
group by b.name

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧