Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL使用排序选择计数_Sql_Informix - Fatal编程技术网

SQL使用排序选择计数

SQL使用排序选择计数,sql,informix,Sql,Informix,我有几行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 d

我有几行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
因此,我得到的是特定表中按正确顺序小于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子句,并且我需要它,因为它的顺序并不总是正确的,那么第二个解决方案就会崩溃。在这种情况下,有没有一种方法可以包含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 ",
             " ) "
因此,没有必要订购。如果需要表id的引用和每个id的计数,则需要将该字段添加到第一个select中,并在需要的order by之前的末尾添加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

。。。或者正如其他评论者所说,为什么顺序很重要?

不,您不能使用order by idno,因为idno不会出现在结果中。据我所知,您为什么需要order by?您正在堆积结果以获得计数,因此顺序根本不相关,不是吗?是的,但是如果查询返回结果为1、2、3、4、5、6、7、8、9、11、10,那么结果将是错误的。查询如何返回idno>10?您正在筛选出大于10的值…@Trent您说不,您不能使用order by idno,因为idno不会出现在结果中。Informix现在不是都这么做了吗?订单对计数有何影响?也就是说,如果省略ORDERBY子句,对计数有什么影响?你应该非常仔细地回顾你正在做的事情,因为订单毫无意义,尽管它很重要。至少,您需要认真解释一下在什么情况下订单对计数很重要。它只用于检索一个值。它需要计算小于10的行总数唯一的问题是它们在数据库中的顺序不正确您可以添加一个示例,说明您的输入数据是什么以及您希望得到什么结果吗?我将接受这一正确答案,因为这似乎是最可靠的方法,使用foreach循环