+ -
当前位置:首页 → 问答吧 → TSQL挑战题 - 第五题 - 20110711

TSQL挑战题 - 第五题 - 20110711

时间:2011-07-11

来源:互联网

本帖最后由 rdcwayx 于 2011-07-11 14:33 编辑

这套TSQL挑战题其实是给SQL 管理员练手的, 我看着不错,只是做了简单的翻译,大家继续啊。

(你可以点上面的标签:TSQL挑战题, 可以找到所有的TSQL挑战题的帖子)

http://beyondrelational.com/blog ... ql-challenge-5.aspx

原文件:

1           post 1               sql,profiler,table
2           post 2               sql,performance
3           post 3               profilter
4           post 4               view,table

模板文件: 

1           sql,performance
2           profiler
3           table,performance,view

要求的输出文件:

1  sql,performance         post 2   sql,performance      2
1  sql,performance         post 1   sql,profiler,table   1
2  profiler                post 1   sql,profiler,table   1
3  table,performance,view  post 4   view,table           2
3  table,performance,view  post 1   sql,profiler,table   1
3  table,performance,view  post 2   sql,performance      1

解释规则:

就是找出匹配,如果匹配了,就给出匹配的post名和匹配数。

作者: rdcwayx   发布时间: 2011-07-11

规则没看懂,怎么算匹配,最后的匹配数怎么算出来的?

作者: ly5066113   发布时间: 2011-07-11

本帖最后由 yinyuemi 于 2011-07-11 14:55 编辑

gawk 4.0.0

array of array
  1. awk -F'[ ,]+' '{for(i=4;i<=NF;i++)a[$2" "$3][$i]=gensub(/^. +/,"",1,$0)}END{while (getline line <"file-B") {for(i in a) {x=0;for(j in a[i]) if(index(line,j)){x++};if(x>0)print line"\t"a[i][j]"\t"x}} }' file-A
  2. 1           sql,performance     post 1               sql,profiler,table 1
  3. 1           sql,performance     post 2               sql,performance    2
  4. 2           profiler    post 1               sql,profiler,table 1
  5. 3           table,performance,view      post 1               sql,profiler,table 1
  6. 3           table,performance,view      post 2               sql,performance    1
  7. 3           table,performance,view      post 4               view,table 2
复制代码

作者: yinyuemi   发布时间: 2011-07-11



QUOTE:
规则没看懂,怎么算匹配,最后的匹配数怎么算出来的?
ly5066113 发表于 2011-07-11 14:35




比如模板 1           sql,performance

在原文件中,sql,performance 出现在post1 和post2 ,只要有一个匹配都算。  sql,performance 在post2 中被匹配,就输出2, 但在post1 中只有一个匹配(sql),就输出1.

作者: rdcwayx   发布时间: 2011-07-11

0> perl 1.pl|sort
1       sql,performance post 1  sql,profiler,table      1
1       sql,performance post 2  sql,performance 2
2       profiler        post 1  sql,profiler,table      1
3       table,performance, view post 1  sql,profiler,table      1
3       table,performance, view post 2  sql,performance 1
3       table,performance, view post 4  view,table      2
1.pl
use strict;
use warnings;
my %HASH_TEM = (
        1 => "sql,performance",
        2 => "profiler",
        3 => "table,performance, view",
);
while ( my $line = <DATA> ) {
        chomp $line;
        my ( $post, $str ) = ( split( '\t', $line ) )[ 1, 2 ];
        foreach my $key ( keys %HASH_TEM ) {
                my $re = $HASH_TEM{$key};
                $re =~ s/\s+//g;
                $re =~ s/,/\|/g;
                if ( $str =~ m#$re# ) {
                        my $num = () = $str =~ m#$re#g;
                        print "$key\t$HASH_TEM{$key}\t$post\t$str\t", $num, "\n";
                }
        }
}
__DATA__
1       post 1  sql,profiler,table
2       post 2  sql,performance
3       post 3  profilter
4       post 4  view,table
没有排序

作者: magnet2008   发布时间: 2011-07-11

热门下载

更多