例如以下情况,假如字段3是日期类型,按照小时分组统计字段1为空的个数,并对字段2大于5的值求和:
SELECT SUM(CASE WHEN field1 IS NULL THEN 1 ELSE 0 END) AS field1count,
SUM(CASE WHEN field2>5 then field2 else 0 end) AS field2sum
GROUP BY CONVERT(VARCHAR(13),field3,120)
例如以下情况,假如字段3是日期类型,按照小时分组统计字段1为空的个数,并对字段2大于5的值求和:
SELECT SUM(CASE WHEN field1 IS NULL THEN 1 ELSE 0 END) AS field1count,
SUM(CASE WHEN field2>5 then field2 else 0 end) AS field2sum
GROUP BY CONVERT(VARCHAR(13),field3,120)