合并SQL查询

时间:2017-04-12 01:56:36

标签: sql select

我想只在一个语句中创建此代码,但我需要在4个不同的列中。

SELECT a017tkahwin AS klaki FROM a017 WHERE a017tkahwin='K' AND a017kjantina='L'
SELECT a017tkahwin AS blaki FROM a017 WHERE a017tkahwin='B' AND a017kjantina='L'
SELECT a017tkahwin AS kpuan FROM a017 WHERE a017tkahwin='K' AND a017kjantina='P'
SELECT a017tkahwin AS bpuan FROM a017 WHERE a017tkahwin='B' AND a017kjantina='P'

2 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情:

fold

对于oracle DB,你最后需要从dual添加。

答案 1 :(得分:-1)

对于sql-server2012 +使用IIF,否则使用CASE

SELECT 
  iif(a017tkahwin = 'K' and a017kjantina = 'L',a017tkahwin,null)  AS klaki,
  iif(a017tkahwin = 'B' and a017kjantina = 'L',a017tkahwin,null)  AS blaki,
  iif(a017tkahwin = 'K' and a017kjantina = 'P',a017tkahwin,null)  AS kpuan,
  iif(a017tkahwin = 'B' and a017kjantina = 'P',a017tkahwin,null)  AS bpuan
 FROM a017 WHERE a017tkahwin IN ('K','B') AND a017kjantina in ('L','P')