I would like to write a simple Oracle SQL 10g statement to compare the items (strings) in an arbitrarily defined comma separated list to a field containing a list of comma separated strings. The items in the defined list can appear in the field in any order, and exact matches need to be found (not substrings).
I have a working solution using a series of regexp_like() statements entered manually, but I need to hand this off to a client who will be maintaining this moving forward, and would like to be able to just update the comma separated string directly.
I also have some software GUI based limitations on what I can do with Oracle SQL to accomplish this. Specifically I can not use any PL/SQL and this must be written in a single select statement (no temporary tables or anything fun/useful like that.) I've found a number of solutions to what I'm trying to accomplish, but almost all depended on being able to write custom functions.
So, now that backstory/limitations are out of the way, let's get down to the nitty gritty.
Example arbitrary (client-provided) list: ItemA,ItemB,ItemC
Table ITEMS:
Column Items (varchar2 of some arbitrary but sufficient length)
- ItemA,ItemB,ItemC
- ItemC,ItemB,ItemA
- ItemX,ItemC,ItemY,ItemA,ItemB,ItemB
- ItemX,ItemY,ItemC
I want a single select statement that will basically select all rows where Items contains "ItemA" and "ItemB" and "ItemC", but without having to break that string up manually. In this case, it would match the first, second and third row, but not the fourth row.
(EDIT) I realize this table structure is badly designed. At this time I do not know if we can go back to the client to fix this as the data may be used as is elsewhere already, making a redesign would be costly and time consuming. I'm sure a lot of you are used to this scenario. The initial system was designed poorly, now I've been brought in to consult on difficulties arising from the poor design. Let's assume it is not possible to normalize this table, and must be used as is.
It is entirely possible that what I would like to do is simply not possible given the limitations of the interface I need to use, but my SQL knowledge is not great enough to determine that.
Thank you all very much for taking the time to read this question. Please let me know if anything is confusing or needs expansion or clarification.
NORMALIZATION
, the basic principle ofRelational data model
. Read about it first. Your design is flawed. The way you display data is NOT the way you store it in database. – Lalit Kumar B