2

我之前问过这个问题,虽然得到了回答,但它确实没有解决我的问题,因为据我所知,CF QoQ 不支持这种CASE说法。

这是另一个问题:背景

那么有没有一种更简单的方法来完成我想要做的事情,而无需CASE在 sql 中使用语句,还是我将不得不回到我最初提出的联合风格的答案?

这就是我想出的:

<cfquery name="range1" dbtype="query">
    select count(col1) as cnt
    from tbl1
    where col1 <= 15000
</cfquery>
<cfquery name="range2" dbtype="query">
    select count(col1) as cnt
    from tbl1
    where col1 <= 30000
</cfquery>
<cfquery name="range3" dbtype="query">
    select count(col1) as cnt
    from tbl1
    where col1 <= 45000
</cfquery>
<cfquery name="range4" dbtype="query">
    select count(col1) as cnt
    from tbl1
    where col1 <= 60000
</cfquery>
<cfquery name="range5" dbtype="query">
    select count(col1) as cnt
    from tbl1
    where col1 <= 75000
</cfquery>
<cfquery name="range6" dbtype="query">
    select count(col1) as cnt
    from tbl1
    where col1 > 75000
</cfquery>
4

1 回答 1

4

The union style is appropriate, but your implementation might not be correct. You have this:

select count(col1) as range1
from tbl1
where col1 <= 15000
union
select count(col1) as range2
from tbl1
where col1 > 15001 and col1 <= 30000
etc...

If you run that you will end up with a single column and won't be able to determine what each row represents. Something like this would be better.

select 'less than 15000' range, count(col1) records
from tbl1
where col1 <= 15000
group by range
union
select '15001 to 30000' range, count(col1) as records
from tbl1
where col1 > 15001 and col1 <= 30000
group by range
etc...
于 2013-07-23T16:23:53.577 回答