2016-05-18 96 views
1

我在oracle中編寫了一個腳本。但它並沒有給我我想要的結果。 我需要這個,想象我有兩張桌子。 Order_table和書桌。 我的訂單表是這樣的在其他表中計算一個表的字段

ORDER_TABLE表

ID TYPE_ID VALUE_ID 
1 11  null 
2 11  null 
3 11  null 
4 12  null 
5 11  null 

書表

ID ORDER_TYPE DELETED 
1  1   F 
2  null  F 
3  5   F 
4  5   F 
5  4   F 
6  4   F 
7  3   T 

我的劇本是這樣的

Select * 
From (
    Select Newtable.Counter As Value_id, 
     o.Id As Id, 
     o.Type_id As Type_id 
    From (
      Select (Count B.Order_Type) As Counter, 
        B.Order_Type As Id 
      From Book B 
      Where B.Deleted = 'F' 
      Group By B.Order_Type 
      Order By Count(B.Order_Type) Desc 
     ) newtable, 
     order_table o 
    where o.id = newtable.id 
    and o.type_id = 11 
) 
order by id asc; 

結果是這樣的。

Value_ID TYPE_ID ID 
2   11  5 
2   11  4 
1   11  1 

沒有顯示第二和第三個id有0個計數,我能顯示0個計數嗎?

結果應該是這樣的。

Value_ID TYPE_ID ID 
2   11  5 
2   11  4 
1   11  1 
0   11  2 
0   11  3 
+0

order_table和book_table之間的關係是什麼? – Prathyush

回答

1

首先,不要使用隱JOIN語法(逗號分隔),這是這個錯誤是很難趕上的原因之一!使用正確的JOIN語法。

其次,你的問題是,你需要一個left join,不是inner join,那麼試試這個:

Select * 
    From (Select coalesce(Newtable.Counter,0) As Value_id, 
       o.Id    As Id, 
       o.Type_id  As Type_id 
     From order_table o 
     LEFT JOIN (Select Count(B.Order_Type) As Counter, B.Order_Type As Id 
        From Book B 
        Where B.Deleted = 'F' 
        Group By B.Order_Type 
        Order By Count(B.Order_Type) Desc) newtable 
      ON(o.id = newtable.id) 
     WHERE o.type_id = 11) 
order by id asc; 
+0

這個查詢比以前的查詢更快@sagi?謝謝你的幫助。腳本解決了我的問題:) –

+0

@Jo_bast這不是一個很大的區別。沒問題:) – sagi

+0

他有一個錯字,沒有注意到。固定@ MT0 – sagi

0

甲骨文設置

CREATE TABLE order_table (id, type_id, value_id) AS 
SELECT 1, 11, CAST(NULL AS INT) FROM DUAL UNION ALL 
SELECT 2, 11, CAST(NULL AS INT) FROM DUAL UNION ALL 
SELECT 3, 11, CAST(NULL AS INT) FROM DUAL UNION ALL 
SELECT 4, 12, CAST(NULL AS INT) FROM DUAL UNION ALL 
SELECT 5, 11, CAST(NULL AS INT) FROM DUAL; 

CREATE TABLE book (id, order_type, deleted) AS 
SELECT 1, 1, 'F' FROM DUAL UNION ALL 
SELECT 2, NULL, 'F' FROM DUAL UNION ALL 
SELECT 3, 5, 'F' FROM DUAL UNION ALL 
SELECT 4, 5, 'F' FROM DUAL UNION ALL 
SELECT 5, 4, 'F' FROM DUAL UNION ALL 
SELECT 6, 4, 'F' FROM DUAL UNION ALL 
SELECT 7, 3, 'T' FROM DUAL; 

查詢

SELECT COUNT(b.order_type) AS value_id, 
     o.id, 
     o.order_type 
FROM order_table o 
     LEFT OUTER JOIN 
     book b 
     ON (o.id = b.order_type AND b.deleted = 'F') 
WHERE o.type_id = 11 
GROUP BY o.id, o.type_id 
ORDER BY value_id DESC, id DESC; 

輸出

VALUE_ID ID TYPE_ID 
-------- -- ------- 
     2 5  11 
     1 1  11 
     0 3  11 
     0 2  11 

然而,如果你確實想使用傳統的Oracle逗號連接語法,那麼你可以得到相同的結果:

SELECT COUNT(b.order_type) AS value_id, 
     o.id, 
     o.order_type 
FROM order_table o, 
     book b 
WHERE o.type_id  = 11 
AND b.order_type (+) = o.id 
AND b.deleted (+) = 'F' 
GROUP BY o.id, o.type_id 
ORDER BY value_id DESC, id DESC; 

不要因爲ANSI/ISO連接更容易理解連接條件。

0

你也可以用標量子查詢來做到這一點,它可能會或可能不會比其他答案中描述的左連接版本更高效。 (很可能,優化器可能會將其重寫爲左連接!):

with order_table (id, type_id, value_id) as (select 1, 11, cast(null as int) from dual union all 
               select 2, 11, cast(null as int) from dual union all 
               select 3, 11, cast(null as int) from dual union all 
               select 4, 12, cast(null as int) from dual union all 
               select 5, 11, cast(null as int) from dual), 
      book (id, order_type, deleted) as (select 1, 1, 'F' from dual union all 
               select 2, null, 'F' from dual union all 
               select 3, 5, 'F' from dual union all 
               select 4, 5, 'F' from dual union all 
               select 5, 4, 'F' from dual union all 
               select 6, 4, 'F' from dual union all 
               select 7, 3, 'T' from dual) 
-- end of mimicking your tables; you wouldn't need the above subqueries as you already have the tables. 
-- See SQL below: 
select (select count(*) from book bk where bk.deleted = 'F' and bk.order_type = ot.id) value_id, 
     ot.type_id, 
     ot.id 
from order_table ot 
order by value_id desc, 
     id desc; 

    VALUE_ID TYPE_ID   ID 
---------- ---------- ---------- 
     2   11   5 
     2   12   4 
     1   11   1 
     0   11   3 
     0   11   2 
相關問題