在c#中的sql表的列中返回true / false的计数?

时间:2011-01-18 02:07:03

标签: asp.net

如何在表的列中返回true / false的计数。 我在sql中有一个包含5列bollean类型的表。现在我必须返回每列中的真实和谬误的数量,并显示在我的前端。 有人可以指导我写这个查询吗?

谢谢

1 个答案:

答案 0 :(得分:-1)

没有更多信息,您的查询将类似于

select
      sum( if( FirstLogicalColumn, 1, 0 ) ) FirstCount,
      sum( if( SecondLogicalColumn, 1, 0 ) ) SecondCount,
      sum( if( ThirdLogicalColumn, 1, 0 ) ) ThirdCount,
      sum( if( FourthLogicalColumn, 1, 0 ) ) FourthCount,
      sum( if( FifthLogicalColumn, 1, 0 ) ) FifthCount
   from
      YourTable

if SQL-Server

select
      sum( case when FirstLogicalColumn then 1 else 0 end ) FirstCount,
      sum( case when SecondLogicalColumn then 1 else 0 end ) SecondCount,
      sum( case when ThirdLogicalColumn then 1 else 0 end ) ThirdCount,
      sum( case when FourthLogicalColumn then 1 else 0 end ) FourthCount,
      sum( case when FifthLogicalColumn then 1 else 0 end ) FifthCount
   from
      YourTable

如果您希望按类别,日期等某些条件进行分组......

select
      YourGroupColumn,
      sum( other columns per abve )
   from
      YourTable
   group by 
      YourGroupColumn