简体   繁体   中英

SQL Inner join group… missing expression

I have following query, which works fine:

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

My requirement is to add another column 'YellowCategory'. For that I tried to change the query to following:

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

But then I get "missing expression" error. I guess in last INNER JOIN. Don't know where is the problem... Any help?

Update

With following query, I get b.batch_counter invalid identifier error now.

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

Is this the query you want?

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;

It is impossible for me to say if this is what you really need . But I think the query will at least compile so you can run it.. The where clause in the e subquery looks really strange. Why would you be converting a date column into a date column using to_date() ?

您在最后一个INNER JOIN的SELECT列表的末尾键入了一个逗号。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM