计算别名表的行数

时间:2014-10-30 09:08:19

标签: sql

select count (*) from
(select a.ACCOUNT_POID_ID0,a.STATUS as moov_profile_status,b.SUB_PLAN_ID,b.PLAN_ID,c.STATUS,c.NAME
from VMS_MOOV_PROFILE_T a, 
VMS_SUB_PLAN_NO_REGEN_T b,
VMS_VOUCHER_CONF_T c,    
where b.ACCOUNT_POID_ID0 = a.ACCOUNT_POID_ID0 
and c.VOUCHER_ID = (select f.VOUCHER_ID from VMS_PLAN_VOUCHER_T f where f.PLAN_ID = b.PLAN_ID));

这会返回错误。我如何计算行数?

2 个答案:

答案 0 :(得分:1)

Try this

SELECT count (*)
FROM   (SELECT a.ACCOUNT_POID_ID0,
               a.STATUS AS moov_profile_status,
               b.SUB_PLAN_ID,
               b.PLAN_ID,
               c.STATUS,
               c.NAME
        FROM   VMS_MOOV_PROFILE_T a,
               VMS_SUB_PLAN_NO_REGEN_T b,
               VMS_VOUCHER_CONF_T c
        WHERE  b.ACCOUNT_POID_ID0 = a.ACCOUNT_POID_ID0
               AND c.VOUCHER_ID = (SELECT f.VOUCHER_ID
                                   FROM   VMS_PLAN_VOUCHER_T f
                                   WHERE  f.PLAN_ID = b.PLAN_ID)) t

答案 1 :(得分:0)

您的错误是:

  1. 您忘了告诉我们您收到的错误消息。
  2. 你忘了告诉我们你正在使用什么dbms。
  3. 有许多逗号(表别名c之后)。
  4. 根据dbms,您可能还需要派生表的名称(即在右括号后添加一些虚拟名称)。
  5. 更正的查询是:

    select count (*) from
    (
      select a.ACCOUNT_POID_ID0,a.STATUS as moov_profile_status,b.SUB_PLAN_ID,b.PLAN_ID,c.STATUS,c.NAME
      from VMS_MOOV_PROFILE_T a, 
      VMS_SUB_PLAN_NO_REGEN_T b,
      VMS_VOUCHER_CONF_T c
      where b.ACCOUNT_POID_ID0 = a.ACCOUNT_POID_ID0 
      and c.VOUCHER_ID = (select f.VOUCHER_ID from VMS_PLAN_VOUCHER_T f where f.PLAN_ID = b.PLAN_ID)
    ) dummy;
    

    此外,您加入表的方式(使用VMS_PLAN_VOUCHER_T上的子查询)有点奇怪。而且你不应该使用二十多年前在标准SQL中替换的旧的逗号分隔连接语法。另外:无论如何,你都不需要派生表。

    以下是您的简化查询:

    select count (*)
    from VMS_MOOV_PROFILE_T a 
    join VMS_SUB_PLAN_NO_REGEN_T b on b.ACCOUNT_POID_ID0 = a.ACCOUNT_POID_ID0 
    join VMS_PLAN_VOUCHER_T f on f.PLAN_ID = b.PLAN_ID
    join VMS_VOUCHER_CONF_T c on c.VOUCHER_ID = f.VOUCHER_ID;