查询错误,无法正确显示

时间:2014-02-17 07:24:33

标签: sql oracle syntax-error

我的查询中有错误,我试图显示日期,数据名称(即POS和非POS)和数据的数量。

期望的输出:

02/20/2014, POS, 40
02/20/2014, Non-POS, 15

查询:

SELECT To_char(D.dtime_day, 'MM/dd/yyyy') 
FROM   owner_dwh.dc_date d 
       left join (SELECT Count(CASE 
                                 WHEN Upper(t.ticket_customer_user_id) = Upper( 
                                      'POS-generic') 
                                                                    THEN 1 
                                 ELSE NULL 
                               END) --POS 
                         ||',' 
                         ||Count(CASE 
                                   WHEN Upper(t.ticket_customer_user_id) = Upper 
                                        ( 
                                        'hcphuser') THEN 
                                   1 
                                   ELSE NULL 
                                 END) --USER 
                  FROM   app_account.otrs_ticket t 
                  WHERE  Trunc(t.create_time, 'DAY') BETWEEN 
                         SYSDATE - 119 AND SYSDATE - 1 
                  GROUP  BY Trunc(t.create_time, 'DAY')) 
              ON d.dtime_day = t.create_time 
       left join(SELECT Count(CASE 
                                WHEN Upper(t.ticket_customer_user_id) = Upper( 
                                     'POS') 
                                                                    THEN 1 
                                ELSE NULL 
                              END) --POS 
                        ||',' 
                        ||Count(CASE 
                                  WHEN Upper(t.ticket_customer_user_id) = Upper( 
                                       'user') 
                                THEN 1 
                                  ELSE NULL 
                                END) --USER 
                 FROM   app_account.otrs_ticket t 
                 WHERE  Trunc(t.close_time, 'DAY') BETWEEN SYSDATE - 119 AND 
                                                           SYSDATE - 1 
                 GROUP  BY Trunc(t.close_time, 'DAY')) 
              ON d.dtime_day = t.close_time 
WHERE  t.queue_id NOT IN ( 63, 61, 69, 59, 
                           58, 60, 56, 64, 
                           65, 23, 67, 68, 57 ); 

我的错误:

 ORA-00904: "T"."CREATE_TIME": invalid identifier
 00904. 00000 -  "%s: invalid identifier"

DTIME_DAY(OWNER_DWH.DC_DATE)中的数据是这样的:

  02/12/2014
  02/13/2014
  02/14/2014

在App_account.otrs.ticket

create_time和close_time包含打开和关闭故障单的日期 TICKET_CUSTOMER_USER_ID包含POS-generic和user(类似于一个类别)

请帮助我并更正我的疑问。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您必须在括号外重复别名:

SELECT To_char(D.dtime_day, 'MM/dd/yyyy') 
FROM   (owner_dwh.dc_date d 
        left join (SELECT Count(CASE 
                                  WHEN Upper(t.ticket_customer_user_id) = Upper( 
                                       'POS-generic') 
                                                                      THEN 1 
                                  ELSE NULL 
                                END) --POS 
                          ||',' 
                          ||Count(CASE 
                                    WHEN Upper(t.ticket_customer_user_id) = 
                                         Upper( 
                                         'hcphuser') THEN 
                                    1 
                                    ELSE NULL 
                                  END), --USER
                          t.create_time -- !!!of course, did not think about returning the field from the subquery!!!
                   FROM   app_account.otrs_ticket t 
                   WHERE  Trunc(t.create_time, 'DAY') BETWEEN 
                          SYSDATE - 119 AND SYSDATE - 1 
                          AND t.queue_id NOT IN ( 63, 61, 69, 59, 
                                                  58, 60, 56, 64, 
                                                  65, 23, 67, 68, 57 ) 
                   GROUP  BY Trunc(t.create_time, 'DAY')) t 
               ON d.dtime_day = t.create_time) 
       left join(SELECT Count(CASE 
                                WHEN Upper(t.ticket_customer_user_id) = Upper( 
                                     'POS') 
                                                                      THEN 1 
                                ELSE NULL 
                              END) --POS 
                        ||',' 
                        ||Count(CASE 
                                  WHEN Upper(t.ticket_customer_user_id) = Upper( 
                                       'user') 
                                THEN 1 
                                  ELSE NULL 
                                END), --USER
                        t.close_time -- return from subquery!!!!
                 FROM   app_account.otrs_ticket t 
                 WHERE  Trunc(t.close_time, 'DAY') BETWEEN SYSDATE - 119 AND 
                                                           SYSDATE - 1 
                        AND t.queue_id NOT IN ( 63, 61, 69, 59, 
                                                58, 60, 56, 64, 
                                                65, 23, 67, 68, 57 ) 
                 GROUP  BY Trunc(t.close_time, 'DAY')) u 
              ON d.dtime_day = u.close_time --you can not use t twice for the joins!!! 

这是我可以从您的查询中收集到的所有内容......