SQL Server:计算Select中的两列是否匹配
我有一个存储过程,它使用以下行从表中的两列获取数据 除了下面的内容外,是否有一种方法可以比较两列并以百分比计算 我要找的是一个数字,它表示(以%)所选记录中有多少与col1和col2中的数据相匹配,例如说“60%”SQL Server:计算Select中的两列是否匹配,sql,sql-server,select,count,Sql,Sql Server,Select,Count,我有一个存储过程,它使用以下行从表中的两列获取数据 除了下面的内容外,是否有一种方法可以比较两列并以百分比计算 我要找的是一个数字,它表示(以%)所选记录中有多少与col1和col2中的数据相匹配,例如说“60%” 非常感谢你在这方面的帮助,蒂姆 这可能包含错误,但尚未测试 declare @i int; set @i = 0; declare @t table ( int id identity, varchar(max) col1, varchar(max) col2 ) insert i
非常感谢你在这方面的帮助,蒂姆 这可能包含错误,但尚未测试
declare @i int;
set @i = 0;
declare @t table
(
int id identity,
varchar(max) col1,
varchar(max) col2
)
insert into @t(col1,col2)
select col1,
col2 from FROM LogTable
FOR XML PATH('archive'), ELEMENTS, TYPE, ROOT('ranks');
set i = 0;
declare @sum int
set @sum = 0;
while(i < select count(*) from @t)
begin
select case when (col1 == col2) then @sum = @sum+1 else @sum from @t
where id = i;
i++;
end
print @sum / select count(*) from @t
声明@i int;
设置@i=0;
声明@t表
(
int id标识,
varchar(max)col1,
varchar(max)col2
)
插入@t(col1,col2)
选择col1,
日志表中的col2
对于XML路径('archive')、元素、类型、根('ranks');
设置i=0;
声明@sum int
设置@sum=0;
而(i
或者在这两行之间,没有SQL Management studio来测试atm。您的意思是,对于每一行,确定
col1=col2
,然后确定整个事件的百分比?谢谢。不,我是说总共。谢谢。我会检查一下,然后回复。
declare @i int;
set @i = 0;
declare @t table
(
int id identity,
varchar(max) col1,
varchar(max) col2
)
insert into @t(col1,col2)
select col1,
col2 from FROM LogTable
FOR XML PATH('archive'), ELEMENTS, TYPE, ROOT('ranks');
set i = 0;
declare @sum int
set @sum = 0;
while(i < select count(*) from @t)
begin
select case when (col1 == col2) then @sum = @sum+1 else @sum from @t
where id = i;
i++;
end
print @sum / select count(*) from @t