因此,我很高興能夠使用一個非常舊的數據庫,該數據庫顯然是在規範化發明之前設置的。我被要求看看我能否想出一種使其正確工作的方法。DB2 - 獲取字符串中多個位置的方法
第一個表實際上有一個像真正的主鍵。 例如:
ID, Reason
--- ----------
1, Write off
2, Overage
3, OLH
的問題是其他的表...
CustomerNum, JobNum, Reasons
------------ ------- ---------------------
42351, 46, X
32313, 456, X
85472, 13, X X
如何在這些表中的系統加入?是的,X的位置在線上。所以如果X位於第一位,那就是原因1,第二位,原因2等等。它本質上是一個平面陣列。這實際上並不壞,如果他們將其限制爲每行1X ...(LOCATE('X', REASONS) as XINDEX
),但事實並非如此。理論上有21種可能的X可以在每一行上檢查。
所以我必須給他們一個關於如何使它工作的建議。
我的第一個建議之一是創建一個單獨的表並對錶格進行規範化,但是我不知道這樣做會有多好,或者他們是否願意改變他們的系統。
所以,我還想建議一些存儲過程,這些存儲過程能夠通過每一行並返回索引,就好像它們在單獨的表中一樣。
我不知道這是否可能,但我很有希望。
編輯
所以,是的,我真的去推鏈接表。
這裏,我已經得到了從建議中工作的選擇:我將與馬林的建議玩了一下,看看它有多少簡化它
Select tblCustomers.*,
CASE WHEN SUBSTRING(Reasons,1,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 1)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,2,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 2)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,3,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 3)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,4,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 4)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,5,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 5)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,6,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 6)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,7,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 7)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,8,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 8)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,9,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 9)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,10,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 10)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,11,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 11)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,12,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 12)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,13,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 13)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,14,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 14)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,15,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 15)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,16,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 16)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,17,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 17)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,18,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 18)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,19,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 19)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,20,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 20)) || ', ' ELSE '' END
|| CASE WHEN SUBSTRING(Reasons,21,1) = 'X' THEN RTRIM((SELECT Reason FROM tblReasons WHERE ID = 21)) || ', ' ELSE '' END AS XPOS
From tblCustomers
,但有一件事我不喜歡約這個查詢是它顯示了他們當前的佈局是多麼荒謬,以及他們爲什麼應該改變它。
這爲它做的工作...我希望他們雖然正常化。幸好我在這方面的責任是給予他們可能的解決方案,而不是重寫所有的東西。 – AndyD273