分多,人傻,速来!

phoeni_xin 2010-01-22 11:48:50
Table Serie
SeriesID start end Series
1 1 200 av
2 201 500 bv

Table Type
ID SeriesID start End
AV1 1 1 20
AV2 1 25 65
AV3 1 70 100
BV1 2 203 206

Table tempRecord
ID series type start end
1 1 av1 1 5
2 1 av1 7 9
3 1 av1 11 15
4 1 av2 26 29
5 1 av2 33 42


现有几个问题。
1.如何在tempRecord里新增记录,如type为av1的话。start-end区间满足type表中id为av1的start-end区间。上SQL语句。谢~
比如。现在新增。可以新增16-20,但15-20不合法。
2.如何在查出,Serie表与Type表。Serie 中有Series = av下的start-end区间内有多少未使用,有多少已经使用(使用的是在type表中使用的)。应查出21-24,66-69,101-200。
3.如何统计出Serie及Type表中,start-end区间内,有多少在tempRecord 表start-end区间内使用了,多少未使用。返回使用的百分比,未使用的百分比。

各位高手如觉得分少。可再开帖散分。
只要解决问题。谢~
...全文
272 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
phoeni_xin 2010-01-25
  • 打赏
  • 举报
回复
顶下。周末也没人回帖的。。
andysun88 2010-01-25
  • 打赏
  • 举报
回复
sf
LIU312591 2010-01-25
  • 打赏
  • 举报
回复
就是
shuifengcun 2010-01-25
  • 打赏
  • 举报
回复
捡分来
zhousq00 2010-01-25
  • 打赏
  • 举报
回复
lai le
phoeni_xin 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 phoeni_xin 的回复:]
引用 18 楼 nianran520 的回复:
。。。临时表SQL code--3select t.SeriesID,ltrim(cast(r.number*1.0/(t.[end]-t.start+1)*100asdec(9,2)))+'%'as[已使用],ltrim(cast((1-r.number*1.0/(t.[end]-t.start+1))*100asdec(9,2)))+'%'as[未使用]from Serie tjoin
(select f.SeriesID,count(1)asnumberfrom Type f,master..spt_values hwhere h.type='P'and h.numberbetween f.startand f.[end]groupby f.SeriesID) ron r.SeriesID= t.SeriesID引用 13 楼 phoeni_xin 的回复:
引用 8 楼 nianran520 的回复:
SQL code--2  select id=identity(int,1,1),r.numberinto #tempfrom Serie t,master..spt_values rwhere t.Series='av'and r.type='P'and r.numberbetween t.startand t.[end]and r.numbernotin
(select h.numberfrom Type f,master..spt_values hwhere f.SeriesID= t.SeriesIDand h.type='P'and h.numberbetween f.startand f.[end])selectltrim(min(number))+'-'+ltrim(max(number))asnumberfrom #tempgroupbynumber- iddroptable #temp---------------------21-2466-69101-200


你这里的id用的是哪个表的id??

怎么查不出数据来?



临时表没问题了。

问下。这个能写成传参数的存储过程吗?

这个sql语句在程序端如何调用呢?
[/Quote]

另外。这个语句查不出101-200
这个段
zhangle861010 2010-01-25
  • 打赏
  • 举报
回复
..................
jc_liumangtu 2010-01-25
  • 打赏
  • 举报
回复
先顶再看
jiaqiang22 2010-01-25
  • 打赏
  • 举报
回复
晕菜,。。。。。
phoeni_xin 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 nianran520 的回复:]
。。。临时表SQL code--3select t.SeriesID,ltrim(cast(r.number*1.0/(t.[end]-t.start+1)*100asdec(9,2)))+'%'as[已使用],ltrim(cast((1-r.number*1.0/(t.[end]-t.start+1))*100asdec(9,2)))+'%'as[未使用]from Serie tjoin
(select f.SeriesID,count(1)asnumberfrom Type f,master..spt_values hwhere h.type='P'and h.numberbetween f.startand f.[end]groupby f.SeriesID) ron r.SeriesID= t.SeriesID引用 13 楼 phoeni_xin 的回复:
引用 8 楼 nianran520 的回复:
SQL code--2  select id=identity(int,1,1),r.numberinto #tempfrom Serie t,master..spt_values rwhere t.Series='av'and r.type='P'and r.numberbetween t.startand t.[end]and r.numbernotin
(select h.numberfrom Type f,master..spt_values hwhere f.SeriesID= t.SeriesIDand h.type='P'and h.numberbetween f.startand f.[end])selectltrim(min(number))+'-'+ltrim(max(number))asnumberfrom #tempgroupbynumber- iddroptable #temp---------------------21-2466-69101-200


你这里的id用的是哪个表的id??

怎么查不出数据来?

[/Quote]

临时表没问题了。

问下。这个能写成传参数的存储过程吗?

这个sql语句在程序端如何调用呢?
phoeni_xin 2010-01-25
  • 打赏
  • 举报
回复
顶啊。。。

各位砖家来回答下啊。
nianran520 2010-01-22
  • 打赏
  • 举报
回复
--改下,用事物有问题。。
--1
create trigger checking on tempRecord
for insert
as
begin
declare @type varchar(20),@start int,@end int
declare @checkstart int,@checkend int
declare @startmax int , @endmax int

select @type=type,@start=start,@end=[end] from inserted

select @checkstart=start,@checkend=[end] from [Type]
where id = @type

select @startmax = max(start),@endmax=max([end]) from tempRecord
where type = @type

if @start < @checkstart or @end > @checkend
begin
delete from tempRecord where id = (select id from inserted)
end
else if @start <= @endmax or @end < @start
begin
delete from tempRecord where id = (select id from inserted)
end
end
nianran520 2010-01-22
  • 打赏
  • 举报
回复
--1
create trigger checking on tempRecord
for insert
as
begin
begin tran checking
declare @type varchar(20),@start int,@end int
declare @checkstart int,@checkend int
declare @startmax int , @endmax int

select @type=type,@start=start,@end=[end] from inserted

select @checkstart=start,@checkend=[end] from [Type]
where id = @type

select @startmax = max(start),@endmax=max([end]) from tempRecord
where type = @type

if @start < @checkstart or @end > @checkend
begin
rollback tran checking
end
else if @start <= @endmax or @end < @start
begin
rollback tran checking
end
else
begin
commit tran checking
end


end
clxsl_huang 2010-01-22
  • 打赏
  • 举报
回复
mark
nianran520 2010-01-22
  • 打赏
  • 举报
回复

--2
select id=identity(int,1,1),r.number
into #temp
from Serie t,master..spt_values r
where t.Series = 'av' and r.type='P'
and r.number between t.start and t.[end]
and r.number not in
(select h.number
from Type f,master..spt_values h
where f.SeriesID = t.SeriesID and h.type='P'
and h.number between f.start and f.[end])

select
ltrim(min(number))+'-'+ltrim(max(number)) as number
from #temp
group by number - id

drop table #temp
---------------------
21-24
66-69
101-200

feixianxxx 2010-01-22
  • 打赏
  • 举报
回复
帮UP
yananguo_1985 2010-01-22
  • 打赏
  • 举报
回复
..
Mr_Nice 2010-01-22
  • 打赏
  • 举报
回复
。。。慢慢看了...
--小F-- 2010-01-22
  • 打赏
  • 举报
回复
分是很多 人却不傻
playwarcraft 2010-01-22
  • 打赏
  • 举报
回复
加载更多回复(9)

22,210

社区成员

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

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