创建一张表,并且想表中插入中文和时间
CREATETABLE[T_Scores](
[Date][datetime]NULL,
[Name][nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Score][nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
);
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF200000000ASDateTime), N'拜仁', N'胜');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF300000000ASDateTime), N'奇才', N'胜');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF300000000ASDateTime), N'湖人', N'胜');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF400000000ASDateTime), N'拜仁', N'负');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF200000000ASDateTime), N'拜仁', N'负');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF600000000ASDateTime), N'奇才', N'胜');
[Date][datetime]NULL,
[Name][nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Score][nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
);
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF200000000ASDateTime), N'拜仁', N'胜');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF300000000ASDateTime), N'奇才', N'胜');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF300000000ASDateTime), N'湖人', N'胜');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF400000000ASDateTime), N'拜仁', N'负');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF200000000ASDateTime), N'拜仁', N'负');
INSERT[T_Scores] ([Date], [Name], [Score]) VALUES (CAST(0x00009AF600000000ASDateTime), N'奇才', N'胜');
查询结果如图:
现在想实现如图的现实效果:
首先先建立一个返回分手的函数
createfunction num
(@namenvarchar(50),@winnvarchar(50))
returnsint
as
begin
declare@numint
select@num=count(Score) from T_Scores where name=@nameand Score=@win
return@num
end
(@namenvarchar(50),@winnvarchar(50))
returnsint
as
begin
declare@numint
select@num=count(Score) from T_Scores where name=@nameand Score=@win
return@num
end
再在Sql查询中调用
selectdistinct(name),dbo.num(name,'胜')as'胜',dbo.num(name,'负')as'负'from T_Scores
显示效果如图: