Ahmed Shehzad
Ahmed Shehzad

Reputation: 3

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

Upvotes: 0

Views: 768

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270301

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()?

Upvotes: 1

Œlrim
Œlrim

Reputation: 543

You typed one comma too much at the end of the SELECT list inside the last INNER JOIN.

Upvotes: 0

Related Questions