SQL内部连接组...缺少表达式

时间:2014-12-06 21:38:13

标签: sql oracle

我有以下查询,工作正常:

SELECT  c.id, c.customer_name, b.batch_prefix, b.BatchCount, b.InvoiceCount, e.time_of_delivery, e.time_of_delivery_mail, e.time_of_delivery_clock
FROM koll_customers c
INNER JOIN (
SELECT batch_prefix, COUNT(*) AS BatchCount,
SUM (batch_counter) AS InvoiceCount
FROM koll_batchlogs
WHERE
          exists_db = 0
          and is_checked = 1
         and batch_counter > 0
         and trunc(created_date) > trunc(sysdate-7)
GROUP BY batch_prefix) b
ON b.batch_prefix=c.customer_prefix
INNER JOIN (
     SELECT  id, time_of_delivery, time_of_delivery_mail, time_of_delivery_clock
     FROM koll_customer_export) e
 ON e.id = c.id

我的要求是添加另一列' YellowCategory'。为此,我尝试将查询更改为以下内容:

SELECT  c.id, c.customer_name, b.batch_prefix, b.BatchCount, b.InvoiceCount, e.time_of_delivery, e.time_of_delivery_mail, e.time_of_delivery_clock, e.YellowCategory
FROM koll_customers c
INNER JOIN (
SELECT batch_prefix, COUNT(*) AS BatchCount,
SUM (batch_counter) AS InvoiceCount
FROM koll_batchlogs
WHERE
         exists_db = 0
         and is_checked = 1
         and batch_counter > 0
         and trunc(created_date) > trunc(sysdate-7)
GROUP BY batch_prefix) b
ON b.batch_prefix=c.customer_prefix
INNER JOIN (
     SELECT id, time_of_delivery, time_of_delivery_mail, time_of_delivery_clock, COUNT(b.batch_counter) AS YellowCategory,
     FROM koll_customer_export
     WHERE  to_date(created_date,'DD.MM.RRRR HH24:MI:SS') < to_date(sysdate-time_of_delivery,'DD.MM.RRRR HH24:MI:SS')
GROUP BY b.batch_counter) e
 ON e.id = c.id

然后我得到&#34;缺少表达&#34;错误。我想最后INNER JOIN。不知道问题出在哪里......有什么帮助吗?

更新

通过以下查询,我现在收到b.batch_counter无效的标识符错误。

SELECT  c.id, c.customer_name, b.batch_prefix, b.BatchCount, 
        b.InvoiceCount, e.time_of_delivery, e.time_of_delivery_mail, 
        e.time_of_delivery_clock, e.YellowCategory
FROM koll_customers c
INNER JOIN (
              SELECT batch_prefix, batch_counter, COUNT(*) AS BatchCount,
                     SUM (batch_counter) AS InvoiceCount
               FROM koll_batchlogs
               WHERE exists_db = 0 and is_checked = 1
                     and batch_counter > 0 and trunc(created_date) > trunc(sysdate-7)
               GROUP BY batch_prefix
           ) b
ON b.batch_prefix=c.customer_prefix
INNER JOIN (
              SELECT id, time_of_delivery, time_of_delivery_mail, 
               time_of_delivery_clock,                  
              COUNT(b.batch_counter) AS YellowCategory
              FROM koll_customer_export
              WHERE  to_date(created_date,'DD.MM.RRRR HH24:MI:SS') 
                        < to_date(sysdate- time_of_delivery,'DD.MM.RRRR HH24:MI:SS')
              GROUP BY b.batch_counter
             ) e
 ON e.id = c.id

2 个答案:

答案 0 :(得分:1)

这是您想要的查询吗?

SELECT c.id, c.customer_name, b.batch_prefix, b.BatchCount, b.InvoiceCount,
       e.time_of_delivery, e.time_of_delivery_mail, e.time_of_delivery_clock, e.YellowCategory
FROM koll_customers c INNER JOIN
     (SELECT batch_prefix, COUNT(*) AS BatchCount, SUM (batch_counter) AS InvoiceCount
      FROM koll_batchlogs
      WHERE exists_db = 0 and is_checked = 1 and batch_counter > 0 and
            trunc(created_date) > trunc(sysdate-7)
      GROUP BY batch_prefix
     ) b
     ON b.batch_prefix = c.customer_prefix INNER JOIN
     (SELECT id, time_of_delivery, time_of_delivery_mail, time_of_delivery_clock,
             COUNT(*) AS YellowCategory,
     FROM koll_customer_export
     WHERE to_date(created_date, 'DD.MM.RRRR HH24:MI:SS') < to_date(sysdate-time_of_delivery,'DD.MM.RRRR HH24:MI:SS')
     GROUP BY id, time_of_delivery, time_of_delivery_mail, time_of_delivery_clock
    ) e
    ON e.id = c.id;

我不可能说这是否是你真正需要的。但我认为查询至少会编译,所以你可以运行它。where子查询中的e子句看起来很奇怪。为什么要使用to_date()将日期列转换为日期列?

答案 1 :(得分:0)

您在最后一次INNER JOIN内的SELECT列表末尾输入了一个逗号太多。