+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 31

Thread: Comparing Subsets III

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

    Comparing Subsets Revisited

    Wolffy's comments back in August were very helpful in giving me some direction on how to compare results from two query result sets. However, I'm still fairly perplexed. In the interest of trying to completely replicate the problem and provide good sample data, I'm going to provide some aliased sample data that I hope will make more sense than my previous posts.

    I posted on another forum too, but I usually get more polite responses here

    Basically, I’m trying to figure out how to exclude any records that have an ID which are associated with any results based on a specific criterion.

    In the sample data, you’ll see certain records that have an “opposite” nature in the user interface represented by a “#” sign. After providing the sample data, I will show the desired results:


    There are 3 tables:

    Code:
    CREATE TABLE MASTEREXCEPTIONCOMBINATION
    (
    EXCCOMBOID INT NOT NULL,
    VISIBLE TINYINT NOT NULL
    (EXCCOMBOID) PRIMARY KEY)
    
    
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (3,1);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (6,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (7,1);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (8,1);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (9,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (11,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (13,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (16,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (18,1);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (19,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (20,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (21,0);
    
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (22,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (23,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (24,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (25,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (26,1);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (27,0);
    INSERT INTO MASTEREXCEPTIONCOMBINATION (EXCCOMBOID, VISIBLE)
    VALUES (29,0);
    
    Code:
    CREATE TABLE EXCEPTIONCOMBINATION
    (
    EXCCOMBOID INT NOT NULL,
    EXCID INT NOT NULL
    (EXCCOMBOID) PRIMARY KEY)
    
    
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (3,3)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (3,6)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (6,1)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (7,1)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (7,3)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (7,5)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (8,1)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (8,5)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (8,7)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (9,3)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (9,5)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (11,2)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (13,5)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (16,1)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (16,3)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (18,2)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (18,3)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (18,5)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (19,2)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (19,5)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (20,6)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (20,7)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (21,3)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (22,5)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (22,7)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (23,7)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (24,6)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (25,1)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (25,5)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (26,6)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (26,7)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (26,8)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (27,2)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (27,6)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (29,1)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (29,6)
    INSERT INTO EXCEPTIONCOMBINATION (EXCCOMBOID, EXCID)
    VALUES (29,7)
    

    Code:
    CREATE TABLE EXCEPTION
    (
    EXCID INT NOT NULL,
    EXCABBR VARCHAR(8) NOT NULL
    EXCLUSION# INT NOT NULL
    
    (EXCID) PRIMARY KEY)
    
    INSERT INTO EXCEPTION (EXCID, EXCABBR, EXCLUSION#)
    VALUES (1,"MWF",0)
    INSERT INTO EXCEPTION (EXCID, EXCABBR, EXCLUSION#)
    VALUES (2,"T-R",0)
    INSERT INTO EXCEPTION (EXCID, EXCABBR, EXCLUSION#)
    VALUES (3,"PSD",3)
    INSERT INTO EXCEPTION (EXCID, EXCABBR, EXCLUSION#)
    VALUES (5,"CSU",5)
    INSERT INTO EXCEPTION (EXCID, EXCABBR, EXCLUSION#)
    VALUES (6,"#CSU",5)
    INSERT INTO EXCEPTION (EXCID, EXCABBR, EXCLUSION#)
    VALUES (7,"#PSD",3)
    INSERT INTO EXCEPTION (EXCID, EXCABBR, EXCLUSION#)
    VALUES (8,"COPO",0)
    

    I need to use the MASTEREXCEPTIONCOMBINATION table to determine which are the visible vs. invisible EXCEPTIONCOMBINATION. This is what I usually write:

    Code:
    SELECT EXC1.EXCCOMBOID, EXC1.EXCID, EX1.EXCABBR, EX1.EXCLUSION#, EXC2.EXCCOMBOID, EXC2.EXCID, EX2.EXCABBR, EX2.EXCLUSION#
    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)
    
    The resulting data set is:

    3 3 PSD 3 9 3 PSD 3
    3 3 PSD 3 16 3 PSD 3
    3 6 #CSU 5 20 6 #CSU 5
    3 3 PSD 3 21 3 PSD 3
    3 6 #CSU 5 24 6 #CSU 5
    3 6 #CSU 5 27 6 #CSU 5
    3 6 #CSU 5 29 6 #CSU 5
    7 1 MWF 0 6 1 MWF 0
    7 3 PSD 3 9 3 PSD 3
    7 5 CSU 5 9 5 CSU 5
    7 5 CSU 5 13 5 CSU 5
    7 1 MWF 0 16 1 MWF 0
    7 3 PSD 3 16 3 PSD 3
    7 5 CSU 5 19 5 CSU 5
    7 3 PSD 3 21 3 PSD 3
    7 5 CSU 5 22 5 CSU 5
    7 1 MWF 0 25 1 MWF 0
    7 5 CSU 5 25 5 CSU 5
    7 1 MWF 0 29 1 MWF 0
    8 1 MWF 0 6 1 MWF 0
    8 5 CSU 5 9 5 CSU 5
    8 5 CSU 5 13 5 CSU 5
    8 1 MWF 0 16 1 MWF 0
    8 5 CSU 5 19 5 CSU 5
    8 7 #PSD 3 20 7 #PSD 3
    8 5 CSU 5 22 5 CSU 5
    8 7 #PSD 3 22 7 #PSD 3
    8 7 #PSD 3 23 7 #PSD 3
    8 1 MWF 0 25 1 MWF 0
    8 5 CSU 5 25 5 CSU 5
    8 1 MWF 0 29 1 MWF 0
    8 7 #PSD 3 29 7 #PSD 3
    18 3 PSD 3 9 3 PSD 3
    18 5 CSU 5 9 5 CSU 5
    18 2 T-R 0 11 2 T-R 0
    18 5 CSU 5 13 5 CSU 5
    18 3 PSD 3 16 3 PSD 3
    18 2 T-R 0 19 2 T-R 0
    18 5 CSU 5 19 5 CSU 5
    18 3 PSD 3 21 3 PSD 3
    18 5 CSU 5 22 5 CSU 5
    18 5 CSU 5 25 5 CSU 5
    18 2 T-R 0 27 2 T-R 0
    26 6 #CSU 5 20 6 #CSU 5
    26 7 #PSD 3 20 7 #PSD 3
    26 7 #PSD 3 22 7 #PSD 3
    26 7 #PSD 3 23 7 #PSD 3
    26 6 #CSU 5 24 6 #CSU 5
    26 6 #CSU 5 27 6 #CSU 5
    26 6 #CSU 5 29 6 #CSU 5
    26 7 #PSD 3 29 7 #PSD 3

    As I said earlier, the # sign in the Excabbr column indicates that it is opposite to the Excabbr mirror image without the #. So for example #CSU and CSU are opposites. #PSD and PSD are opposites and can not exist in the same subset together.

    So if you look at EXC1.EXCCOMBOID where EXC1.EXCCOMBOID=3, it returns the following:

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

    The problem is that if you look at EXC2.EXCCOMBOID where EXC2.EXCOMBOID=9, it contains an EX2.EXCABBR that equals ‘CSU’. Because it has been marked with that EXCABBR at least once, it needs to be excluded from its association with EXC1.EXCCOMBOID=3. Likewise, I don’t want EXC2.EXCCOMBOID in (20, 29) since they are associated with ‘#PSD’ and/or ‘CSU’.

    So my desired results if I only concentrated on EXC1.EXCCOMBOID=3 would be:

    3 3 PSD 3 16 3 PSD 3
    3 3 PSD 3 21 3 PSD 3
    3 6 #CSU 5 24 6 #CSU 5
    3 6 #CSU 5 27 6 #CSU 5


    I know this is a bit convoluted, I hope it’s making sense! Any help will be greatly appreciated!
    Last edited by micky; November 1st, 2009 at 12:06 PM. Reason: Added code tags, please add them in future

  2. #2
    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, sorry, don't get it. I can't figure out from your post why the records in red are to be excluded from your final results set.
    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. #3
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Follow Up

    Hey Wolffy,

    Thanks...I understand. Let me see if I can show you.

    If I take my earlier queries where I alias the EXCEPTIONCOMBINATION table for visible and invisible then separate them into 2 queries, I get the following:

    Query: Visible Exceptioncombinations

    SELECT * FROM EXCEPTIONCOMBINATIONS EXC1
    JOIN EXCEPTIONS EX1 ON EX1.EXCID=EXC1.EXCID

    WHERE EXC1.EXCCOMBOID IN (SELECT EXCCOMBOID FROM MASTEREXCEPTIONCOMBINATIONS WHERE VISIBLE=1) AND
    EXC1.EXCCOMBOID=3

    Data:
    Exccomboid Excid Excabbr Exclusion#
    3 3 PSD 3
    3 6 #CSU 5


    Invisible Exceptioncombinations:

    SELECT *
    FROM EXCEPTIONCOMBINATIONS EXC2
    JOIN EXCEPTIONS EX2 ON EX2.EXCID=EXC2.EXCID

    WHERE EXC2.EXCCOMBOID IN (SELECT EXCCOMBOID FROM MASTEREXCEPTIONCOMBINATIONS WHERE VISIBLE=0) AND
    EXC2.EXCCOMBOID=9


    Data:
    Exccomboid Excid Excabbr Exclusion#
    9 3 PSD 3
    9 5 CSU 5


    Because the rows in red are opposite (#CSU in the visible set and CSU in the invisible set) EXC2.Exccomboid=9 should be excluded. The visible exceptioncombinations basically act as the Parent and the invisible act as the Child records.

    It's not sufficient to just exclude the rows where the offending records exist, but to completely discount any records associated with that exccomboid. This is because any invisible Exccomboid that contains an opposite excid to an excid in the subset of a Visible Exccomboid must not coexist. In essence, the EXC1.Exccombid=3 is saying, "PSD in Session" "CSU out of session" where
    the EXC2.Exccomboid=9 is saying, "PSD in Session" "CSU in session"

    Does this make more sense?

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

    I suppose it's still not making sense. Any feedback would be greatly appreciated.

    Thanks again!

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

    Comparing Subsets III

    (J, thanks for the feedback, I hope this makes sense, but if not, I have one more idea)

    Ok...I'm woefully deficient in communicating this stuff, so please bear with me. I understand the situation but have a very difficult time articulating it.

    Let me try to talk through it in a word problem.

    PROBLEM:

    I manage a City transit bus service. Now, say I have a bus route that runs depending on the time of year, when school is in or out or other combinations of events.

    Now, say Route 91 makes 2 trips 5 days per week, but depends again on factors such as school schedules etc.

    The normal routing for Route 91 is:

    Leaves @ 8:05 am
    @ 8:25 am

    However, there are certain days it will divert from this schedule. To break it down, we have
    what is called, "exceptions". In order to apply this exceptions on a particular schedule, we have
    what is called, "exceptioncombinations".

    Now there are 2 schools, CSU and PSD. We have the following exeptions:

    CSU (this is when CSU is "in session")
    #CSU (this is when CSU is "NOT in session")
    PSD (this is when PSD is "in session")
    #PSD (this is when PSD is "NOT in session")

    Now, assuming we're looking at a calendar and we know that the week of November 15 that CSU will be Out of session, but PSD will be IN Session. In order to show this and apply it to the trips, we create an exception combination. The exception combination would look like this:

    PSD, #CSU

    There is a unique id created for this exception combination.

    To further complicate things, the scheduler who creates these exception combinations has what is called, "visible"exception combinations that they can choose from or invisible exception combinations that they can not choose from, but actually create by building upon the visible exception comination.

    So for instance, on the week of November 15, they would see the visible combination of ExcComboid=3 which has the ExcID of #CSU and PSD (CSU out of session, PSD in session).

    The challenge is that from time to time, when a Scheduler edits or builds on a visible combination, the system will do what it needs to do correctly, but we have a difficult time reporting on what combinations were used. So if I want to see any possible exceptioncombinations that were "built"on over a visible and thus creating an invisible exceptioncombination, I need to make sure that I'm only showing invisible combinations that CAN coexist with a Visible combination.

    So for example, it is impossible for CSU to be In session and out of session at the same time. Because of that, I do not ever want to see the exccomboid=9 related to the exccomboid=3. This is because exccomboid=3 contains (PSD, #CSU) and exccomboid=9 contains (PSD, CSU). I need to filter out any exccomboid's where there is an excid contained in the combination that opposes an excid in the visible exceptioncombination.

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

    Can you elaborate on the last paragraph some more. I'm sure it's back in you last thread, but how is ID 9 known to be related to ID 3?

    Usually, when there is difficulty in writing a query to solve a problem, it's because the database design is poor or inflexible. I doubt you have ability to change the design any (that would break existing applications, no doubt) but consider writing VIEWs to refactor the data into a more flexible design. For example, prepending a hash to the school ID is rather a poor design -- rather it should be a bit field for inSession/outOfSession. This can be done in a VIEW:
    Code:
    Select
      Case 
        When left(schoolID,1) = '#' then right(schoolID, 3) -- assumes all IDs are 3 chars
        Else schollID
      End as SchoolID
    , Case
        When left(schoolID, 1) = '#' then 1
        Else 0
      End as outOfSession
    
    I'm guess that rethinking the problem into what table design WOULD work, and then doing some VIEWs (Stored Queries in Access) that support the design would make this problem easier.

    I've been busy doing work that I get paid to do, so haven't been able to expend too many brain cells here. However, I think if you/we concentrate on the problem in the last paragraph first, we should be able to sort this. (i.e. chunk it down into smaller bits).
    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. #7
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Hi Wolffy,

    Good feedback. Yes, I understand that it's probably not the most intuitive process. The application is enormous and the company who made the software basically adds onto their product as time goes by. My experience has been that they don't always go through a lot of beta testing to ensure there are no unintended negative consequences when they make these additions.

    Nevertheless, I do need to find a way to work around it. I can use CTE's and Views...but the problem with Views is the front end users won't be able to access them. I can do those things directly in SQL Server, but can not distribute them to the front end users. I can publish reports with CTE's though.

    I've tried some case expressions like the one you demonstrated but it gets a bit dicey. Essentially, I need to figure out a way to apply this filter so that I can do a "crazy" join to another table in order to lay out when drivers are actually supposed to be running a certain trip.

    Going back to your question regarding the EXCCOMBOID=9 and why that one in particular should be filtered out, it's because it is a combination of exceptions. The unique identifer, EXCCOMBOID=9 was created when someone added PSD and CSU as a cominbation in the database. So that exccomboid has two records:

    Exceptioncombination.ExcComboid Exception.ExcID
    9 3 (PSD)
    9 5 (CSU)

    When I do a join between the Visible and Invisible exceptioncombinations,
    it will appropriately filter out the second record because I ask for only records where the visible and invisible exccomboid's have excid's that are equal. So it leaves me with this:

    Exceptioncombination.ExcComboid Exception.ExcID
    9 3 (PSD)

    The only problem with that is that I MUST filter out the Exccomboid completely because that invisible exccomboid can not coexist with the visible exceptioncombination.

    I hope this is starting to make more sense. I have one of my dispatch staff working on a very funny laymen word problem that might make this easier.

    Thanks for bearing with me...and for taking time to assist. We have so many limited resources and reductions in force that this is becoming next to impossible to keep up with

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

    Nevertheless, I do need to find a way to work around it. I can use CTE's but the problem with views is the front end users won't be able to access them. I can do those things directly in SQL Server, but can not distribute them to the front end users.
    OK, I think I'm beginning to understand the whole problem. However, I'm confused about your second paragraph. I think I take this to mean that this is a third-party application and that your front-end users are using this third-party application's user interface and you can't/don't-want-to change that. If that is the case, that is quite understandable. So then, you are trying to (a) reverse engineer the third-party database design (or maybe you even have the third-part schema to work with) and (b) develop a report (and I'll use report of any kind of output to be used by a human) for supervisors/administrators rather than the front-end users.

    If that is this case, then consider this out-of-box idea. Since you mention CTE's, that means you have SQL Server 2005/2008. You could create another database on the same server and use Views to mung up the data (as I mentioned before) by looking back at the original, third-party database. For example (and a really simple one at that):
    Code:
    Use MySecretDatabase
    Go
    Create View MyView As
    Select *
      From ThirdPartyDatabase.dbo.ExceptionCombination
    
    There are some advantages to this; (a) the third-party may (heck probably will) change the design of the database in the future -- and thus break your reporting application and (b) you don't need to add non-third-party code to the third-party database and (c) when it does break, all you need do is change your database views and your reporting application will continue to work.

    Now as to the actual problem at hand -- I don't have a clue yet. At least I know it's not your design. I'll think on it in my spare cycles. One question tho -- does your reporting application need to work with 'real-time' data, or is 'near-time' need acceptable (such as data from yesterday). If the later, then perhaps consider using some stored procedures to mung up the data to make your reporting easier. You can (a)do alot more in a stored procedure than you can in a query and (b)it doesn't matter if it runs for minutes, or even hours, to get the job done.
    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.

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

    Note that I merged the two threads together on this subject so that all the information can be found in one place.
    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. #10
    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

    So if you look at EXC1.EXCCOMBOID where EXC1.EXCCOMBOID=3, it returns the following:

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

    The problem is that if you look at EXC2.EXCCOMBOID where EXC2.EXCOMBOID=9, it contains an EX2.EXCABBR that equals ‘CSU’. Because it has been marked with that EXCABBR at least once, it needs to be excluded from its association with EXC1.EXCCOMBOID=3. Likewise, I don’t want EXC2.EXCCOMBOID in (20, 29) since they are associated with ‘#PSD’ and/or ‘CSU’.
    This is the bit that I still don't see. Specifically the bit that I bolded. Perhaps I don't understand what 'it' refers to. I see the following combinations in the ExceptionCombinationTable:
    Code:
    (3,3)
    (3,6)
    (9,3)
    (9,5)
    
    Now, I take it to mean that the first number identifies the Exception Record, thus we really have 2 exceptions here. The second number is really a Foreign Key into the Exception table, and thus
    Code:
    3 = PSD
    5 = CSU
    6 = #CSU
    
    Now, are you saying that the (9,5) record should be excluded because of the (3,6) record in that CSU and #CSU and mutually exclusive? (if so, which record is considered the 'good' record) And the (9,3) record should be excluded because of the (3,3) record in that both records indicate an ExcAbbr of PSD? (i.e. ID=3)? (again, which is the 'good' record?)
    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 1 of 4 1 2 3 ... 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