Coldfusion查询查询案例如语句

时间:2013-07-23 15:15:42

标签: coldfusion

之前我问过这个问题,虽然答案已经解决,但实际上并没有解决我的问题,因为据我所知,CF QoQ不支持CASE语句。

以下是另一个问题:Background

那么有没有更简单的方法来完成我正在尝试做的事情,而不在sql中使用CASE语句,或者我将不得不回到我最初提出的联合风格的答案与?

这就是我想出来的:

<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>

1 个答案:

答案 0 :(得分:4)

联合样式是合适的,但您的实现可能不正确。你有这个:

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...

如果你运行它,你将得到一个列,并且无法确定每一行代表什么。这样的事情会更好。

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...