开发者

return count of true/false in a column of a sql table in c#?

开发者 https://www.devze.com 2023-02-05 09:14 出处:网络
How to return count of true/ false in a column of a table. I have a table in sql containing 5 columns with bollean type. Now I have to return the number of trues and falses in each column and displ开发

How to return count of true/ false in a column of a table. I have a table in sql containing 5 columns with bollean type. Now I have to return the number of trues and falses in each column and displ开发者_如何学JAVAay on my frontend. Can somebody guide me to write the query?

Thanks


without more info, your query would be something like

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

If you wanted grouped by some criteria like a category, date, etc...

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

精彩评论

暂无评论...
验证码 换一张
取 消