2

有没有一种简单的方法来实现这个查询结果来提高它的性能。我不想为每个选择加入两个表。

select Product, LCTL, LQ, LCTQL from
(
(select 'Phone / HSI' as Product, (select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and c.category_long_name='m ECOMM ORD') as LCTL,
(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and (c.category_long_name='CRIS ECOMM ORD' or c.category_long_name='CRIS ECOMM HELD')) as LQ,
(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and (c.category_long_name='CRIS ECOMM ORD' or c.category_long_name='CRIS ECOMM HELD' or c.category_long_name='m ECOMM ORD')) as LCTQL from dual
union all

select '(Phone / HSI) + Prism' as Product,(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and c.category_long_name='m ECOMM PRISM') as LCTL,
(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and c.category_long_name='CRIS ECOMM PRISM') as LQ,
(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and (c.category_long_name='m ECOMM PRISM' or c.category_long_name='CRIS ECOMM PRISM')) as LCTQL from dual

union all
select '(Phone / HSI) + DTV' as Product,(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and c.category_long_name='m ECOMM DTV') as LCTL,
(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and c.category_long_name='CRIS ECOMM DTV') as LQ,
(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and (c.category_long_name='m ECOMM DTV' or c.category_long_name='CRIS ECOMM DTV')) as LCTQL from dual

union all
select 'Total' as Product,(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and (c.category_long_name='m ECOMM DTV' or c.category_long_name='m ECOMM PRISM' or c.category_long_name='m ECOMM DTV')) as LCTL,
(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and (c.category_long_name='CRIS ECOMM DTV' or c.category_long_name='CRIS ECOMM PRISM' or c.category_long_name='CRIS ECOMM ORD' or c.category_long_name='CRIS ECOMM HELD')) as LQ,
(select count(1) as "LCTL" from tasks t,task_categories c where t.category_id=c.category_id and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))>=?1 and to_date(to_char(t.user_create_date,'DD-MON-YYYY'))<=?2 and (c.category_long_name='m ECOMM DTV' or c.category_long_name='m ECOMM PRISM' or c.category_long_name='m ECOMM DTV' or c.category_long_name='CRIS ECOMM DTV' or c.category_long_name='CRIS ECOMM PRISM' or c.category_long_name='CRIS ECOMM ORD' or c.category_long_name='CRIS ECOMM HELD' )) as LCTQL from dual)
)

输出应该如下:

Product                 LCTL    LQ      LCTQL
Phone / HSI         17209   39849   57058
(Phone / HSI) + Prism   4095    6   4101
(Phone / HSI) + DTV 6192    1   6193


Total   10287   39856   50143
4

1 回答 1

1

我认为您的查询可以这样写:

SELECT
  product,
  SUM(CASE WHEN c.category_long_name  = 'm ECOMM ORD'      THEN 1 ELSE 0 END) AS LCTL,
  SUM(CASE WHEN c.category_long_name  = 'CRIS ECOMM ORD' 
             OR c.category_long_name  = 'CRIS ECOMM HELD'  THEN 1 ELSE 0 END) AS LQ,
  SUM(CASE WHEN c.category_long_name IN('CRIS ECOMM ORD',
                                        'CRIS ECOMM HELD',
                                        'm ECOMM ORD')     THEN 1 ELSE 0 END) AS LCTQ,
  SUM(CASE WHEN c.category_long_name = 'm ECOMM PRISM'     THEN 1 ELSE 0 END) AS lccc,
  ...
  ...
  COUNT(*) AS "Total"
FROM tasks t
INNER JOIN task_categories c ON t.category_id=c.category_id
WHERE to_date(to_char(t.user_create_date, 'DD-MON-YYYY')) >= ?1
  and to_date(to_char(t.user_create_date, 'DD-MON-YYYY')) <= ?2
GROUP BY product;

使用CASE表达式 with GROUP BY product,您可以在一个查询中执行此操作。这是基本思想。

此外,您可能需要pivot在 Oracle 中使用运算符,但不清楚是否可以使用pivot运算符编写。

于 2013-02-06T07:17:52.723 回答