The best solution to an ugly problem

I am looking for the best way to put together a query on a table that has a partial construction like the following:
Key loss
1 arm
2 leg
3 eye
4 leg/eye
5 leg/eye/arm/foot
6 foot/eye
7 hand/foot
(I know, I know. Don't shoot the messenger! this is the table I have to deal with.)
I have to accept the value from an Apex shuttle object that returns selected values in a colon delimited string. Here are two examples of a potential query string:
arm:leg
eye
I need to the records that meet all the minimum criteria. For example, if the request is:
arm
My query needs to return records 1 and 5. If the query is for
arm:leg
my query needs to return ONLY record five.
As you can see the order does not matter, but all selected values must be contained in the loss column.
Now here is the part that I am trying to discern: What is the best way to construct this query?
Is there a clever way to use Oracle regular expressions to execute the query in one step? Or, do I need to construct some sort of union query?
The database is 10.2
Thanks in advance!

Hi,
You're right; this is an ugly problem.
Vorlon1 wrote:
I am looking for the best way to put together a query on a table that has a partial construction like the following:
Key loss
1 arm
2 leg
3 eye
4 leg/eye
5 leg/eye/arm/foot
6 foot/eye
7 hand/footThis is the ugliest part of the problem.
Sometimes we have to accept delimited lists, like the colon-delimited parameter in this problem, but there is no excuse for storing such a list in the database. Each column should store one piece of information. This is something so basic to relational datbase design that it is called First Normal Form.
(I know, I know. Don't shoot the messenger! this is the table I have to deal with.)Okay; pass it along to whoever is to blame.
I have to accept the value from an Apex shuttle object that returns selected values in a colon delimited string. Here are two examples of a potential query string:
arm:leg
eye
I need to the records that meet all the minimum criteria. For example, if the request is:
arm
My query needs to return records 1 and 5. If the query is for
arm:leg
my query needs to return ONLY record five.
As you can see the order does not matter, but all selected values must be contained in the loss column.
Now here is the part that I am trying to discern: What is the best way to construct this query?One way (shown below) is to split the colon-delimitd input string into individual items (one per row), join that result set to the table, GROUP BY the rows in the table, and only display the results if the total number of matches equals the total number of items in the input string:
WITH      targets     AS
     SELECT     '%/' || REGEXP_SUBSTR ( :str
                                , '[^:]+'
                                , 1
                                , LEVEL
               || '/%'          AS target
     ,     target_cnt
     FROM     (
              SELECT  LENGTH (         :str       || 'ab')
                    - LENGTH (REPLACE (:str, ':') || 'a')     AS target_cnt
              FROM  dual
     CONNECT BY     LEVEL     <= target_cnt
SELECT       l.key
FROM       limbs        l
JOIN       targets  t  ON   '/' || l.loss
                              || '/'     LIKE t.target
GROUP BY  l.key
HAVING       COUNT (*)     = MIN (t.target_cnt)
Is there a clever way to use Oracle regular expressions to execute the query in one step? Regular expressions can't do the whole job, but they sure help. The problem is that you might get input like 'arm:leg:eye', but the row in the table might have 'leg/arm/eye', or 'eye/leg/arm', or 4 other arrangements, so you can't just look for the input string as a whole.
If you were using Oracle 11.1 (or higher) you could also use REGEXP_COUNT instead of LENGTH to comput target_cnt.
Or, do I need to construct some sort of union query?UNIONs are often slow. Whenever you're tempted to use UNION, see if there's some way you can use aggregate or analytic functions instead. That will often be simpler and more efficient than UNION.
Edited by: Frank Kulash on Jan 29, 2013 8:56 PM
NSK2KSN wrote:
... even am also trying and waiting for better solutionI just saw your solution.
This is basically the same approach, only instead of splitting both the parameter and the table strings into individual parts, it only splits the parameter, saving a somewhat messy CONNECT BY query.

Similar Messages

  • TA24002 My 500 GB can't verify nor repair. I have photoshop work that I need to recover. I would like to know which erase option would be the best solution for this problem.

    My 500 GB can't verify nor repair. I have photoshop work that I need to recover. I would like to know what option would be the best solution for this problem?

    You appear to have two issues: 1) a hard drive that is not working properly and 2) files you wish to recover.
    Re 1) you need to answer Kappy's questions.
    Re 2) does the drive load and can you see your photo files? If so can you copy them to another drive?
    Do you not have a backup of the photo files?

  • I have problems in the initiation of the Encore process when opening presents the following error message : "Encore CS6 Cannot Run in Non-Royalty Serialized".... What is the best solution for this problem ?

    Help Me.
    What is the best solution for this problem ?

    Encore is activated when you activate Premiere Pro... so, as Stan asked, how did you install P-Pro?
    Ask for serial number http://forums.adobe.com/thread/1234635 has a FAQ link
    -and a fix for Encore http://forums.adobe.com/thread/1421765?tstart=0 in reply #7
    -plus more Encore http://helpx.adobe.com/encore/kb/cant-write-image-fie-larger1.html

  • What is the best solution to this problem?

    I have so many solutions in mind right now but i am looking for the best solution if possible. i have the following query
    SELECT one_query.date_required as Month_id,
               nvl(one_query.amount_used, 0) as overalluserhours_A,
                  nvl(second_query.amount_used_b, 0) as overalluserhours_B,
                  nvl((trunc(((second_query.amount_used_b/one_query.amount_used) * 100), 2)), 0) as p_change
    from
                 (select to_char(b1.needed_date,'YYYY-MM') as date_required,
                    SUM(b1.amount_used) as amount_used,
                      b1.type_id as type_id
                        from table_one b1
                         where b1.zone_type like 'NEWYORK%'
                         and b1.type_id = 'CARS'
                        and trunc(b1.needed_date) between to_date('2009-01-01', 'YYYY-MM-DD') and to_date('2009-12-31', 'YYYY-MM-DD')
                              group by to_char(b1.needed_date,'YYYY-MM'), b1.type_id) one_query,
                (select to_char(b2.needed_date, 'YYYY-MM') as date_required,
                                SUM(b2.amount_used) as amount_used_b,
                                       b2.type_id as type_id
                                    from table_one b2
                                       where b2.zone_type like
                                       'CHICAGO%'
                                  and b2.type_id = 'BIKES'
                                   and trunc(b2.needed_date) between to_date('2009-01-01', 'YYYY-MM-DD') and to_date('2009-12-31', 'YYYY-MM-DD')
                                 group by to_char(b2.needed_date, 'YYYY-MM'), b2.type_id)second_query
    where one_query.date_required = second_query.date_required(+);the above query is being used on table_one. The current problem I am having is based on the fact that table_one might sometimes contain data for only chicago and not for newyork. in this case, table_one would look like this
    identification_id         needed_date                   zone_type             type_id                  
    2                             3/22/2006 12:00:00          CHICAGO                BIKES
    3                              2/12/2006 12:00:00         CHICAGO                BIKEShowever though, in other case, it could be the other way around. in this case, table_one will look like this
    identification_id         needed_date                   zone_type             type_id                  
    4                            4/21/2007 12:00:00          NEWYORK                CARS
    5                             1/12/2007 12:00:00         NEWYORK                CARS
    and finally table_one could contain information for both cases. hence, we could have the following situation
    identification_id         needed_date                   zone_type             type_id                  
    6                            6/21/2008 12:00:00          NEWYORK             BIKES
    7                            8/12/2008 12:00:00         CHICAGO               CARSKindly note, my above query is currently being used inside a function. I know I can write so many if statement to handle but the main issue is regarding the fact, i am also using that query in another query which performs so many union all.

    I'm not sure how you're going to parameterize it, how those filters change from call to call, but an idea would be something like this:
    select date_required month_id,
           max(amt_used_chicago_bikes) overalluserhours_A,
           max(amt_used_newyork_cars) overalluserhours_B,
           (max(amt_used_chicago_bikes) / max(amt_used_newyork_cars)) * 100 p_change
      from (select zone_type,
                   to_char(b1.needed_date, 'YYYY-MM') as date_required,
                   b1.type_id as type_id,
                   SUM(case when zone_type like 'CHICAGO%' and type_id = 'BIKES'
                                 then b1.amount_used end) as amt_used_chicago_bikes,
                   SUM(case when zone_type like 'NEWYORK%' and type_id = 'CARS'
                                 then b1.amount_used end) as amt_used_newyork_cars
              from table_one b1
             where trunc(b1.needed_date) between
                   to_date('2009-01-01', 'YYYY-MM-DD') and
                   to_date('2009-12-31', 'YYYY-MM-DD')
             group by b1.zone_type,
                      to_char(b1.needed_date, 'YYYY-MM'),
                      b1.type_id)
    where amt_used_chicago_bikes is not null or amt_used_newyork_cars is not null
    group by date_required;Again, this is not the biggest concern regarding performance and certainly not the best way of doing it. Cracking those 250 lines of SQL and making it optimized would probably be the best way to approach the issue here.

  • What is the best solution to the earphones problem?? (Ipod shuffle 3rd gen)

    Hey guys,
    at the end of the day what is the best solution for that problem with the earphones of the new Ipod shuffle? After do some reading through the forums i came up with a couple of solutions that i could probably choose but i am creating that topic as i would like to hear more about your opinions - suggestions.
    The first one of the solutions i thought is that i should go and buy one of these..
    http://www.amazon.co.uk/Scosche-tapSTICK-IRMC-Controls-Shuffle/dp/B002CVTU5Q/ref =wlit_dpo?ie=UTF8&coliid=I10ZELYHA99O08&colid=2OIMEI23BWJ9Z
    What do you guys thing about that? Is that one of the effective solutions?
    And the other option that i was considering is to buy one of this:
    http://www.amazon.co.uk/Scosche-IRM35-tapLINE-Control-Headphone/dp/B002CVTU56/re f=wlit_dpo?ie=UTF8&coliid=I2TP2KR5MGFSTJ&colid=2OIMEI23BWJ9Z
    What do you guys thing about that? Is that one of the effective solutions?
    Well i am looking forward for your replies!!
    Just to let you know i wouldn't consider the glue/tape solution as one of my final choices as in my opinion that is not so good to look at and on the other hand who would like to spend his/hers money to buy something good and then you pour glue on it!! Anyway, to finish, buying a 2nd generation Ipod Shuffle is still one of my solutions to that problem, what do you thing is best for me?
    Regards

    Hey there, first of all let me thank you for your help and advice!!
    furthermore, as i am intending to use my devise in my car as well by using an fm transmitter
    (something like that one: http://www.griffintechnology.com/products/itrip-auto-universal-plus)ALWAYS BEARING IN MIND APPLE'S TWO HUGE MISTAKES:
    1)the controls are on the earphones so i would also need to have one of this http://www.amazon.co.uk/Scosche-tapSTICK-IRMC-Controls-Shuffle/dp/B002CVTU5Q/ref =wlit_dpo?ie=UTF8&coliid=I10ZELYHA99O08&colid=2OIMEI23BWJ9Z (WHAT A MISTAKE!!)
    AND 2)the purchase of one of these tapSTICKs in addition to a new pair of earphones is needed due to that moisture - sweat problem right?
    So what do you guys suggest to me as the best solution to use my ipod in the gym (with all that moisture and sweat!) as well as using it in the car while having the option to change song?
    Thank you in advance!

  • I have iphone 5, after upgrading it to iOS7, front is working find unfortunately rear camera became blurred, what is the best way to fix this? Looking forward to the best solution of this problem.

    I have iphone 5, after upgrading it to iOS7, front is working find unfortunately rear camera became blurred, what is the best way to fix this? Looking forward to the best solution of this problem.

    WORKAROUND FOUND ! Download and install "Awesome Camera" app and take a picture with that app. After 1-2 seconds of standby, it will work. Then you can go back to default Camera app which would work again.Please let me know

  • I have three iPhone 4 models purchased in late 2010 that all share the same iTunes account (apps, music, etc), but I can only update the software on one of the three iPhones. What is the best solution to manage these devices for software updates, apps?

    I have three iPhone 4 models purchased in late 2010 (for my family) that all share the same iTunes account for access to apps and music, but each phone/user still has his/her custom contact list, email accts, and select lists of apps and music from the one iTunes acct library.  The problem is I can only update the software on one of the three iPhones – the primary phone I used to setup the iTunes acct. What is the best solution to manage these devices for software updates, apps? This is probably a common problem with families wanting to share apps and music without realizing the issues created by this approach. As it stands today... the first (primary) iPhone associated with the iTune acct has been updated with the latest software version 5.1.1 and sync'd up with all the apps, music, etc from the one iTune acct, while the other two iPhones are still running on the original software version 4.0.2 and are experiencing problems now. I was advised by AT&T back in January that it would be necessary to setup separate iTunes accts for the second and third iPhones in order to receive system updates. The problem would be how to keep all the apps, etc from being deleted off the second and third phones that were originally loaded from the one iTunes acct. Since this AT&T advice was prior to iCloud coming out, would iCloud be a better solution or at least part of the solution? I really don't yet understand how iCloud works. Hopefully, someone out there can help me??  Please?

    I have three iPhone 4 models purchased in late 2010 (for my family) that all share the same iTunes account for access to apps and music, but each phone/user still has his/her custom contact list, email accts, and select lists of apps and music from the one iTunes acct library.  The problem is I can only update the software on one of the three iPhones – the primary phone I used to setup the iTunes acct. What is the best solution to manage these devices for software updates, apps? This is probably a common problem with families wanting to share apps and music without realizing the issues created by this approach. As it stands today... the first (primary) iPhone associated with the iTune acct has been updated with the latest software version 5.1.1 and sync'd up with all the apps, music, etc from the one iTune acct, while the other two iPhones are still running on the original software version 4.0.2 and are experiencing problems now. I was advised by AT&T back in January that it would be necessary to setup separate iTunes accts for the second and third iPhones in order to receive system updates. The problem would be how to keep all the apps, etc from being deleted off the second and third phones that were originally loaded from the one iTunes acct. Since this AT&T advice was prior to iCloud coming out, would iCloud be a better solution or at least part of the solution? I really don't yet understand how iCloud works. Hopefully, someone out there can help me??  Please?

  • Wat is the best solution when I get DLL file is missing ,is that SFC to run or system restore?

    hi,
    wat is the best solution if I get an error some dll file is missing ,  is that SFC utility to run  or sytem restore  or some other solution?
    thanks
    johan
    h.david

    then it's fine to perform a system restore, but it will revert some changes you have made on your system.
    and detailed infoemation would be helpful.
    Regards
    Yolanda
    TechNet Community Support
    Most error messages relating to missing .dll files are due to virus scanners. The scanner removes the threat (the .dll file) but leaves the program behind that tries to invoke the .dll file. Your recipe of using System Restore to fix the problem could well
    backfire: It might restore the infected .dll file! This is why it is important to know the full wording of the error message. If the missing file resided in the System32 folder then it's probably OK to restore it. If it resided elsewhere then restoration would
    probably be a bad idea.

  • Socket Or RMI for the best solution

    Do me a favor guys...help me to figure this out, which the best solution of this, i create some networks, what i plan is like this..
    if client send their request to appServer which using web browser, the request will arrive to the appServer,right? and then i create some network application to forward the request to another computer to be process, and response the client... the problem are :
    1. which is more efficient, faster,reliabe and secure ,create socket and i put the socket on My RMI or only using socket?
    2. if i combine both RMI and Socket, is that good or not? why?
    Thank u so much for answer....

    Is that much more easier to implement using RMI? why? if i only use Socket,how about the connection,is that slower than RMI or not? and how about the security problem?
    will have problem with RMI if u client behind Firewall or proxy.is that true? how to handle this? how can this could be happen?

  • Sometimes my computer takes too long to connect to new website. I am running a pretty powerful work program at same time, what is the best solution? Upgrading speed from cable network, is it a hard drive issue? do I need to "clean out" the computer?

    Many times my computer takes too long to connect to new website. I have wireless internet (time capsule) and I am running a pretty powerful real time financial work program at same time, what is the best solution? Upgrading speed from cable network? is it a hard drive issue? do I only need to "clean out" the computer? Or all of the above...not to computer saavy.  It is a Macbook Pro  osx 10.6.8 (late 2010).

    Almost certainly none of the above!  Try each of the following in this order:
    Select 'Reset Safari' from the Safari menu.
    Close down Safari;  move <home>/Library/Caches/com.apple.Safari/Cache.db to the trash; restart Safari.
    Change the DNS servers in your network settings to use the OpenDNS servers: 208.67.222.222 and 208.67.220.220
    Turn off DNS pre-fetching by entering the following command in Terminal and restarting Safari:
              defaults write com.apple.safari WebKitDNSPrefetchingEnabled -boolean false

  • SQL Server 2012 - Wat Is The Best Solution For Creating a Read Only Replicated/AlwaysOn Database

    Hi there I was wondering if someone may have a best recommendation for the following requirement I have with regards setting up a third database server for reporting?
    Current Setup
    SQL Server 2012 Enterprise setup at two sites (Site A & Site B).
    Configured to use AlwaysOn Availability groups for HA and DR.
    Installed on Windows 2012 Servers.
    This is all working and failover works fine and no issues. So…
    Requirement
    A third server needs to be added for the purpose of reporting, to be located on another site (Site C) possibly in another domain. This server needs to have a replicated read only copy of the live database from site A or Site B, whichever is in use. The Site
    C reporting database should be as up-to-date to the Site A or Site B database as possible – preferably within a few seconds anyway….
    Solution - What I believe are available to me
    I believe I can use AlwaysOn and create a ReadOnly replica for the Site C. If so do I assume Site C needs to have the Enterprise version of SQL server i.e. to match Site A & Site B?
    Using log shipping which if I am correct means the Site C does not need to be an Enterprise version.
    Any help on the best solution for this would be greatly appreciated.
    Thanks, Steve

    for always on - all nodes should be part of one windows cluster..if there site C is on different domain - I do not think it works.
    Logshipping works --as long as the sql on site C is is same or higher version(sql 2012 or above).  you can only do read only.
    IMHo, if you can make site C in the same domain then, Always is better solution else log shipping
    also, if your database has enterprise level features such as - partitonin, data compression -- you cannot restore the database on lower editions- so you need to have enterprise edition.
    Hope it Helps!!

  • Adobe Acrobat the Best Solution?

    I've never used Adobe Acrobat for anything other than reading PDF files and new to Macs.
    I have a training records for work that I want to go from filling out every week in a binder to the computer. I want to either scan the current form into my MBP (I have a HP 2410 PSC but can't scan through the Airport) and set it up so I can fill out the form every week and save them on he hard drive.
    OR I can create a whole new document but again I need to be able to have fields that I can just fill in every week.
    With the finished product I would like to be able to fill out the form but not be able to change the form itself. (I hope this makes sense)
    Would Acrobat be the best product to use to create this or is there another better product out there for the Mac?

    Numbers and Excel are spreadsheet programs, not database programs. Depending on how simple your needs are, however, either may provide a solution.
    There is also Bento, a very inexpensive database program from Filemaker. There is a free demo, and this may prove a good solution.
    If you already have Adobe Acrobat, it does offer forms functions and would save you from having to but additional software. I have never used the forms functions for actual production (just played around with it). However, you can set up a form, have check boxes, radio buttons, text fields, etc. and then have the entered results saved and/or output for further manipulation.
    Acrobat might prove the best solution for what you are trying to do.

  • [Sales/Shipment report] - What's the best solution?

    Hello everybody.
    I have to develop a report with some sales data, delivery service data and shipment costs data.
    I have the cubes: 0SD_C03, 0SD_C04, 0LES_C02.. and I thought to do a multicube, but I don't no how to link these cubes...
    I don't have, for example, Sales Document/Item and/or Delivery Document/Item in all os these cubes...
    What is the best solution to this sittuation?
    I thought in some solutions like...
    1 - Extend extractor of 0LES_C02 to include Sales Doc/Item, but I don't know if all delivery doc is related to a sales doc.
    2 - I know I have all the fields that I want in the tables VBAP and LIPS...so I thought in create an generic extractor with a view of these two tables, and than creat a Z cube, to receive these datas...
    I really don't know whats the best solution, or if there is abother solution to this....
    I'll apreciate ideas, or a solution from someone that already face with this and developed a solution.
    Best Regards,
    Thiago

    Hi,
    Its better for you to add sales order,item in the cubes also from the source where your cube gets data ie from DSO as they are only point through which you can combine sales,delivery and billing data.
    Thanks,
    Arun

  • What is the best solution for me to run Microsoft Access on my brand new iMAC?  Assume I'm a casual user.

    What is the best solution for me to run Microsoft Access on my brand new iMAC?  Assume I'm a casual user.
    I am lead to believe by some real smart guys on the Apple site that If I have the Apple Store partition my iMAC and add the full suite of Office products on that partition, I can run the few Access programs I have and need to run.
    Comments encouraged.  Thank you in advance for your consideration and help.

    You would have to install Windows, then install Microsoft Office Professional for Windows on it  To install Windows you will have to choose between Boot Camp (faster, free) and a Virtual Machine (simpler, slower, easier to backup)
    You should try LibreOffice (free), it can open Access files, it may not have all Access's feature set though, worth a try.
    www.libreoffice.org

  • What is the best solution for migrating from Maverick to Yosemite?

    What is the best solution for migrating from Maverick to Yosemite? Anyone have suggestions?

    Back up all data. Update all third-party software to the latest version and remove any you don't need. Download the Yosemite installer from the App Store. Run it.

Maybe you are looking for

  • Can two user accounts on the same mac share the Address Book

    My wife and I have separate user accounts on the same Mac but we would like to share the same Address Book. I have searched the forums to no avail. It seems silly to use .mac to do so, and a needless expense when the information is already on the sam

  • Cannot creat PR

    This is Naresh, new to SAP-MM. Iam getting an error while creating PR. The error is "Enter rate EUR/INR rate type M for 3:01:2009 in the system settings". Please anyone clarify my error.

  • How can I install CS2 on my new Mac?

    I gave my ibook to a woman headed for an orphanage in Africa. Before I gave it to her, I asked the tech guy to take off all applications, which he did. I now want to intall CS2 on my new comptuer, a 10.5.7 laptop.  I put in the first disk and clicked

  • Age analysis for customers and vendors

    Hi All, i needs to create an age analysis report on customers and vendors (0FIAR_C03), analysis could be done on <30 days, >30 days,>60,>90,>120 days , i needs to calculate these based between netdue date for payments and system date. any one can giv

  • How to load external storage html file in web view

    hi all,     how to load external storage html file in web view, please help me    " ms-appdata://local/index.html" not working veerasuthan veerakesan