若是支持SQL92,可以写成:
select count(*) as 总共帐单, sum(case 帐单是否已付 when true then 1 else 0 end) as 已付, sum(case 帐单是否已付 when false then 1 else 0 end) as 没付 from table where ....
select count(*) as 总共帐单, sum(case 帐单是否已付 when true then 1 else 0 end) as 已付, sum(case 帐单是否已付 when false then 1 else 0 end) as 没付 from table where ....
已付=SUM(CASE WHEN 帐单是否已付='True' THEN 1 ELSE 0 END),
没付=SUM(CASE WHEN 帐单是否已付='False' THEN 1 ELSE 0 END)
FROM yourtable
-- WHERE 用户名='王一' /*加上Where条件可以具体到某个用户或日期*/
但要注意你用的SQL是否支持
如果不支持你可以稍加改动!
begin query1.Close;
query1.SQL.Clear;
query1.SQL.Add('select count(*) as 总共帐单, sum(case bl_HavePaid when true then 1 else 0 end) as 已付, sum(case bl_HavePaid when false then 1 else 0 end) as 没付 from 表);
query1.open;
end;
运行时的确有错误呀,请问‘SQL是否支持’怎么判断呀!
可以获得已付和未付的总数,两个相加不就是总数了么?
如果要求不是很严格的话,有很多灵活方法的
能说的详细点吗!
已付=count(b.*)
没付=count(c.*)
FROM
(select * from Table 用户名='王一') as a
(select * from Table where 帐单是否已付='True' and 用户名='王一') as b
(select * from Table where 帐单是否已付='false' and 用户名='王一' )as c
写得太水了,
又*又长,
呵呵
别这么说,你比我强多了呀,我连思路都没有。
to: bobfang(匆匆过客)
我用的是SQL Server 数据库,问题果然是你说的那样,把true ,false改成1,0就可以了。不过我有点不太明白原因呀!应为表里写的是1,0但我用delphi的DBgrid显示的时候是 true ,false,是不是要跟表里的数据一致的缘故呀!问题解决了结贴!