2017-03-09 79 views
1

我有這個查詢來計算我們的SOP應用程序中的訂單數量是由我們的客戶直接轉移的,並按週數對數據進行分組。在Oracle SQL中連接兩次相同的表格

Select To_Char(AUF_KOPF.ERFASS_DAT, 'IW') As "Week No", 
    Count(AUF_KOPF.AUF_NR) As "Total Orders" 
From AUF_KOPF AUF_KOPF 
    Inner Join ORDER_TOTALS On AUF_KOPF.AUF_NR = ORDER_TOTALS.ORDER_NO 
Where AUF_KOPF.KOPF_SACH In ('EDI', 'MODEM') And AUF_KOPF.ERFASS_DAT = 
    '<|Current year|>' 
Group By To_Char(AUF_KOPF.ERFASS_DAT, 'IW') 
Order By "Week No" 

結果:

Week No Total Orders 
01 253 
02 303 
03 318 
04 280 
05 256 
06 308 
07 295 
08 301 
09 333 
10 268 

但我希望看到的是計算所有其他訂單的附加列,但是當我改變它結束了與查詢結果0,沒有錯誤消息,所以我不確定我做錯了什麼。

Select To_Char(AUF_KOPF.ERFASS_DAT, 'IW') As "Week No", 
    Count(AUF_KOPF.AUF_NR) As EDI, 
    Count(b.AUF_NR) As "Other" 
From AUF_KOPF AUF_KOPF 
    Inner Join ORDER_TOTALS On AUF_KOPF.AUF_NR = ORDER_TOTALS.ORDER_NO 
    Inner Join AUF_KOPF b On b.AUF_NR = ORDER_TOTALS.ORDER_NO 
Where AUF_KOPF.KOPF_SACH In ('EDI', 'MODEM') And AUF_KOPF.ERFASS_DAT = 
    '<|Current year|>' And b.KOPF_SACH Not In ('EDI', 'MODEM') And 
    b.ERFASS_DAT = '<|Current year|>' 
Group By To_Char(AUF_KOPF.ERFASS_DAT, 'IW') 
Order By "Week No" 

結果:

Week No EDI Other 

有人能說出什麼是錯的?我所做的所有事情都再次加入了這個表格,並顛倒了WHERE條款。

+0

添加一些樣本表數據和預期的結果。順便說一句,你可以使用'case'表達式來做條件計數,並跳過第二次連接。 – jarlh

回答

1

有人能指出什麼是錯的嗎?

FROM AUF_KOPF AUF_KOPF 
     Inner Join ORDER_TOTALS 
     On AUF_KOPF.AUF_NR = ORDER_TOTALS.ORDER_NO 
     Inner Join AUF_KOPF b 
     On b.AUF_NR = ORDER_TOTALS.ORDER_NO 
Where AUF_KOPF.KOPF_SACH In ('EDI', 'MODEM') 
And b.KOPF_SACH Not In ('EDI', 'MODEM') 

沒有任何數據是很難確定,但我的猜測是,有具有相同ORDER_NO任何行有KOPF_SACH它們同時IN ('EDI', 'MODEM')NOT IN ('EDI', 'MODEM')。由於您正在執行INNER JOIN,並且訂單不能同時符合這兩個標準,因此將它們排除在外。您可以更改爲使用LEFT OUTER JOIN s。

或者你可以從WHERE子句中過濾在COUNT聚集到過濾改變:

SELECT TO_CHAR(AUF_KOPF.ERFASS_DAT, 'IW') AS "Week No", 
     COUNT(CASE WHEN a.KOPF_SACH IN ('EDI', 'MODEM') THEN a.AUF_NR END) 
     AS "Total Orders", 
     COUNT(CASE WHEN a.KOPF_SACH NOT IN ('EDI', 'MODEM') THEN a.AUF_NR END) 
     AS "Other Orders" 
FROM AUF_KOPF a 
     INNER JOIN ORDER_TOTALS o 
     ON (a.AUF_NR = o.ORDER_NO) 
WHERE a.ERFASS_DAT = '<|Current year|>' 
GROUP BY To_Char(a.ERFASS_DAT, 'IW') 
ORDER BY "Week No" 
0

並不需要你兩次加入同一個表。你可以用casedecode

Select To_Char(AUF_KOPF.ERFASS_DAT, 'IW') As "Week No", 
    Count(decode(AUF_KOPF.KOPF_SACH,'EDI', 1, 'MODEM',1)) As "Orders EDI or MODEM", 
    Count(decode(AUF_KOPF.KOPF_SACH,'EDI', null, 'MODEM',null, 1)) As "Orders but EDI or MODEM", 
    Count(AUF_KOPF.AUF_NR) As "total orders" 
From AUF_KOPF AUF_KOPF 
    Inner Join ORDER_TOTALS On AUF_KOPF.AUF_NR = ORDER_TOTALS.ORDER_NO 
Where AUF_KOPF.ERFASS_DAT = 
    '<|Current year|>' 
Group By To_Char(AUF_KOPF.ERFASS_DAT, 'IW') 
Order By "Week No" 

解決它添加一個樣本數據

with AUF_KOPF (ERFASS_DAT,KOPF_SACH, AUF_NR) as (
select sysdate, 'EDI', 1 from dual union all 
select sysdate, 'MODEM', 2 from dual union all 
select sysdate, 'EDI1', 4 from dual union all 
select sysdate, 'EDI3', 6 from dual 
) 
, ORDER_TOTALS (ORDER_NO) as (
select 1 from dual union all 
select 1 from dual union all 
select 1 from dual union all 
select 2 from dual union all 
select 2 from dual union all 
select 2 from dual union all 
select 4 from dual union all 
select 4 from dual union all 
select 6 from dual 
) 
Select To_Char(AUF_KOPF.ERFASS_DAT, 'IW') As "Week No", 
    Count(decode(AUF_KOPF.KOPF_SACH,'EDI', '1', 'MODEM','1')) As "Orders EDI or MODEM", 
    Count(decode(AUF_KOPF.KOPF_SACH,'EDI', null, 'MODEM',null, '1')) As "Orders but EDI or MODEM", 
    Count(AUF_KOPF.AUF_NR) As "total orders" 
From AUF_KOPF AUF_KOPF 
    Inner Join ORDER_TOTALS On AUF_KOPF.AUF_NR = ORDER_TOTALS.ORDER_NO 
Where trunc(AUF_KOPF.ERFASS_DAT,'yy') = date'2017-01-01' 
Group By To_Char(AUF_KOPF.ERFASS_DAT, 'IW') 
Order By "Week No"