在sql中将查询结果添加为新列

时间:2017-02-27 20:25:04

标签: mysql sql

我想将查询结果显示为新列,这是我的代码,但它显示在2个表中。

@Configuration
public class ElasticsearchConfiguration {

@Bean
public Client client() throws UnknownHostException {
    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

    return client;
}

@Bean
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
    return new ElasticsearchTemplate(client());
}
}

Current result

我的代码应该是如何在这样的1个表中使用

desired result

1 个答案:

答案 0 :(得分:0)

我认为JobNo是UNIQUE。如果是这样,你可以简单地使用:

SELECT JobNo
      ,Prepared1
      ,Prepared2
      ,CASE WHEN Prepared1 > 1  THEN 1 ELSE 0 END
       + CASE WHEN Prepared2 >1 THEN 1 ELSE 0 END 
  FROM [test] t1

如果不是:

select JobNo,
    Prepared1,
    Prepared2,
    t2.done
from [test] t1
join (
    select JobNo,
        COUNT(case 
                when Prepared1 > 1
                    then 1
                end) + COUNT(case 
                when Prepared2 > 1
                    then 1
                end) as done
    from [test] t2
    group by JobNo
    ) t2 on t1.JobNo = t2.JobNo;