问题描述
- sql server count函数问题
-
代码:select jl.ORGAN_NAME as '油站名称',jl.KPLX as '开票类型',jl.SFCH as '冲红数量',sum(jl.KPJE) as '开票金额' from DZFP_KPJL jl where jl.ORGAN_NAME = '组织机构2' and jl.KPRQ between '2016-03-22' and '2016-03-24' group by jl.ORGAN_NAME,jl.KPLX
查询结果:
如图所示,要把第三列值是1的所有数据用count函数统计数量,如果值是0,则不统计,求解答,急急急急急----------------------------
解决方案
最简单的实现方式,再包一层,写个子查询。
SELECT
count(1)
FROM
(
SELECT
jl.ORGAN_NAME AS '油站名称',
jl.KPLX AS '开票类型',
jl.SFCH AS '冲红数量',
sum(jl.KPJE) AS '开票金额'
FROM
DZFP_KPJL jl
WHERE
jl.ORGAN_NAME = '组织机构2'
AND jl.KPRQ BETWEEN '2016-03-22'
AND '2016-03-24'
GROUP BY
jl.ORGAN_NAME,
jl.KPLX
) t
WHERE
t.开票金额
= 1
解决方案二:
Count函数能让我们知道表格被选出来的数据的个数
它的语法:
SELECT COUNT("栏位名")?
FROM "表格名"
以tb_Teachers_info表为例:
tb_Teachers_info
如果我们想得到表中教师的人数
select?? COUNT(TeachersName)?
'count' ?? from? tb_Teachers_info;
结果:......
答案就在这里:十五. Sql server count函数