+ Reply to Thread
Page 2 of 4 FirstFirst 1 2 3 4 LastLast
Results 11 to 20 of 31

Thread: Comparing Subsets III

  1. #11
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Hi Wolffy,

    Thanks again. A couple things...I will look at implementing your suggestions, though it will probably be a fairly large task to tie into the database through Access etc. The schema is fairly complicated and there are a lot of things to consider.

    In terms of what is "it" in my example, sorry if it was confusing. In the bold letters, "It" refers to the invisible exceptioncomboid=9. I need to figure out a way to say that whenever a visible exceptioncombination exists, exclude the primary key of the invisible exceptioncombination that has a foreign key (excid) that is mutually exclusive to the foreign key (excid) of the visible exceptioncombination.

    The desired query would look like this:

    SELECT EXC1.EXCCOMBOID, EXC1.EXCID, EX1.EXCABBR,
    EXC2.EXCCOMBOID, EXC2.EXCID, EX2.EXCABBR
    FROM EXCEPTIONCOMBINATION EXC1
    JOIN EXCEPTION EX1 ON EX1.EXCID=EXC1.EXCID
    LEFT OUTER JOIN EXCEPTIONCOMBINATION EXC2 ON EXC2.EXCID=EXC1.EXCID
    LEFT OUTER JOIN EXCEPTION EX2 ON EX2.EXCID=EXC2.EXCID

    WHERE
    EXC1.EXCCOMBOID IN (SELECT EXCCOMBOID FROM MASTEREXCEPTIONCOMBINATION WHERE VISIBLE=1) AND
    EXC2.EXCCOMBOID IN (SELECT EXCCOMBOID FROM MASTEREXCEPTIONCOMBINATION WHERE VISIBLE=0) AND
    CONDTION_THAT_EXCLUDES_PRIMARY_KEY_FROM_INVISIBLE_ EXCCOMBO_WHERE_FOREIGN_KEY_(EXCID)_IS_MUTUALLY_EXC LUSIVE_TO_FOREIGN_KEY_(EXCID)_OF_VISIBLE_EXCCOMBO



    I know this looks a bit silly but I thought it might help to show what I'm talking about.

    The desired outcome for this query would be:

    3 3 PSD 9 3 PSD
    3 3 PSD 16 3 PSD
    3 6 #CSU 20 6 #CSU
    3 3 PSD 21 3 PSD
    3 6 #CSU 24 6 #CSU
    3 6 #CSU 27 6 #CSU
    3 6 #CSU 29 6 #CSU
    Attached Images

  2. #12
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Scary thing is, I think I understand your second paragraph

    So, let's see if I get it. Looking at your screen shot then, since the duple (18,2) is the 'visible' duple, the other duples (n, 2) that are 'invisible' should be excluded from the selection -- i.e. (9, 2), (11, 2), (27,2) etc.

    Further, there are 'related' values for y in the duple (x,y), specifically (5 and 6) and (3 and 7) which are mutually exclusive. Therefore if (a, 3) is the visible duple, then duples of (b, 7) should be excluded regardless of visibility.

    Closer?
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  3. #13
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Ooops, not quite. By my logic I see why the (9,3) duple is excluded, but not the (16, 3).
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  4. #14
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Hi Wolffy,

    I think we're getting closer, but not quite. First of all, I forgot to mention that the screenshot is the result set of a query where I don't re-alias the exceptioncombination table to be visible and invisible, but I think you got that.

    Regarding the duple (18,2), it's a bit off track. Actually, I would want to see
    see the primary key of all combinations where y was not mutually exclusive to y of (18,2). So (11,2) would actually work because y in both examples is "T-R" and can coexist.

    For the exccomboid 3 (subsets (3,3) and (3,6)) exccomboid 9 can not coexist. Because the second duple of exccomboid 9 ((9,3) and (9,5)) is mutually exclusive to one of the duples of exccomboid 3, the entire exccomboid 9 can not relate to exccomboid 3.

    Really, if you think of the exccomboid's as a combination rather than a single record, I think it might make sense. In essence, a combination inherently will have a string of numbers related to it. If one of the numbers in the string does not jive with one of the numbers within the combination of another set, then it can't coexist.

    Even though the combinations can be broken down to distinct records with one record relating to a specific exception, they really must be looked at as one whole combination. So going back to one of my initial assertions, if exccombid 9 refers to a time period when PSD is in session and CSU is in session, then the whole combination can NOT exist with exccomboid 3 because exccomboid 9 has a mutually exclusive exception ('CSU').

    My brain hurts now

  5. #15
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Wolffy,

    I re-read your second paragraph and it is close. I would add that any visible exceptioncombo is mutually exclusive of any other visible exceptioncombo. This is handled, however, by my aliasing of the exceptioncombination table (ie
    EXC1.EXCCOMBOID IN (SELECT EXCCOMBOID FROM MASTEREXCEPTIONCOMBINATION WHERE VISIBLE=1) AND
    EXC2.EXCCOMBOID IN (SELECT EXCCOMBOID FROM MASTEREXCEPTIONCOMBINATION WHERE VISIBLE=0) )

    Also, I would add that if (a,3) is one of the visible duples on a visible combination, then any invisible combinations that have one instance of (b,7) must be excluded. So if there is an exccombo with (b,7) and (b,6) for example, then B would be excluded all together.

  6. #16
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    It's too dang early to grock this -- I need coffee. But I think I see where we are going. 'fraid I may have to dust off my set theory of college -- where I studied under Euclid. I thought some more about this on the way home last night (I must get a life) and we are on the same page in that the set of Visible and Not-Visible items is really a set of sets and thus we are interested in a result were is a member of the subset is excluded, then the entire set must be excluded.

    Now, if I fully understand, the incompatible members are CSU and #CSU, and PSD and #PSD -- so if A(m) = {{a,CSU}, {a, PSD}} and B(n) = {{b, #CSU}, {b,PSD}} then our result set would not include any member of B(n).

    Having established that, let consider only the members of A (i.e. the visible set). Disregarding B (i.e the invisible set) would there be an condition under which a member of A would be excluded (i.e. would there be a case where in A where A(n) = {{x, CSU}} and A(m) = {{y, #CSU}}).

    Also, are there any other exclusive pairs besides CSU and PSD -- such as MWF and T-R?

    Sorry to ask so many questions, but it helps to get to an understanding of the problem.

    Oh, and so I'm sure I'm getting it, the sets A={{3, PSD}, {3, #CSU}} and B={{16, MWF}, {16, PSD}} are not mutually exclusive since neither member of B is mutually exclusive of A (PSD can 'live' with MWF and PSD, and #CSU can 'live' with MWF and PSD)

    Ow -- my head hurts too.
    Last edited by Wolffy; November 10th, 2009 at 10:32 AM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  7. #17
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    And one more thought.

    You might consider creating a table/view/CTE/stored query that contains all of the mutually exclusive ExcIDs. I can see (at the moment) that this table could be very useful when trying to determine which members belong in the excluded set. For example. this table would contains the rows {(3,7), (5,6)} and probably others.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  8. #18
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Hi Wolffy,

    Yes that's it Finally!!! lol...I have such a hard time describing this stuff but you hit it on the head with paragraphs 2 and 3 of your first post today.

    Regarding your question as to whether or not the Visible combo should be excluded, theoretically yes, but I should always be using the visible aliased table as the left table in the join as I want to see the visible results and any invisible results that correllate to it. (does that make sense?)

    As for the other scenarios that could exist which are mutually exclusive, yes, you're correct, T-R and MWF are mutually exclusive of eachother. I chose to leave that out because I was having a difficult enough time conveying the CSU v #CSU and PSD v #PSD idea...so it would be applicable to that other set as well. Right now (meaning unless we create other exceptions) the ones that can be mutually exclusive are:

    PSD <> #PSD
    CSU <> #CSU
    T R <> MWF

    I love the idea of setting up a CTE because then I can use the results to reference the "legal" primary keys in my main query. I was dreading the idea of using derived tables because my query already has 20 tables referenced in my main query. (transit is extremely complicated)

    Thank you so much for working on this, I realize you're spending a lot of time on it and I very much appreciate it. I'm not trained in IT, I've just had to learn it (which I love!) because of the severe limitations of resources we have here.

    I'd offer to provide your coffee, but I don't think you'd want ours If you follow Southpark at all and go back to the Harbuck's episode where Tweak's father describes his coffee something like this, "It's warm, rich blend with notes of pear and orchids" However, in reality, it tastes like mud, so you might want to stick with your Harbuck's

    Thanks again! Any help setting up a CTE would be immensely helpful!!!!

  9. #19
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    OK, bear with me here, this is a big query. In thinking on this, I believe the key is the Mutually Exclusive Pairs table -- and we need to setup a Primary Key field on this table. Now, I think we can look at the Visible and Not Visible data as separate tables, logically or physically it doesn't really matter. Let's call these V and N

    Now, consider the following sample data:
    Code:
    Table: V
    ID--ExcID
    10  1
    10  2
    
    Table: N
    ID--ExcID
    13  3
    13  4
    14  2
    14  3
    15  3
    15  6
    17  6
    
    Now, let's assume the following exclusive pair: (1,5) and (2, 4). These would go into a table thus:
    Code:
    Table: M
    ID--ExcID1--ExcID2
    21       2       4
    22       1       5
    
    Now, we can 'pad out' the V and N table to include the ID value from the M table by doing an outer join -- the resulting sets being:
    Code:
    Table: V
    ID--ExcID--MExID
    10      1     22
    10      2     21
    
    Table: N
    ID--ExcID--MExID
    13      3   Null
    13      4     21
    14      2     21
    14      3   Null
    15      3   Null
    15      6   Null
    17      6   Null
    
    Now the fun beings. We can build the following CTE. First there are a number of records in the N table that we can consider 'SAFE' in that they don't contain any ExcID that have a MutEx pair -- in this case 15 and 17.
    Code:
    With SAFE_N as (
      Select ID, ExcID, MExID
        From N
        Where ID Not IN (Select Distinct ID From N Where MexID is not null)
    
    this gives us the records (15, 3), (15, 6) and (17, 6).

    Next we need to get the other records from N, and let's call this UNSAFE_N
    Code:
    With UNSAFE_N as (
      Select ID, ExcID, MExID
        From N
        Where ID Not IN (Select Distinct ID From SAFE_N)
    )
    
    This gives us (13, 3), (13, 4), (14, 2) and (14, 3)

    Now in those UNSAFE records, some should be excluded (13 in this case) while some really are OK (14). Here 14 is OK because (10, 2) and (14, 2) are compatible since they have the SAME ExcID. So, let's get the ID's of the UNSAFE records that are really OK to be included:
    Code:
    With OK_N_ID as (
        select distinct UNSAFE_N.ID from V
        JOIN UNSAFE_N
            On (UNSAFE_N.MexID = V.MExID And V.ExcID = UNSAFE_N.ExcID)
    )
    
    This returns 14.

    Using the results of the previous CTE, we can get the records from UNSAFE that are indeed OK to include:
    Code:
    With OK_N as (
        Select * from N
            Where ID In (Select ID from OK_N_ID)
    )
    
    Finally, we can put it all together to get our final results:
    Code:
    Select * from V
    UNION
    Select * from OK_N
    UNION
    Select * from SAFE_N
    
    Which with the sample data yields:
    Code:
    10    1    22
    10    2    21
    14    2    21
    14    3    NULL
    15    3    NULL
    15    6    NULL
    17    6    NULL
    
    Now, this doesn't address the problem of MutEx pairs within the V table, but should be most of the work. To scrub the V table, it would probably be similar to the above.

    I'm sure this isn't the perfect solution, but it is another way to come at the problem. Yes, the query is ugly, but if there isn't too much data, it shouldn't run too long.

    Putting the above together into a query:
    Code:
    With SAFE_N as (
      Select ID, ExcID, MExID
        From N
        Where ID Not IN (Select Distinct ID From N Where MexID is not null)
    )  
    , UNSAFE_N as (
      Select ID, ExcID, MExID
        From N
        Where ID Not IN (Select Distinct ID From SAFE_N)
    )  
    , OK_N_ID as (
        select distinct UNSAFE_N.ID from V
        JOIN UNSAFE_N
            On (UNSAFE_N.MexID = V.MExID And V.ExcID = UNSAFE_N.ExcID)
    )   
    , OK_N as (
        Select * from N
            Where ID In (Select ID from OK_N_ID)
    )
    Select * from V
    UNION
    Select * from OK_N
    UNION
    Select * from SAFE_N
    
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  10. #20
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Hi Wolffy,

    I think I follow, actually doesn't seem as bad as I thought it would be .

    Here's the deal though...If I write the query on SQL Server itself, I can create views and/or temp tables.

    The problem is I can't publish them to the staff. I can only use a query writer that is read only; it doesn't allow any insert/create/delete/alter/update queries at all.

    When I'm updating/deleting records, I have to do it on SQL server itself.

    One thing I can do in the query writer (and yes, I realize it is fairly silly looking) is to do something like this:

    SELECT * FROM (SELECT 1 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 2 AS ID, 5 AS EXCID) V
    UNION ALL
    SELECT * FROM (SELECT 2 AS ID, 7 AS EXCID
    UNION ALL
    SELECT 5 AS ID, 8 AS EXCID) N

    But I don't know how that would work within the CTE. I guess I could start with something like this:

    WITH MYCTE (ID, EXCID)
    AS (SELECT * FROM (SELECT 1 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 2 AS ID, 5 AS EXCID) V
    UNION ALL
    SELECT * FROM (SELECT 2 AS ID, 7 AS EXCID
    UNION ALL
    SELECT 5 AS ID, 8 AS EXCID) N)

    SELECT * FROM MYCTE

    It seems like that might get sticky (I realize the numbers I chose don't correlate to anything you wrote...just sample)

    So I guess my idea is that if I can populate the sample data that way, then I can go back and reference it (sample data) throughout the rest of the cte.

    What do you think?

+ Reply to Thread
Page 2 of 4 FirstFirst 1 2 3 4 LastLast

Similar Threads

  1. Comparing Subsets Revisited
    By Flam in forum SQL Development
    Replies: 3
    Last Post: November 5th, 2009, 02:07 AM
  2. Replies: 3
    Last Post: April 12th, 2009, 11:59 PM
  3. comparing records
    By javier_83 in forum Microsoft Access
    Replies: 10
    Last Post: August 7th, 2008, 02:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO