1 入球
2 点球
3 乌龙球
4 黄牌
5 红牌--上面的是进球类型
-------------------------------------------------------------------------------------
--下面的是一个表数据1 1002776 408 4 7 柏斯爾 40290 ''
2 1002776 408 1 13 柏斯爾 40290 1-0
3 1002776 103 1 62 Lambert 24594 1-1
4 1002776 103 2 83 費格遜 24595 1-2
5 1002776 103 3 87 費格遜 24595 2-2
解释下表数据字段意思:
第一列是:ID
第二列式:赛事ID
第三列是:球队ID
第四列是:进球类型ID
第五列是:进球时间
第六列,七列:进球人名字和ID第八列 就是我要提的问题:
最后那一列是我列出来的 什么意思了?
解释下,在第7分钟的时候 主队吃了一张黄牌 所以在这个阶段场上的比分情况是0-0 ,但是要显示为'' .红牌也一样
第2行的 “1-0” 是表示 主队在13分钟的时候进了一球 在这一阶段 场上比分是 1-0
依次类推
如果是乌龙球,比喻说是 第5行 客队进了乌龙球 那么这球算是对方的 及在那一阶段场上比分是 “2-2”最后一列是本来不存在的 现在是要在查询的时候 在查询结果中加上这一列
小弟sql文很烂
望高手帮忙,谢谢...
-- Author: HappyFlyStone
-- Version:V1.001
-- Date:2009-07-09 00:18:41
-------------------------------------- Test Data: ta
If object_id('ta') is not null
Drop table ta
Create table ta(col1 int,col2 int,col3 int,col4 int,col5 int,col6 nvarchar(7),col7 int,col8 nvarchar(3))
go
Insert into ta
select 1,1002776,408,4,7,'柏斯爾',40290,'' union all
select 2,1002776,408,1,13,'柏斯爾',40290,'' union all
select 3,1002776,103,1,62,'Lambert',24594,'' union all
select 4,1002776,103,2,83,'費格遜',24595,'' union all
select 5,1002776,103,3,87,'費格遜',24595,''
Go
--Start
declare @i int,@j int
set @I = 0
set @j = 0
update a
set col8 = case when col4>=4 then '''''' else
ltrim(@i) +'-'+ltrim(@j) end,
@i = case when (col3 = 408 and col4 < 3) or (col3 != 408 and col4 = 3) then @i + 1 else @I end,
@j = case when (col3 != 408 and col4 < 3) or (col3 = 408 and col4 = 3) then @j + 1 else @j end
from ta a
Select * from ta
--Result:
/*col1 col2 col3 col4 col5 col6 col7 col8
----------- ----------- ----------- ----------- ----------- ------- ----------- ----
1 1002776 408 4 7 柏斯爾 40290 ''
2 1002776 408 1 13 柏斯爾 40290 1-0
3 1002776 103 1 62 Lambert 24594 1-1
4 1002776 103 2 83 費格遜 24595 1-2
5 1002776 103 3 87 費格遜 24595 2-2(5 行受影响)
*/
--End
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================IF OBJECT_ID('tb1') IS NOT NULL
DROP TABLE tb1
GO
CREATE TABLE tb1(进球id int, 进球方式 varchar(100))
go
insert into tb1
select 1,'入球' union all
select 2,'点球' union all
select 3,'乌龙球' union all
select 4,'黄牌' union all
select 5,'红牌'
go
IF OBJECT_ID('tb2') IS NOT NULL
DROP TABLE tb2
GO
CREATE TABLE tb2(id int, 赛事ID varchar(100),球队ID int,进球类型id int,进球时间 int, 进球人名字 varchar(100),进球人id int)
go
insert into tb2
select
1 , '1002776' , 408 , 4 , 7 , '柏斯爾' , 40290 union all select
2 , '1002776' , 408 , 1 , 13 , '柏斯爾' , 40290 union all select
3 , '1002776' , 103 , 1 , 62 , 'Lamber' , 24594 union all select
4 , '1002776' , 103 , 2 , 83 , '費格遜' , 24595 union all select
5 ,'1002776' , 103 , 3 , 87 , '費格遜' , 24595
go
alter table tb2 add 比分 varchar(10)
go
declare @主队分数 int,@客队分数 int
set @主队分数 = 0
set @客队分数 = 0
update a
set 比分= case when @主队分数 = 0 and @客队分数 = 0 then '''''' else
convert ( varchar ,@主队分数) +'-'+convert ( varchar ,@客队分数 )end,
@主队分数 = case when (a.球队ID = 408 and 进球方式 in('入球','点球')) or (a.球队ID <> 408 and 进球方式 = '乌龙球') then @主队分数 + 1 else @主队分数 end,
@客队分数 = case when (a.球队ID <>408 and 进球方式 in('入球','点球')) or (a.球队ID = 408 and 进球方式 = '乌龙球') then @客队分数 + 1 else @客队分数 end
from tb1,tb2 a
where tb1.进球id=a.进球类型id
go
select * from tb2/*------------
1 1002776 408 4 7 柏斯爾 40290 ''
2 1002776 408 1 13 柏斯爾 40290 1-0
3 1002776 103 1 62 Lamber 24594 1-1
4 1002776 103 2 83 費格遜 24595 1-2
5 1002776 103 3 87 費格遜 24595 2-2
-------*/
set col8 = case when col4>=4 then '''''' else 这句不太对 应该比分0比0的时候’‘
出现红黄牌时候 比分应该不变
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================IF OBJECT_ID('tb1') IS NOT NULL
DROP TABLE tb1
GO
CREATE TABLE tb1(进球id int, 进球方式 varchar(100))
go
insert into tb1
select 1,'入球' union all
select 2,'点球' union all
select 3,'乌龙球' union all
select 4,'黄牌' union all
select 5,'红牌'
go
IF OBJECT_ID('tb2') IS NOT NULL
DROP TABLE tb2
GO
CREATE TABLE tb2(id int, 赛事ID varchar(100),球队ID int,进球类型id int,进球时间 int, 进球人名字 varchar(100),进球人id int)
go
insert into tb2
select
1 , '1002776' , 408 , 4 , 7 , '柏斯爾' , 40290 union all select
2 , '1002776' , 408 , 1 , 13 , '柏斯爾' , 40290 union all select
3 , '1002776' , 103 , 1 , 62 , 'Lamber' , 24594 union all select
4 , '1002776' , 103 , 2 , 83 , '費格遜' , 24595 union all select
5 ,'1002776' , 103 , 3 , 87 , '費格遜' , 24595
go
alter table tb2 add 比分 varchar(10)
go
declare @主队分数 int,@客队分数 int
set @主队分数 = 0
set @客队分数 = 0
update a
set 比分= case when 进球方式 in('黄牌,'红牌') then '''''' else
convert ( varchar ,@主队分数) +'-'+convert ( varchar ,@客队分数 )end,
@主队分数 = case when (a.球队ID = 408 and 进球方式 in('入球','点球')) or (a.球队ID <> 408 and 进球方式 = '乌龙球') then @主队分数 + 1 else @主队分数 end,
@客队分数 = case when (a.球队ID <>408 and 进球方式 in('入球','点球')) or (a.球队ID = 408 and 进球方式 = '乌龙球') then @客队分数 + 1 else @客队分数 end
from tb1,tb2 a
where tb1.进球id=a.进球类型id
go
select * from tb2
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================IF OBJECT_ID('tb1') IS NOT NULL
DROP TABLE tb1
GO
CREATE TABLE tb1(进球id int, 进球方式 varchar(100))
go
insert into tb1
select 1,'入球' union all
select 2,'点球' union all
select 3,'乌龙球' union all
select 4,'黄牌' union all
select 5,'红牌'
go
IF OBJECT_ID('tb2') IS NOT NULL
DROP TABLE tb2
GO
CREATE TABLE tb2(id int, 赛事ID varchar(100),球队ID int,进球类型id int,进球时间 int, 进球人名字 varchar(100),进球人id int)
go
insert into tb2
select
1 , '1002776' , 408 , 4 , 7 , '柏斯爾' , 40290 union all select
2 , '1002776' , 408 , 1 , 13 , '柏斯爾' , 40290 union all select
3 , '1002776' , 103 , 1 , 62 , 'Lamber' , 24594 union all select
4 , '1002776' , 103 , 2 , 83 , '費格遜' , 24595 union all select
5 ,'1002776' , 103 , 3 , 87 , '費格遜' , 24595 union all select
6 ,'1002776' ,103, 5 , 87 , '費格遜', 24595
go
alter table tb2 add 比分 varchar(10)
go
declare @主队分数 int,@客队分数 int
set @主队分数 = 0
set @客队分数 = 0
update a
set 比分= case when 进球方式 in('黄牌','红牌') then '''''' else
convert ( varchar ,@主队分数) +'-'+convert ( varchar ,@客队分数 )end,
@主队分数 = case when (a.球队ID = 408 and 进球方式 in('入球','点球')) or (a.球队ID <> 408 and 进球方式 = '乌龙球') then @主队分数 + 1 else @主队分数 end,
@客队分数 = case when (a.球队ID <>408 and 进球方式 in('入球','点球')) or (a.球队ID = 408 and 进球方式 = '乌龙球') then @客队分数 + 1 else @客队分数 end
from tb1,tb2 a
where tb1.进球id=a.进球类型id
go
select * from tb2
/*
1 1002776 408 4 7 柏斯爾 40290 ''
2 1002776 408 1 13 柏斯爾 40290 1-0
3 1002776 103 1 62 Lamber 24594 1-1
4 1002776 103 2 83 費格遜 24595 1-2
5 1002776 103 3 87 費格遜 24595 2-2
6 1002776 103 5 87 費格遜 24595 ''
*/
既然楼主这么要求
程序 如下
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================IF OBJECT_ID('tb1') IS NOT NULL
DROP TABLE tb1
GO
CREATE TABLE tb1(进球id int, 进球方式 varchar(100))
go
insert into tb1
select 1,'入球' union all
select 2,'点球' union all
select 3,'乌龙球' union all
select 4,'黄牌' union all
select 5,'红牌'
go
IF OBJECT_ID('tb2') IS NOT NULL
DROP TABLE tb2
GO
CREATE TABLE tb2(id int, 赛事ID varchar(100),球队ID int,进球类型id int,进球时间 int, 进球人名字 varchar(100),进球人id int)
go
insert into tb2
select
1 , '1002776' , 408 , 4 , 7 , '柏斯爾' , 40290 union all select
2 , '1002776' , 408 , 1 , 13 , '柏斯爾' , 40290 union all select
3 , '1002776' , 103 , 1 , 62 , 'Lamber' , 24594 union all select
4 , '1002776' , 103 , 2 , 83 , '費格遜' , 24595 union all select
5 ,'1002776' , 103 , 3 , 87 , '費格遜' , 24595 union all select
6 ,'1002776' ,103, 5 , 87 , '費格遜', 24595
go
alter table tb2 add 比分 varchar(10)
go
declare @主队分数 int,@客队分数 int
set @主队分数 = 0
set @客队分数 = 0
update a
set 比分= case when 进球方式 in('黄牌','红牌') then '''''' else
convert ( varchar ,@主队分数) +'-'+convert ( varchar ,@客队分数 )end,
@主队分数 = case when (a.球队ID = 408 and 进球方式 in('入球','点球')) or (a.球队ID <> 408 and 进球方式 = '乌龙球') then @主队分数 + 1 else @主队分数 end,
@客队分数 = case when (a.球队ID <>408 and 进球方式 in('入球','点球')) or (a.球队ID = 408 and 进球方式 = '乌龙球') then @客队分数 + 1 else @客队分数 end
from tb1,tb2 a
where tb1.进球id=a.进球类型id
go
select * from tb2
/*
1 1002776 408 4 7 柏斯爾 40290 ''
2 1002776 408 1 13 柏斯爾 40290 1-0
3 1002776 103 1 62 Lamber 24594 1-1
4 1002776 103 2 83 費格遜 24595 1-2
5 1002776 103 3 87 費格遜 24595 2-2
6 1002776 103 5 87 費格遜 24595 ''
*/
go
Insert into #ta
select 1,1002776,408,4,7,'柏斯爾',40290,'' union all
select 2,1002776,408,1,13,'柏斯爾',40290,'' union all
select 3,1002776,103,1,62,'Lambert',24594,'' union all
select 4,1002776,103,2,83,'費格遜',24595,'' union all
select 5,1002776,103,3,87,'費格遜',24595,''
Godeclare @tm varchar(10)
set @tm=(select top 1 col3 from #ta order by col1)select @tmselect * ,
cast ((select sum(case when (col3=@tm and col4<3) or (col3<>@tm and col4=3 ) then 1 else 0 end ) from #ta where col1<=a.col1) as varchar(10))+'-'+
cast((select sum(case when (col3<>@tm and col4<3) or (col3=@tm and col4=3 ) then 1 else 0 end ) from #ta where col1<=a.col1) as varchar(10))
from #ta a
drop table #ta
--Result:
/*col1 col2 col3 col4 col5 col6 col7 col8
----------- ----------- ----------- ----------- ----------- ------- ----------- ----
1 1002776 408 4 7 柏斯爾 40290 ''
2 1002776 408 1 13 柏斯爾 40290 1-0
3 1002776 103 1 62 Lambert 24594 1-1
4 1002776 103 2 83 費格遜 24595 1-2
5 1002776 103 3 87 費格遜 24595 2-2(5 行受影响)
*/
--End
麦迪马上升星,支持你
(select convert(char(1),count(*)) from tg t where (t.col3=408 and t.col4 in (1,2) and t.col5<=tg.col5) or (t.col3=103 and t.col4=3 and t.col5=tg.col5) ) + '-' +
(select convert(char(1),count(*)) from tg t where (t.col3=103 and t.col4 in (1,2) and t.col5<=tg.col5) or (t.col3=408 and t.col4=3 and t.col5<=tg.col5))
from tg