将2个select语句与不同的where子句sqlserver结合使用

时间:2016-08-18 07:59:13

标签: sql sql-server sql-server-2012

我希望结合2选择与不同的where子句 首先选择是

push

结果是:

SELECT name,
   COUNT(telat_1_30min)
FROM kkpsurabaya
WHERE telat_1_30min >= 1
AND telat_1_30min <= 30;

和其他选择是

name         | telat1 1-30 min
FERI WAHYUDI | 2

结果是:

SELECT COUNT(late) AS 'telat 31-60min'
FROM kkpsurabaya
WHERE timetable NOT LIKE 'minggu'
AND timetable NOT LIKE 'sabtu'
AND late <= 90
AND late >= 61;

我想要像

那样结合
name         | telat1 31-60 min
KARIYONO     | 1

我尝试使用查询

name         | telat1 1-30 min|telat1 31-60 min
FERI WAHYUDI | 2              |0
KARIYONO     | 0              |1

结果是:

SELECT name,
       'telat < 30menit' = (SELECT
         COUNT(telat_1_30min) AS 'telat 1-30min'
       FROM kkpsurabaya
       WHERE telat_1_30min >= 1
       AND telat_1_30min <= 30),
       'telat 31-60 min' = (SELECT
         COUNT(late) AS telat_60_90
       FROM kkpsurabaya
       WHERE timetable NOT LIKE 'minggu'
       AND timetable NOT LIKE 'sabtu'
       AND late <= 90
       AND late >= 61)
FROM kkpsurabaya
GROUP BY Name;

telat_1_30min类型数据为int

晚期类型数据是int

解决

name | telat1 1-30 min|telat1 31-60 min FERI WAHYUDI | 2 |1 KARIYONO | 2 |1

1 个答案:

答案 0 :(得分:2)

[UIUtils registerLocalNotification:describe:completed:]

修改

    select  name, count( telat_1_30min)  as ' telat1 1-30 min',0 as 'telat1 31-60 min'
    from kkpsurabaya where  telat_1_30min >=1 and telat_1_30min <=30 
    union all
    SELECT name,0 as  ' telat1 1-30 min', count(late) as 'telat 31-60min' 
    FROM kkpsurabaya WHERE timetable not like 'minggu' and timetable not like 'sabtu' and late <=90 and late >=61