sql分类统计的问题

shenlong0125 2010-01-07 08:41:57
BookType 图书类型表

BookTypeId BookTypeName flfatherID
1 马克思主义 0
2 自然科学 0
3 马克思著作 1
4 单行著作 3
5 单行著作精选 4 并且图书分类是无限延伸的



Book 图书信息表

BookId BookType BookName BookStock
1 4 单行著作1 5
2 4 双行著作 5
3 3 专题汇编 5


根据传过来的flfatherID,查询BookTypeId、BookTypeName、BookStock,也就是图书类型ID、图书类型名称、该类图书的所有库存。
而我现在查的是,只要图书信息表中存在的BookTypeID,就能查出所有图书库存数量,而这个BookTypeID的flfatherID的图书库存数量却为0。哪位大侠能帮忙解决一下。
...全文
230 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenlong0125 2010-01-07
  • 打赏
  • 举报
回复
create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int)

f_cid((@BookTypeId int)是图书类型ID吧!不是那个flfatherID吗?

select m.BookTypeId,m.BookTypeName,m.flfatherID,isnull(sum(n.BookStock),0) AS BookStock from
(select a.* from BookType a , f_cid(0) b where a.BookTypeId = b.BookTypeId) m,book n
where m.BookTypeId = n.BookType GROUP BY m.BookTypeId,m.BookTypeName,m.flfatherID

这个函数 f_cid(0) 都不能够查出 flfatherID为0的BookTypeId
xiedi1209 2010-01-07
  • 打赏
  • 举报
回复
下面的 函数 收藏了 ~
dawugui 2010-01-07
  • 打赏
  • 举报
回复
不好意思.你把order by 去掉.

--调用函数查询1及其所有子节点
select m.* , n.* from
(select a.* from tb a , f_cid(1) b where a.BookTypeId = b.BookTypeId ) m,
book n
where m.BookTypeId = n.BookType
shenlong0125 2010-01-07
  • 打赏
  • 举报
回复


--查询指定节点及其所有子节点的函数
create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @BookTypeId , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.BookTypeId , @level
from tb a , @t_Level b
where a.flfatherID = b.BookTypeId and b.level = @level - 1
end
return
end
go

--调用函数查询1及其所有子节点
select m.* , n.* from
(select a.* from tb a , f_cid(1) b where a.BookTypeId = b.BookTypeId order by a.BookTypeId) m,
book n
where m.BookTypeId = n.BookType

执行出现这样的问题。

消息 1033,级别 15,状态 1,第 2 行
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。

麻烦给解释一下,我是个新手。
htl258_Tony 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 dawugui 的回复:]
引用 6 楼 htl258 的回复:
有老D兄在场,我只能来捡剩下的.

哈哈,咋能这么说呢?

欢迎抢.

我最多还能抢一个星期,就又要被警察"保护"了.
[/Quote]

还好论坛抢手多,是件好事,要不论坛就不热闹了.欢迎大家一起抢,一起学习进步.
dawugui 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 htl258 的回复:]
有老D兄在场,我只能来捡剩下的.
[/Quote]
哈哈,咋能这么说呢?

欢迎抢.

我最多还能抢一个星期,就又要被警察"保护"了.
dawugui 2010-01-07
  • 打赏
  • 举报
回复
大致为这样:
--查询指定节点及其所有子节点的函数
create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @BookTypeId , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.BookTypeId , @level
from tb a , @t_Level b
where a.flfatherID = b.BookTypeId and b.level = @level - 1
end
return
end
go

--调用函数查询1及其所有子节点
select m.* , n.* from
(select a.* from tb a , f_cid(1) b where a.BookTypeId = b.BookTypeId order by a.BookTypeId) m,
book n
where m.BookTypeId = n.BookType


如果还需要进行统计,你就sum(...) group by ....
htl258_Tony 2010-01-07
  • 打赏
  • 举报
回复
有老D兄在场,我只能来捡剩下的.
htl258_Tony 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 beirut 的回复:]
引用 1 楼 htl258 的回复:
结果应为如何

tony 哥又开始了疯狂的强分行动
[/Quote]
偶尔出动,不会疯狂的.
dawugui 2010-01-07
  • 打赏
  • 举报
回复
你需要先按照下面的函数获取到所有的子节点,然后再联合Book表进行统计.

/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 10 行)
*/

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 7 行)
*/

drop table tb
drop function f_cid



@@ROWCOUNT:返回受上一语句影响的行数。
返回类型:integer。
注释:任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。
示例:下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。

UPDATE authors SET au_lname = 'Jones' WHERE au_id = '999-888-7777'
IF @@ROWCOUNT = 0
print 'Warning: No rows were updated'

结果:

(所影响的行数为 0 行)
Warning: No rows were updated

shenlong0125 2010-01-07
  • 打赏
  • 举报
回复
显示,图书类型ID、图书类型名称、库存数
黄_瓜 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 htl258 的回复:]
结果应为如何
[/Quote]
tony 哥又开始了疯狂的强分行动
htl258_Tony 2010-01-07
  • 打赏
  • 举报
回复
结果应为如何
shenlong0125 2010-01-07
  • 打赏
  • 举报
回复
create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @BookTypeId , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.BookTypeId , @level
from BookType a , @t_Level b
where a.flfatherID = b.BookTypeId and b.level = @level - 1
end
return
end
go

select m.BookTypeId,m.BookTypeName,m.flfatherID,isnull(sum(n.BookStock),0) AS BookStock from
(select a.* from BookType a , f_cid(0) b where a.BookTypeId = b.BookTypeId) m,book n
where m.BookTypeId = n.BookType GROUP BY m.BookTypeId,m.BookTypeName,m.flfatherID

这些不能够统计出flfatherID=0的所有BookTypeId
shenlong0125 2010-01-07
  • 打赏
  • 举报
回复
哪位大侠帮忙看一下啊。
xls829 2010-01-07
  • 打赏
  • 举报
回复
不知道
shenlong0125 2010-01-07
  • 打赏
  • 举报
回复
create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @BookTypeId , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.BookTypeId , @level
from BookType a , @t_Level b
where a.flfatherID = b.BookTypeId and b.level = @level - 1
end
return
end
go

select m.BookTypeId,m.BookTypeName,m.flfatherID,isnull(sum(n.BookStock),0) AS BookStock from
(select a.* from BookType a , f_cid(0) b where a.BookTypeId = b.BookTypeId) m,book n
where m.BookTypeId = n.BookType GROUP BY m.BookTypeId,m.BookTypeName,m.flfatherID

这些不能够统计出flfatherID=0的所有BookTypeId

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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