为sql查询搜索更好的解决方案

时间:2018-04-18 10:10:49

标签: sql postgresql

我有下表:

public void test() {
    List<Integer> integers = Arrays.asList(1, 4, 1234, 5, 6, 77);
    int secondBiggest = second(integers, -1, 0, 0);
    System.out.println(secondBiggest);
}


public Integer second(List<Integer> a, Integer biggest, Integer secondBiggest, Integer length) {
    if (a.size() > length) {
        Integer cur = a.get(length);
        length++;
        if (cur > biggest) {
            secondBiggest = biggest;
            biggest = cur;
        } else if (cur < biggest && cur > secondBiggest) {
            secondBiggest = cur;
        }
        secondBiggest = second(a, biggest, secondBiggest, length);
    }
    return secondBiggest;
}

列4 5 6和7引用类型(type1,type2,type3,type4)

所以如果col4为true我必须将type1分配给元组(col1,col2,col3),但是一个元组可以有多个类型,所以使用上面的表格结果将是这样的

| col1 (varchar) | col2(varchar) | col3(varchar) | col4(bool) | col5(bool) | col6(bool) | col7(bool) |
|----------------|---------------|---------------|------------|------------|------------|------------|
| a              | a             | a             | true       | true       | false      | false      |
| b              | b             | b             | true       | true       | true       | true       |
| c              | c             | c             | false      | false      | true       | false      |

我可以使用4个查询并使用 CASE WHEN

| col1 (varchar) | col2(varchar) | col3(varchar) | type    |
|----------------|---------------|---------------|---------|
| a              | a             | a             | "type1" |
| a              | a             | a             | "type2" |
| b              | b             | b             | "type1" |
| b              | b             | b             | "type2" |
| b              | b             | b             | "type3" |
| b              | b             | b             | "type4" |
| c              | c             | c             | "type3" |

并将结果连接到一个表中,但这是一个昂贵的解决方案(我必须编写所有查询)。

  

有更好的解决方案吗?

2 个答案:

答案 0 :(得分:3)

从布尔列创建一个数组,并且不要跳过空值:

select col1, col2, col3, type
from (
    select 
        col1, col2, col3, unnest(array[
            case when col4 then 'type1' end,
            case when col5 then 'type2' end,
            case when col6 then 'type3' end,
            case when col7 then 'type4' end]) as type
    from my_table
    ) s
where type is not null;

 col1 | col2 | col3 | type  
------+------+------+-------
 a    | a    | a    | type1
 a    | a    | a    | type2
 b    | b    | b    | type1
 b    | b    | b    | type2
 b    | b    | b    | type3
 b    | b    | b    | type4
 c    | c    | c    | type3
(7 rows)

请参阅SqlFiddle.

中的在线演示

没有数组的解决方案:

select *
from (
    select col1, col2, col3, case when col4 then 'type1' end as type from my_table
    union all
    select col1, col2, col3, case when col5 then 'type2' end as type from my_table
    union all
    select col1, col2, col3, case when col6 then 'type3' end as type from my_table
    union all
    select col1, col2, col3, case when col7 then 'type4' end as type from my_table
    ) s
where type is not null
order by col1, col2, col3, type

答案 1 :(得分:0)

您可以使用一个case表达式执行此操作:

select col1, col2, col3,
       (CASE WHEN col4 THEN 'type1' 
             WHEN col5 THEN 'type2' 
             WHEN col6 THEN 'type3' 
             WHEN col7 THEN 'type4' 
        END) as type
from ...
where ...;

请注意使用单引号。这些是SQL中的标准字符串分隔符。