sql查询基于分隔符进行拆分并与其他人进行比较

时间:2016-08-03 10:47:37

标签: postgresql

我有一张表test_data

Studentid       type       value
    1           height     class:sixth|heightInCm:135
    2           height     class:sixth|heightInCm:127
    3           height     class:seventh|heightInCm:142
    4           height     class:seventh|heightInCm:100
    5           weight     class:sixth|WeightInKg:26
    6           weight     class:sixth|WeightInKg:32
    7           weight     class:seventh|WeightInKg:38
    8           weight     class:seventh|WeightInKg:40

我有另一个名为Rule_table的表格为:

type    operator    value       score
height  AND(=,>=)   sixth|125     1
height  AND(=,>=)   seventh|135   1
weight  AND(=,>=)   sixth|35      1
weight  AND(=,>=)   seventh|40    1

height AND(=,>=) sixth|125 1中此行rule_table的含义是:if class = sixth AND heightInCm >= 125则得分为1,否则为0。像这样可以理解所有行。因此在运算符列中,它是带有And的运算符的顺序,并且将使用该值进行检查。

将此rule_table应用于test_data表后,我们可以得到如下表格:

Studentid       type       value                            score
    1           height     class:sixth|heightInCm:135        1
    2           height     class:sixth|heightInCm:127        1
    3           height     class:seventh|heightInCm:142      1 
    4           height     class:seventh|heightInCm:100      0
    5           weight     class:sixth|WeightInKg:36         1 
    6           weight     class:sixth|WeightInKg:32         0
    7           weight     class:seventh|WeightInKg:38       0
    8           weight     class:seventh|WeightInKg:40       1

我正在努力编写查询。如果它是单值和单个操作符,那么我会轻松地完成它。但对于多价值和多运营商来说,这对我来说很难。

任何人都可以帮助我。我正在使用PostgreSQL。

0 个答案:

没有答案