我有這個查詢來計算我們的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
條款。
添加一些樣本表數據和預期的結果。順便說一句,你可以使用'case'表達式來做條件計數,並跳過第二次連接。 – jarlh