SQL Select 使用排序计数
SQL Select Count using ordering
我有几行 Informix-4GL 代码执行以下操作
##
# prepare sql
##
let lv_sql = " select table.idno from table ",
" where table.status != 'X' ",
" and table.idno <= 10 ",
" order by table.idno "
prepare table_sel from lv_sql
declare table_cur cursor for table_sel
##
# loop through results and count them
##
let count = 0
foreach table_cur into ti_num
let count = count + 1
end foreach
display count
所以我得到特定 table 中按正确顺序排列少于 10 的行的总数,但我需要一个 foreach 循环来计算总数
我有第二种方法,我更喜欢
##
# prepare sql
##
let lv_sql = " select count(table.idno) from table ",
" where table.idno in ( ",
" select table.idno from table "
" where table.status != 'X' ",
" and table.idno <= 10 ",
" ) "
prepare table_sel from lv_sql
##
# just get result
##
execute table_sel into count
display count
问题是,如果我在 where in 过滤器中包含 order by 子句并且我需要它,那么第二个解决方案会崩溃,因为它的顺序并不总是正确的。有没有办法在这种情况下包含订单?
为什么不直接在代码的第一部分进行计数?
let lv_sql = " select count(table.idno) from table ",
" where table.status != 'X' ",
" and table.idno <= 10 ",
" order by table.idno "
有效吗?
我认为你的问题在于你计算的方式。如果我没记错的话,你的 Select 计数将只检索一个值,我的意思是这个:
let lv_sql = " select count(table.idno) from table ",
" where table.idno in ( ",
" select table.idno from table "
" where table.status != 'X' ",
" and table.idno <= 10 ",
" ) "
因此,无需订购。如果您需要 table id 的引用和每个 Id 的计数,您需要将该字段添加到您的第一个 select 并在末尾添加一个 group by 子句,在订单之前根据你的需要,像这样:
let lv_sql = " select table.idno, count(table.idno) as counting from table ",
" where table.idno in ( ",
" select table.idno from table "
" where table.status != 'X' ",
" and table.idno <= 10 ",
" ) group by table.idno order by counting"
请告诉我这是否是您要找的东西
我一直在重新阅读问题并认为答案是...
let lv_sql = " select table.idno, count(*) ",
" from table ",
" where table.status != 'X' ",
" and table.idno <= 10 ",
" order by table.idno "
prepare table_sel from lv_sql
declare table_cur cursor for table_sel
foreach table_cur into l_idno, l_count
...
end foreach
...或者正如其他评论者所说,为什么顺序很重要?
我有几行 Informix-4GL 代码执行以下操作
##
# prepare sql
##
let lv_sql = " select table.idno from table ",
" where table.status != 'X' ",
" and table.idno <= 10 ",
" order by table.idno "
prepare table_sel from lv_sql
declare table_cur cursor for table_sel
##
# loop through results and count them
##
let count = 0
foreach table_cur into ti_num
let count = count + 1
end foreach
display count
所以我得到特定 table 中按正确顺序排列少于 10 的行的总数,但我需要一个 foreach 循环来计算总数
我有第二种方法,我更喜欢
##
# prepare sql
##
let lv_sql = " select count(table.idno) from table ",
" where table.idno in ( ",
" select table.idno from table "
" where table.status != 'X' ",
" and table.idno <= 10 ",
" ) "
prepare table_sel from lv_sql
##
# just get result
##
execute table_sel into count
display count
问题是,如果我在 where in 过滤器中包含 order by 子句并且我需要它,那么第二个解决方案会崩溃,因为它的顺序并不总是正确的。有没有办法在这种情况下包含订单?
为什么不直接在代码的第一部分进行计数?
let lv_sql = " select count(table.idno) from table ",
" where table.status != 'X' ",
" and table.idno <= 10 ",
" order by table.idno "
有效吗?
我认为你的问题在于你计算的方式。如果我没记错的话,你的 Select 计数将只检索一个值,我的意思是这个:
let lv_sql = " select count(table.idno) from table ",
" where table.idno in ( ",
" select table.idno from table "
" where table.status != 'X' ",
" and table.idno <= 10 ",
" ) "
因此,无需订购。如果您需要 table id 的引用和每个 Id 的计数,您需要将该字段添加到您的第一个 select 并在末尾添加一个 group by 子句,在订单之前根据你的需要,像这样:
let lv_sql = " select table.idno, count(table.idno) as counting from table ",
" where table.idno in ( ",
" select table.idno from table "
" where table.status != 'X' ",
" and table.idno <= 10 ",
" ) group by table.idno order by counting"
请告诉我这是否是您要找的东西
我一直在重新阅读问题并认为答案是...
let lv_sql = " select table.idno, count(*) ",
" from table ",
" where table.status != 'X' ",
" and table.idno <= 10 ",
" order by table.idno "
prepare table_sel from lv_sql
declare table_cur cursor for table_sel
foreach table_cur into l_idno, l_count
...
end foreach
...或者正如其他评论者所说,为什么顺序很重要?