+ Reply to Thread
Page 3 of 4 FirstFirst 1 2 3 4 LastLast
Results 21 to 30 of 31

Thread: Comparing Subsets III

  1. #21
    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

    As large as it may appear, that really is only a Select statement. However, I guess it's possible that your query tool could muck about with it somehow and not make it work. In that case, you could replace the CTE's in my query with Views that you store on the Server. Then, with your query tool, just do that last SELECT..UNION statement.
    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.

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

    Actually, I'm liking the idea of the CTE...the only concern was that I can't start the query with a create table statement to insert the sample data. But I think I can work around it this way. 2 questions though...1. Did the numeric values you chose for the exceptions correspond to my initial sample data? If not, not a big deal, just trying to follow. 2. I'm not sure how I would do a left outer join between tables in your examples of Tables V, N and Mex (calling M in my query). The reason I ask is because in the mutually exclusive table, the primary key will not be equal to the primary key of the V or N tables. Anyway, let me know what you think of this initial CTE. My guess is, I can use this to populate my data then eventually write in your CTE below mine.

    ;WITH

    V (ID, EXCID)
    AS (SELECT * FROM (SELECT 10 AS ID, 1 AS EXCID
    UNION ALL
    SELECT 10 AS ID, 2 AS EXCID) V)
    ,
    N (ID, EXCID)
    AS (SELECT * FROM (SELECT 13 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 13 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 13 AS ID, 4 AS EXCID
    UNION ALL
    SELECT 14 AS ID, 2 AS EXCID
    UNION ALL
    SELECT 14 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 15 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 15 AS ID, 6 AS EXCID
    UNION ALL
    SELECT 17 AS ID, 6 AS EXCID) N),
    M (ID, EXCID1, EXCID2)
    AS (SELECT * FROM (SELECT 21 AS ID, 2 AS EXCID1, 4 AS EXCID2
    UNION ALL
    SELECT 22 AS ID, 1 AS EXCID1, 5 AS EXCID2 ) M)
    SELECT * FROM M, N, V

  3. #23
    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

    1. No, I made all the numbers up for example -- I like to use different ranges for different things so it make following (for me at least) easier.

    2. Indeed, the PK on the M table has nothing to do with the PK on either V or N. In my example the M table has a PK called MExID which can be an AutoIncrement field. ExcID1 and ExcID2 then are FK to the V or N table as necessary.

    Thus to JOIN to the M table the ON clause could/would be
    Code:
     
       On V.ID = M.ExcID1 or V.ID = M.ExcID2
    
    Left Right or Otherwise.
    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. #24
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    K, so my results from
    SELECT * FROM V LEFT OUTER JOIN M ON M.ID=V.EXCID
    would be

    10 1 0 0 0
    10 2 0 0 0

    which would be giving me the visibles and excluding the mutually exclusive...correct?

  5. #25
    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

    Not quite. I screwed up the ON clause in #23. Going back to your query in #22, I think the query you want is:
    Code:
    ;WITH
    
    V (ID, EXCID)
    AS (SELECT * FROM (SELECT 10 AS ID, 1 AS EXCID
    UNION ALL
    SELECT 10 AS ID, 2 AS EXCID) V)
    ,
    N (ID, EXCID)
    AS (SELECT * FROM (SELECT 13 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 13 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 13 AS ID, 4 AS EXCID
    UNION ALL
    SELECT 14 AS ID, 2 AS EXCID
    UNION ALL
    SELECT 14 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 15 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 15 AS ID, 6 AS EXCID
    UNION ALL
    SELECT 17 AS ID, 6 AS EXCID) N),
    M (ID, EXCID1, EXCID2)
    AS (SELECT * FROM (SELECT 21 AS ID, 2 AS EXCID1, 4 AS EXCID2
    UNION ALL
    SELECT 22 AS ID, 1 AS EXCID1, 5 AS EXCID2 ) M)
    -- All Above Same
    Select V.*, M.ID, 1 as Visible
     From V
     Left Join M
       on (V.ExcID = M.Excid1 or V.ExcID = M.Excid2)
    Union
    Select N.*, M.ID, 0 --as NotVisible
      From N
      Left Join M
       on (N.ExcID = M.Excid1 or N.ExcID = M.ExcId2)
    
    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.

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

    Ok...so I think I'm getting it...The results are making sense as is the logic in the query. Now, since all I've done so far is produce the sample data, do I still need to set up the next query below this string of CTE's? What I mean by that is if you call the query we just worked on as the VIEW (even though it's not quite a view) do I still need to run this next query after it to reference the view:

    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


    If so, how could I tie them together?

    Thanks again!

  7. #27
    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

    Sort of, at this point we have combined the V and N tables into 1 table rather than having them separate through the query. After all the previous fun, you should be at the point in my post #17 Comparing Subsets III where you see 'Now the fun begins'

    However, that query is still using the V and N tables with the additional column (MExID I believe). So, to make the remaining query work, substitute the following for V and N (in the query from #26 above that is)
    Code:
    -- V:
       , NEW_V AS (
        Select from <resultsOfPreviousQuery> Where Visible = 1)
    -- N:
      , NEW_N AS (
        Select from <resultsOfPreviousQuery> Where Visible = 0)
    
    That should make it work.
    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. #28
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    I'm sorry, I'm not quite following. I'm fairly new to CTE's...(J introduced me to them this summer). If I divide the query up from beginning to end, do you think you could write in/edit where I'm going wrong? Also, if I'm missing punctuation/commands between the data producing CTE at the beginning and the Select query to return results based on the "view" that would be helpful

    I'll divide it into
    A------------- (first part)
    B---------------(second part)


    A------------- (creates sample data)
    ;WITH
    V (ID, EXCID)
    AS (SELECT * FROM (SELECT 10 AS ID, 1 AS EXCID
    UNION ALL
    SELECT 10 AS ID, 2 AS EXCID) V)
    ,
    N (ID, EXCID)
    AS (SELECT * FROM (SELECT 13 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 13 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 13 AS ID, 4 AS EXCID
    UNION ALL
    SELECT 14 AS ID, 2 AS EXCID
    UNION ALL
    SELECT 14 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 15 AS ID, 3 AS EXCID
    UNION ALL
    SELECT 15 AS ID, 6 AS EXCID
    UNION ALL
    SELECT 17 AS ID, 6 AS EXCID) N),
    M (ID, EXCID1, EXCID2)
    AS (SELECT * FROM (SELECT 21 AS ID, 2 AS EXCID1, 4 AS EXCID2
    UNION ALL
    SELECT 22 AS ID, 1 AS EXCID1, 5 AS EXCID2 ) M)
    -- All Above Same
    Select V.*, M.ID, 1 as Visible
    From V
    Left Join M
    on (V.ExcID = M.Excid1 or V.ExcID = M.Excid2)
    Union
    Select N.*, M.ID, 0 --as NotVisible
    From N
    Left Join M
    on (N.ExcID = M.Excid1 or N.ExcID = M.ExcId2)

    B------------- (sets up the query for query results...what exactly goes here?)

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

    Ok, I got it! (I think )

    I reworked it a bit to put my data in so I can use it. And I switched the "not in" clauses around in the Safe_n and Unsafe N because this way made more sense to me. I also populated a MEX id for everything in N and V. Could you take a look and let me know if you think it's correct? I needed to do a cross join between V and Safe_N because I wanted to see all combinations of Safe_N that can coexist with V. Any suggestions or notes for possible pitfalls will be greatly appreciated...again, thanks for sticking thru this with me...in the end this is going to save a ton of time!

    Code:
    ;WITH
    
    V (ID, EXCID, MEX)
    AS (SELECT * FROM (
    
    SELECT 3 AS ID, 3 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 3 AS ID, 6 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 7 AS ID, 1 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 7 AS ID, 3 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 7 AS ID, 5 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 8 AS ID, 1 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 8 AS ID, 5 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 8 AS ID, 7 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 18 AS ID, 2 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 18 AS ID, 3 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 18 AS ID, 5 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 26 AS ID, 6 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 26 AS ID, 7 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 26 AS ID, 8 AS EXCID, NULL AS MEX) V)
    ,
    
    N (ID, EXCID, MEX)
    AS (SELECT * FROM (
    SELECT 6 AS ID, 1 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 9 AS ID, 3 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 9 AS ID, 5 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 11 AS ID, 2 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 13 AS ID, 5 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 16 AS ID, 1 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 19 AS ID, 2 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 19 AS ID, 5 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 20 AS ID, 6 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 20 AS ID, 7 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 21 AS ID, 3 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 22 AS ID, 5 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 22 AS ID, 7 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 23 AS ID, 7 AS EXCID, 100 AS MEX
    UNION ALL
    SELECT 24 AS ID, 6 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 25 AS ID, 1 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 25 AS ID, 5 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 27 AS ID, 2 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 27 AS ID, 6 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 29 AS ID, 1 AS EXCID, NULL AS MEX
    UNION ALL
    SELECT 29 AS ID, 6 AS EXCID, 200 AS MEX
    UNION ALL
    SELECT 29 AS ID, 7 AS EXCID, 100 AS MEX) N)
    ,
    M (ID, EXCID1, EXCID2)
    AS (SELECT * FROM (
    SELECT 100 AS ID, 3 AS EXCID1, 7 AS EXCID2
    UNION ALL
    SELECT 200 AS ID, 5 AS EXCID1, 6 AS EXCID2 ) M)
    -- All Above Same
    ,
    SAFE_N (ID, EXCID, MEX)
    AS (SELECT ID, EXCID, MEX FROM N WHERE ID IN (SELECT DISTINCT ID FROM N WHERE MEX IS NULL))
    ,
    UNSAFE_N (ID, EXCID, MEX)
    AS (SELECT ID, EXCID, MEX FROM N WHERE ID NOT IN (SELECT DISTINCT ID FROM SAFE_N))
    ,
    OK_N_ID (ID, EXCID, MEX)
    AS (SELECT DISTINCT UNSAFE_N.ID, UNSAFE_N.EXCID, UNSAFE_N.MEX FROM V JOIN UNSAFE_N ON (UNSAFE_N.MEX=V.MEX AND V.EXCID=UNSAFE_N.EXCID))
    ,
    OK_N (ID, EXCID, MEX)
    AS (SELECT * FROM N WHERE ID NOT IN (SELECT ID FROM OK_N_ID))
    
    SELECT V.ID, SAFE_N.ID  FROM V CROSS JOIN SAFE_N
    
    GROUP BY V.ID, SAFE_N.ID
    
    Last edited by Wolffy; November 12th, 2009 at 10:37 AM. Reason: Addded CODE Tags

  10. #30
    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

    If I get a chance today, I'll take a look through it -- but if its working for you, great. I'm literally writing 3 different applications in 3 different languages right now, I think my brain is going to explode.
    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.

+ Reply to Thread
Page 3 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