首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请高手解决SQL难题!该怎么解决

2012-01-19 
请高手解决SQL难题!!第一题目:有如下表,分别记录了“张三”每天消费的情况。其中,消费类型只有3种:1买香烟2加

请高手解决SQL难题!!
第一题目:
有如下表,分别记录了“张三”每天消费的情况。其中,消费类型只有3种:1   买香烟   2   加油   3   吃饭  

-------------------------------------------------
时间     |   消费类型   |   消费金额   |
-------------------------------------------------
2006-1-1   |   1     |   10.00     |
-------------------------------------------------
2006-1-1   |   2     |   200.00   |
-------------------------------------------------
2006-1-1   |   1     |   12.00     |
-------------------------------------------------
2006-1-1   |   3     |   12.00     |
-------------------------------------------------
2006-1-2   |   1     |   11.00     |
-------------------------------------------------
2006-1-2   |   3     |   107.00   |
-------------------------------------------------
2006-1-3   |   1     |   17.00     |
-------------------------------------------------
2006-1-3   |   3     |   17.00     |
-------------------------------------------------
2006-1-4   |   1     |   10.00     |
-------------------------------------------------
.........  

请从上表用   “一句组合查询”   查询出每日消费统计表,要求检索出的内容格式如下:
-------------------------------------
年   |   月   |   日   |   买香烟   |   加油     |   吃饭     |
-------------------------------------
2006   |   1   |   1   |   22.00     |   200.00   |   12.00     |
-------------------------------------
2006   |   1   |   2   |   11.00     |   0.00     |   107.00   |
-------------------------------------
2006   |   1   |   3   |   17.00     |   0.00     |   17.00     |
-------------------------------------
2006   |   1   |   4   |   10.00     |   0.00     |   0.00     |
-------------------------------------  

请执行下列语句生成:  

create   table   CostList   (时间   datetime,   消费类型   int,   消费金额   money)
insert   into   CostList   values   ( '2006-1-1 ',   1,   10)
insert   into   CostList   values   ( '2006-1-1 ',   2,   200)
insert   into   CostList   values   ( '2006-1-1 ',   1,   12)
insert   into   CostList   values   ( '2006-1-1 ',   3,   12)
insert   into   CostList   values   ( '2006-1-2 ',   1,   11)
insert   into   CostList   values   ( '2006-1-2 ',   3,   107)
insert   into   CostList   values   ( '2006-1-3 ',   1,   17)
insert   into   CostList   values   ( '2006-1-3 ',   3,   17)
insert   into   CostList   values   ( '2006-1-4 ',   1,   10)
GO  

(请调试通过并结果正确,请注意语句的性能)
===================================================  

第2题:  



已知父子关系记录存在于一张表中:
__________________________________________
id       |       Parent_id       |       Name
------------------------------------------
1         |       -1                     |       张三
------------------------------------------
2         |       1                       |       里斯
------------------------------------------
3         |       1                       |       王武
------------------------------------------
4         |       2                       |       赵六
------------------------------------------
5         |       3                       |       牛七
------------------------------------------
.......  

用一个存储过程,计算出某用户下级共有多少子记录(下级的下级...所有子级)  

P_GetUsersScore   @User_id   bigint,   @Score   int   output
AS
.....
.....
set   @Score   =   xxxx
GO  

注:级别层数可能会超过100层,注意性能。  

=========================================================================
The   End.
=========================================================================
   


[解决办法]
呵呵,不是吧,好没有给分.
难道我的解答有问题么?
哈哈,MSSQL里面用递归没用过吧
建立测试环境
CREATE TABLE [tb] (
[id] [int] NULL ,
[Parent_id] [int] NULL ,
[username] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL
)
插入数据
insert into tb
select 1,-1, ' '
union
select 2,1, ' '
union
select 3,1, ' '
union
select 4,2, ' '
union
select 5,2, ' '
union
select 6,4, ' '
union
select 7,3, ' '
union
select 8,3, ' '
创建函数
create function P_GetUsersScore(@User_id bigint) returns int
as
begin
if not exists (select * from tb where id=@User_id)
return 0
if not exists (select * from tb where Parent_id=@User_id)
return 0
--以上首先检测免得浪费时间
declare @rt bigint
declare @tmpid int
set @rt=0
declare cur cursor for select id from tb where Parent_id=@User_id
open cur
fetch next from cur into @tmpid
while @@fetch_status=0
begin
set @rt=@rt+dbo.P_GetUsersScore(@tmpid)+1
--每个字节点分别查询字节点数
--加一是因为他本身就是一子记录
fetch next from cur into @tmpid
end
close cur
return @rt
end
测试结果
select id,dbo.P_GetUsersScore(id) as Score from tb
ID Score
17
23
32
41
50
60
70
80

楼上的还有疑问么?
这样不给分怎么以后混呀!

热点排行