开发者

Help with SQL Query Count

I have a table similar to the following

ID NUMBER
VAL1 NUMBER
VAL2 NUMBER

I need an SQL query that displays count of rows that have VAL1 开发者_如何学Python> VAL2 and Count of rows that have VAL1 < VAL2. Both counts using one SQL query. Thanks.


This query should work with most database platforms:

 select sum(case when val1 > val2 then 1 end) as GreaterThanCount,
    sum(case when val1 < val2 then 1 end) as LessThanCount
from MyTable

To show the sums in separate rows, you can do:

select case when val1 > val2 then 'GreaterThan' else 'LessThan' end as Type,
    count(*) as Count
from MyTable    
group by case when val1 > val2 then 'GreaterThan' else 'LessThan' end


SELECT SUM(VAL1 > VAL2) as arg1, SUM(VAL1 < VAL2) as arg2


Try this:

select id, count(VAL1 > VAL2) as more1, count(val2 > val1) as more2
from your_table
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜