A complicated query , group by issue (I guess)

Hi , on a bad structured database 10g running on Windows XP , I have two tables , one for the clients paid money and the other is for clients invoices , I need to calculate the sum of invoices of one client - which identified by his FirstNAme and LastName minus the sum of his paid money beside oney that already paid when making the invoice for example x buy an item , he made an invoice ith 100$ , he paid immediately 10$ nad then later - this will be on the other table - he paid 40$ , so my final result should be 50$ ,all in respect to the same year, here is what I did
SELECT NVL(SUM(NORMAL_CLIENTS.INV_VALUE), 0) - (NVL(SUM(NORMAL_CLIENTS.PAID), 0) + NVL(SUM(NORMAL_CLIENTS_RECEIPTS.REC_VALUE), 0))
AS EXPR1, NORMAL_CLIENTS.NAME, NORMAL_CLIENTS.SURNAME
FROM NORMAL_CLIENTS INNER JOIN
NORMAL_CLIENTS_RECEIPTS ON NORMAL_CLIENTS.NAME = NORMAL_CLIENTS_RECEIPTS.NAME AND
NORMAL_CLIENTS.SURNAME = NORMAL_CLIENTS_RECEIPTS.SURNAME
WHERE (TO_CHAR(NORMAL_CLIENTS.INV_DATE, 'YYYY') =
(SELECT DATE3
FROM TEMP)) AND (TO_CHAR(NORMAL_CLIENTS_RECEIPTS.REC_DATE, 'YYYY') =
(SELECT DATE3
FROM TEMP TEMP_1))
GROUP BY NORMAL_CLIENTS.NAME, NORMAL_CLIENTS.SURNAME
so this worked for me in one condition , the client has to have records on both table , otherwise it returns null
any help will be much appreciated

Hi,
So that we can help you, can you provide us with some DDLs to create the tables, and some INSERTs to populate them with representative data?
You don't need to give the full structure of your tables, just relevant columns for the problem.
Also, to preserve formatting and readability, please use the tag to enclose code snippets.
Thanks.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • SQL Query Group By Issues - Urgent

    I currently have an issue writing a pl\sql report, I can get part of the way to the results I want but the group by clause is causing problems, because I have to add more columns to the group by, dispersing the figures further, I have tried it with coalesce for each of the task types but I still get the same results, I am getting close to the results I need but not quite there yet. I would really appreciate it if someone could take at look at this for me as it is an urgent requirement.
    The report is based on the tables similar to the following:
    TASKS, ORGANISATIONS, POSITIONS
    A position is a member of an organisation.
    A task has a position assigned to it.
    The SQL for the tables and to insert the data that would produce the report is detailed below:
    CREATE TABLE TASKS
    (     TASK_ID NUMBER NOT NULL ENABLE,
         TASK_TYPE VARCHAR2 (15 BYTE) NOT NULL ENABLE,
         STATUS VARCHAR2 (15 BYTE) NOT NULL ENABLE,
         POS_ID NUMBER NOT NULL ENABLE,
         CONSTRAINT TASKS_PK PRIMARY KEY (TASK_ID));
    CREATE TABLE ORGANISATIONS
    (     ORG_ID NUMBER NOT NULL ENABLE,
         ORG_NAME VARCHAR2 (15 BYTE) NOT NULL ENABLE,
         CONSTRAINT ORGANISATIONS_PK PRIMARY KEY (ORG_ID));
    CREATE TABLE POSITIONS
    (     POS_ID NUMBER NOT NULL ENABLE,
         POS_NAME VARCHAR2 (25 BYTE) NOT NULL ENABLE,
         ORG_ID NUMBER NOT NULL ENABLE,
         CONSTRAINT POSITIONS_PK PRIMARY KEY (POS_ID));
    INSERT INTO ORGANISATIONS (ORG_ID, ORG_NAME) VALUES (1,'ABC');
    INSERT INTO ORGANISATIONS (ORG_ID, ORG_NAME) VALUES (2,'DEF');
    INSERT INTO ORGANISATIONS (ORG_ID, ORG_NAME) VALUES (3,'EFG');
    INSERT INTO POSITIONS (POS_ID, POS_NAME, ORG_ID) VALUES (1,'ABC-1', 1);
    INSERT INTO POSITIONS (POS_ID, POS_NAME, ORG_ID) VALUES (3,'ABC-2', 1);
    INSERT INTO POSITIONS (POS_ID, POS_NAME, ORG_ID) VALUES (2,'ABC-3', 1);
    INSERT INTO POSITIONS (POS_ID, POS_NAME, ORG_ID) VALUES (5,'DEF-2', 2);
    INSERT INTO POSITIONS (POS_ID, POS_NAME, ORG_ID) VALUES (4,'DEF-1', 2);
    INSERT INTO POSITIONS (POS_ID, POS_NAME, ORG_ID) VALUES (7,'EFG-1', 3);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (12,'TASK_TYPE_3','LIVE',3);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (11,'TASK_TYPE_2','LIVE',3);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (10,'TASK_TYPE_2','LIVE',2);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (9,'TASK_TYPE_2','LIVE',2);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (8,'TASK_TYPE_1','LIVE',3);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (7,'TASK_TYPE_1','LIVE',3);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (6,'TASK_TYPE_1','LIVE',3);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (5,'TASK_TYPE_1','LIVE',3);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (4,'TASK_TYPE_1','LIVE',2);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (3,'TASK_TYPE_3','LIVE',1);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (2,'TASK_TYPE_1','LIVE',1);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (1,'TASK_TYPE_1','LIVE',1);
    INSERT INTO TASKS (TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (13,'TASK_TYPE_3','LIVE',3);
    The report should detail the following information based on the information in the tables:
    1st Column
    Organisation
    ABC
    DEF
    EFG
    2nd Column
    No. of Positions in Organsiation
    3
    2
    1
    With total of the number of people in all of the organisation 6
    3rd Column
    Number of tasks assigned to the organisation of task type1
    2
    1
    4
    4th Column
    Number of tasks assigned to the organisation of task type 2
    0
    2
    1
    5th Column
    Number of tasks assigned to the organisation of task type 3
    1
    0
    2
    Total no of tasks assigned to the Organisation
    3
    3
    7
    Message was edited by:
    Suzy_r_82
    Message was edited by:
    Suzy_r_82

    Hi,
    Apologies, my insert statements where incorrect, if you try the data below instead it should give you output I was expecting
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (1,'TASK_TYPE_1', 'LIVE',1);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (2,'TASK_TYPE_1', 'LIVE',2);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (3,'TASK_TYPE_1', 'LIVE',5);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (4,'TASK_TYPE_1', 'LIVE',7);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (5,'TASK_TYPE_1', 'LIVE',7);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (6,'TASK_TYPE_1', 'LIVE',7);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (7,'TASK_TYPE_1', 'LIVE',7);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (8,'TASK_TYPE_2', 'LIVE',4);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (9,'TASK_TYPE_2', 'LIVE',5);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (10,'TASK_TYPE_3', 'LIVE',1);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (11,'TASK_TYPE_3', 'LIVE',7);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (12,'TASK_TYPE_2', 'LIVE',7);
    INSERT INTO TASKS( TASK_ID, TASK_TYPE, STATUS, POS_ID) VALUES (13,'TASK_TYPE_3', 'LIVE',7);
    The results I would like are
    ORG    No. of Pos     No of Task    No of Task     No of Task     Total no
               In Org           Type1 for      Type2 for        Type3 for       of Tasks
                                   Org             Org                Org               for Org
    ABC        3                   2                 0                   1                   3
    DEF        2                   1                 2                   0                   3
    EFG        1                   4                 1                   2                   7
    Total        6The results I get are multiple lines for each organisation, I would like to rollup these lines so I can have one line per organisation.
    Hope this helps a bit more, I appreciate the help, let me know if you need more information
    Thanks
    Suzy

  • Multi-query group above report creates more pages

    Hi,
    I have a multi-query group above report (paper only), the parent group creates 5 rows(subframes) all onto the same page, but then creates 4 more IDENTICAL pages!!?
    at the end I have 5 repating frames and 5 pages.
    If I set Maximum Records per Page to 1, I have 5 pages (IDENTICAL) with the first frame only...
    any idea?
    cheers
    Matteo

    hello,
    you will have to create a counter, that tells you the numbers of students (summary-column, function : count, reset on : course) and create a format-trigger on the heading that hides it when the number of students is 0.
    regards,
    the oracle reports team --pw                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Query Group in Query Category different with Authorization's group number

    Hi All,
    In Authorization screen, it allowed to authorize up to 20 query group
    Whereas in Query Category window, it has only max 15 query group.
    Anyone has idea on why is the difference?
    Thanks in advance,
    MH

    You have a very good point.  I believe something must be for system to use during development.  You may post a development request to see if the next version could have matched numbers of query groups.  The forum is on this link:
    /community [original link is broken]
    Thanks,
    Gordon
    Edited by: Paulo Calado on Jun 19, 2009 3:33 PM

  • Query Group and Authorization group is different

    Hi,
    In Authorization screen, it allowed to authorize up to 20 query group. Whereas in Query Category window, it has only max 15 query group. For future patch, is it possible to have matched query groups?
    Thanks.
    Regards,
    MH

    Hi,
    In Authorization screen, it allowed to authorize up to 20 query group. Whereas in Query Category window, it has only max 15 query group. For future patch, is it possible to have matched query groups?
    Thanks.
    Regards,
    MH

  • Adobe Error 150:30    Its a licensing issue I guess...how do I fix this?

    I just transferred CS4 onto my new mac but its telling me there is an "Error 150:30"   Its a licensing issue I guess...how do I fix this so I can use the program on my new mac? Is it even possible to fix? I don't think I have the physical software anymore its been so long since I have had this program!

    Klizb this often occurs when Adobe Creative applications are copied/transferred/migrated to a different computer.  Please make sure to run any available uninstallers followed by the use of the CC Cleaner Tool to attempt to recover from the transfer process.  You can find details on how to use the CC Cleaner Tool at Use the CC Cleaner Tool to solve installation problems | CC, CS3-CS6.
    If you need to download a fresh copy of the retail installation files they are available at Download CS4 products.

  • Linking Query group to an SAP role

    I am able to link the Query group to the role but when we test the user does not have access to it. I know this used to be a problems years ago that I thought was fixes. Any ideas on how to get this to work?

    Hi,
    Assign the required user groups to the user in Sq03. if user still getting same error even after assignments in Sq03, ask user to please change query areas as below and check.
    Sq01 --> Environment --> Query areas --> select "Stanard area (client-specific).
    Regards,
    Gowrinadh

  • Configure custom query group in navigation menu

    How do we configure custom query groups? Say for example: I have created a custom query group and added my custom query definitions to it. How do I configure this so that when I click on Contract Management Tab on the workbench, my custom query group is invoked?
    I need to configure custom query group in navigation menu. Any help would be appreciated.
    Regards,
    Bindu Sharma

    Hi Bindu,
    As per my understanding, it is standard and query group cannot be added in the navigation but you can add your quries under Agreement and Claus Library.
    Kindly refer the blog for the same: http://scn.sap.com/docs/DOC-55733
    Let me know if you need any other information.
    Thanks,
    Kushagra A

  • Saved query groups authorization

    Dear All,
    Saved query groups - no.1 to no. 20 are available in the authorizations form.
    I see there are only 15 groups  in the query manager --> manage categories, and in the authorizations are 20. the remnants are 5. Where will the five groups to be used and be able to find ?
    I use SBO 2004A. I also see that it is happened in SBO 2005A. I have tried to find in SAP notes but I can't see.
    I appreciate your answers. TIA
    Rgds,

    Hi Steve,
    Seems that noone here on SDN knows the answer (including myself)
    I suggest that you raise that to SAP Support - to have them checking the case.
    Sorry,
    Frank

  • Easiest way to know the query group name?

    Hello All,
    This has been nagging at me for quite some time now. Would appreciate you inputs on the ways to find out the query group name for a set of queries we see on the UI.
    The obvious method is to search your query group from the list of query groups that system shows when we navigate to Setup->Queries and Reports->Query Groups. Do we have some other ways as well.
    Thanks In Advance
    Devesh

    Hi Devesh,
    If you know the query definition then the easiest way to find the query group would be to use "Search Query Groups by Queries Used" query on the Query Groups list page. This query allows you to select a query definition and returns the query group using the query.
    Or you could use "Query Group Review" query on the Query Groups list page. This returns all queries within all Query Groups. You can further refine your search by Query Name or Internal Name of the query you are interested in.
    If you want to do a general search for all buy-side list page or buy-side picker page query groups, search for <buylist> and <buypick> respectively.
    Hope this helps.
    Regards,
    Vikram

  • To enable filtering the Purchase Orders based on Purchasing Group in Issue

    Hi All,
    We are facing one issue
    Currently we are working with Extended Classic Scenario with SRM 5.0 and support pack 11
    If the buyer uses Issue PO transaction in SRM, there are POs from all the buyers.
    There is no filter to allow the buyer to display only POs relevant for his/her Purchasing group.
    To enable filtering the Purchase Orders based on Purchasing Group in Issue Purchase Order transction, we found one
    OSS note : 1162884 - BBP_PPF: Purchasing group as search criteria for PO
    But we found that this oss note is not applicable for our system version
    Could you please help me to resolve this issue by any suggestion or by any other oss note?
    Thanks
    Snehal

    Hi Snehal,
    There is a easy way to do that.
    Go to buyer role in PFCG transaction and look for profile and go inside profile ...look for your transaction...
    for Process PO  transaction -  BBP_PD_PO and you have field BBP_PURGRP...using which you can restrict it on purchasing group..
    You may have to copy same role and create new roles based on purchasing group..
    I feel that basis or Security and Authorization team can help you in this matter.
    Regards,Nishant

  • SQL Query with a little bit more complicated WHERE clause performance issue

    Hello, I have some performance issue in this case:
    Very simplified query:
    SELECT COUNT(*) FROM Items
    WHERE
    ConditionA OR
    ConditionB OR
    ConditionC OR ...
    Simply I have to determine how many Items the user has access through some complicated conditions.
    When there is a large number of records (100,000+) in the Items table and say ~10 complicated conditions concatenated in WHERE clause, I get the result about 2 seconds in my case. The problem is when very few conditions are met, f.e. when I get only
    10 Items from 100,000.
    How can I improve the performace in this "Get my items" case?
    Additional information:
    the query is generated by EF 6.1
    MS SQL 2012 Express
    Here is the main part of the real SQL Execution Plan:

    Can you post table/index DDL?  Query?
    Sample query:
    exec sp_executesql N'SELECT
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT
    COUNT(1) AS [A1]
    FROM [dbo].[Tickets] AS [Extent1]
    LEFT OUTER JOIN [dbo].[Services] AS [Extent2] ON [Extent1].[ServiceId] = [Extent2].[Id]
    WHERE (@p__linq__0 = 1) OR ([Extent1].[SubmitterKey] = @p__linq__1) OR ([Extent1].[OperatorKey] = @p__linq__2) OR (([Extent1].[OperatorKey] IS NULL) AND (@p__linq__2 IS NULL)) OR ([Extent1].[SolverKey] = @p__linq__3) OR (([Extent1].[SolverKey] IS NULL) AND (@p__linq__3 IS NULL)) OR ([Extent1].[Incident2ndLineSupportKey] = @p__linq__4) OR (([Extent1].[Incident2ndLineSupportKey] IS NULL) AND (@p__linq__4 IS NULL)) OR ((@p__linq__5 = 1) AND ((1 = CAST( [Extent1].[TicketType] AS int)) OR ((@p__linq__6 = 1) AND (((2 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[IncidentManager] = @p__linq__7) OR (([Extent2].[IncidentManager] IS NULL) AND (@p__linq__7 IS NULL)))) OR ((3 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[ServiceRequestManager] = @p__linq__8) OR (([Extent2].[ServiceRequestManager] IS NULL) AND (@p__linq__8 IS NULL)))) OR ((4 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[ProblemManager] = @p__linq__9) OR (([Extent2].[ProblemManager] IS NULL) AND (@p__linq__9 IS NULL)))) OR ((5 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[ChangeManager] = @p__linq__10) OR (([Extent2].[ChangeManager] IS NULL) AND (@p__linq__10 IS NULL)))))) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceDeputyManagers] AS [Extent3]
    WHERE ([Extent1].[ServiceId] = [Extent3].[ServiceId]) AND ( CAST( [Extent3].[TicketType] AS int) = CAST( [Extent1].[TicketType] AS int)) AND ([Extent3].[UserProviderKey] = @p__linq__11)
    )))) OR ((2 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsIncidents] = 1) OR ((201 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableIncidentTakeFromQueueAction] <> cast(1 as bit)))) AND ([Extent2].[Incident1stLineSupportLimitedAccess] <> cast(1 as bit))) OR ((3 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsServiceRequests] = 1) OR ((301 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableServiceRequestTakeFromQueueAction] <> cast(1 as bit)))) AND ([Extent2].[ServiceRequestLimitedAccess] <> cast(1 as bit))) OR ((4 = CAST( [Extent1].[TicketType] AS int)) AND ([Extent2].[AllowAccessToOtherOperatorsProblems] = 1) AND ([Extent2].[ProblemLimitedAccess] <> cast(1 as bit))) OR ((5 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsChanges] = 1) OR ((501 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableChangeTakeFromQueueAction] <> cast(1 as bit)))) AND ([Extent2].[ChangeLimitedAccess] <> cast(1 as bit))) OR ((2 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsIncidents] = 1) OR ((201 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableIncidentTakeFromQueueAction] <> cast(1 as bit)))) AND (( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent4]
    WHERE ([Extent1].[ServiceId] = [Extent4].[ServiceId]) AND (2 = CAST( [Extent4].[TicketType] AS int)) AND ([Extent4].[UserProviderKey] = @p__linq__12) AND (1 = [Extent4].[SupportLine])
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent5]
    WHERE ([Extent5].[UserProviderKey] = @p__linq__13) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent6]
    WHERE ([Extent6].[ServiceId] = [Extent1].[ServiceId]) AND (2 = CAST( [Extent6].[TicketType] AS int)) AND (1 = [Extent6].[SupportLine]) AND ([Extent6].[OperatorGroupId] = [Extent5].[OperatorGroupId])
    )))) OR ((2 = CAST( [Extent1].[TicketType] AS int)) AND ([Extent1].[IncidentFunctionEscalatedTo2ndLineSupport] = 1) AND ([Extent1].[Incident2ndLineSupportKey] IS NULL) AND (([Extent2].[Incident2ndLineSupportLimitedAccess] <> cast(1 as bit)) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent7]
    WHERE ([Extent1].[ServiceId] = [Extent7].[ServiceId]) AND (2 = CAST( [Extent7].[TicketType] AS int)) AND ([Extent7].[UserProviderKey] = @p__linq__14) AND (2 = [Extent7].[SupportLine])
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent8]
    WHERE ([Extent8].[UserProviderKey] = @p__linq__15) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent9]
    WHERE ([Extent9].[ServiceId] = [Extent1].[ServiceId]) AND (2 = CAST( [Extent9].[TicketType] AS int)) AND (2 = [Extent9].[SupportLine]) AND ([Extent9].[OperatorGroupId] = [Extent8].[OperatorGroupId])
    )))) OR ((3 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsServiceRequests] = 1) OR ((301 = CAST( [Extent1].[TicketState] AS int)) AND ([Extent2].[WfDisableServiceRequestTakeFromQueueAction] <> cast(1 as bit)))) AND (( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent10]
    WHERE ([Extent1].[ServiceId] = [Extent10].[ServiceId]) AND (3 = CAST( [Extent10].[TicketType] AS int)) AND ([Extent10].[UserProviderKey] = @p__linq__16)
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent11]
    WHERE ([Extent11].[UserProviderKey] = @p__linq__17) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent12]
    WHERE ([Extent12].[ServiceId] = [Extent1].[ServiceId]) AND (3 = CAST( [Extent12].[TicketType] AS int)) AND ([Extent12].[OperatorGroupId] = [Extent11].[OperatorGroupId])
    )))) OR ((4 = CAST( [Extent1].[TicketType] AS int)) AND ([Extent2].[AllowAccessToOtherOperatorsProblems] = 1) AND (( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent13]
    WHERE ([Extent1].[ServiceId] = [Extent13].[ServiceId]) AND (4 = CAST( [Extent13].[TicketType] AS int)) AND ([Extent13].[UserProviderKey] = @p__linq__18)
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent14]
    WHERE ([Extent14].[UserProviderKey] = @p__linq__19) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent15]
    WHERE ([Extent15].[ServiceId] = [Extent1].[ServiceId]) AND (4 = CAST( [Extent15].[TicketType] AS int)) AND ([Extent15].[OperatorGroupId] = [Extent14].[OperatorGroupId])
    )))) OR ((5 = CAST( [Extent1].[TicketType] AS int)) AND (([Extent2].[AllowAccessToOtherOperatorsChanges] = 1) OR ((501 = [Extent1].[TicketStateValue]) AND ([Extent2].[WfDisableChangeTakeFromQueueAction] <> cast(1 as bit)))) AND (( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperators] AS [Extent16]
    WHERE ([Extent1].[ServiceId] = [Extent16].[ServiceId]) AND (5 = CAST( [Extent16].[TicketType] AS int)) AND ([Extent16].[UserProviderKey] = @p__linq__20)
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[OperatorGroupUsers] AS [Extent17]
    WHERE ([Extent17].[UserProviderKey] = @p__linq__21) AND ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[ServiceOperatorGroups] AS [Extent18]
    WHERE ([Extent18].[ServiceId] = [Extent1].[ServiceId]) AND (5 = CAST( [Extent18].[TicketType] AS int)) AND ([Extent18].[OperatorGroupId] = [Extent17].[OperatorGroupId])
    )))) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM [dbo].[TicketInvitations] AS [Extent19]
    WHERE ([Extent19].[TicketId] = [Extent1].[Id]) AND (([Extent19].[InvitedUserProviderKey] = @p__linq__22) OR (([Extent19].[InvitedUserProviderKey] IS NULL) AND (@p__linq__22 IS NULL)))
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM (SELECT
    [Extent20].[CustomerId] AS [CustomerId]
    FROM [dbo].[CustomerUsers] AS [Extent20]
    WHERE ([Extent20].[UserProviderKey] = @p__linq__23) AND ([Extent20].[CanAccessOthersTickets] = 1)
    INTERSECT
    SELECT
    [Extent21].[CustomerId] AS [CustomerId]
    FROM [dbo].[CustomerUsers] AS [Extent21]
    WHERE [Extent21].[UserProviderKey] = [Extent1].[SubmitterKey]) AS [Intersect1]
    )) OR ( EXISTS (SELECT
    1 AS [C1]
    FROM (SELECT
    [Extent22].[InternalGroupId] AS [InternalGroupId]
    FROM [dbo].[InternalGroupUsers] AS [Extent22]
    WHERE ([Extent22].[UserProviderKey] = @p__linq__24) AND ([Extent22].[CanAccessOthersTickets] = 1)
    INTERSECT
    SELECT
    [Extent23].[InternalGroupId] AS [InternalGroupId]
    FROM [dbo].[InternalGroupUsers] AS [Extent23]
    WHERE [Extent23].[UserProviderKey] = [Extent1].[SubmitterKey]) AS [Intersect2]
    ) AS [GroupBy1]',N'@p__linq__0 bit,@p__linq__1 varchar(8000),@p__linq__2 varchar(8000),@p__linq__3 varchar(8000),@p__linq__4 varchar(8000),@p__linq__5 bit,@p__linq__6 bit,@p__linq__7 varchar(8000),@p__linq__8 varchar(8000),@p__linq__9 varchar(8000),@p__linq__10 varchar(8000),@p__linq__11 varchar(8000),@p__linq__12 varchar(8000),@p__linq__13 varchar(8000),@p__linq__14 varchar(8000),@p__linq__15 varchar(8000),@p__linq__16 varchar(8000),@p__linq__17 varchar(8000),@p__linq__18 varchar(8000),@p__linq__19 varchar(8000),@p__linq__20 varchar(8000),@p__linq__21 varchar(8000),@p__linq__22 varchar(8000),@p__linq__23 varchar(8000),@p__linq__24 varchar(8000)',@p__linq__0=0,@p__linq__1='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__2='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__3='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__4='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__5=1,@p__linq__6=0,@p__linq__7='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__8='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__9='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__10='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__11='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__12='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__13='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__14='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__15='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__16='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__17='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__18='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__19='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__20='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__21='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__22='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__23='31555851-b89d-4a15-bb05-5a6fd42f9552',@p__linq__24='31555851-b89d-4a15-bb05-5a6fd42f9552'
    Generated DDL for related tables: (indexes are primary on PKs and FKs)
    CREATE TABLE [dbo].[CustomerUsers](
    [UserProviderKey] [varchar](184) NOT NULL,
    [CustomerId] [int] NOT NULL,
    [CanAccessOthersTickets] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.CustomerUsers] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[InternalGroupUsers] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[InternalGroupUsers](
    [UserProviderKey] [varchar](184) NOT NULL,
    [InternalGroupId] [int] NOT NULL,
    [CanAccessOthersTickets] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.InternalGroupUsers] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC,
    [InternalGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[OperatorGroupUsers] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[OperatorGroupUsers](
    [UserProviderKey] [varchar](184) NOT NULL,
    [OperatorGroupId] [int] NOT NULL,
    CONSTRAINT [PK_dbo.OperatorGroupUsers] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC,
    [OperatorGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[ServiceDeputyManagers] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[ServiceDeputyManagers](
    [UserProviderKey] [varchar](184) NOT NULL,
    [ServiceId] [int] NOT NULL,
    [TicketType] [int] NOT NULL,
    CONSTRAINT [PK_dbo.ServiceDeputyManagers] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC,
    [ServiceId] ASC,
    [TicketType] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[ServiceOperatorGroups] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[ServiceOperatorGroups](
    [ServiceId] [int] NOT NULL,
    [OperatorGroupId] [int] NOT NULL,
    [TicketTypeValue] [int] NOT NULL,
    [SupportLine] [int] NOT NULL,
    [TicketType] [int] NOT NULL,
    CONSTRAINT [PK_dbo.ServiceOperatorGroups] PRIMARY KEY CLUSTERED
    [ServiceId] ASC,
    [OperatorGroupId] ASC,
    [TicketTypeValue] ASC,
    [SupportLine] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Object: Table [dbo].[ServiceOperators] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[ServiceOperators](
    [UserProviderKey] [varchar](184) NOT NULL,
    [ServiceId] [int] NOT NULL,
    [TicketTypeValue] [int] NOT NULL,
    [SupportLine] [int] NOT NULL,
    [TicketType] [int] NOT NULL,
    CONSTRAINT [PK_dbo.ServiceOperators] PRIMARY KEY CLUSTERED
    [UserProviderKey] ASC,
    [ServiceId] ASC,
    [TicketTypeValue] ASC,
    [SupportLine] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[Services] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[Services](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ParentId] [int] NULL,
    [Name] [nvarchar](256) NOT NULL,
    [Description] [nvarchar](max) NULL,
    [Disabled] [bit] NOT NULL,
    [NewTicketLimitedAccess] [bit] NOT NULL,
    [Incident1stLineSupportLimitedAccess] [bit] NOT NULL,
    [Incident2ndLineSupportLimitedAccess] [bit] NOT NULL,
    [ServiceRequestLimitedAccess] [bit] NOT NULL,
    [ProblemLimitedAccess] [bit] NOT NULL,
    [ServiceRequestManager] [varchar](184) NOT NULL,
    [IncidentManager] [varchar](184) NOT NULL,
    [ProblemManager] [varchar](184) NOT NULL,
    [Deleted] [bit] NOT NULL,
    [WfDisableIncidentAssignedState] [bit] NOT NULL,
    [WfDisableIncidentConfirmedState] [bit] NOT NULL,
    [WfDisableIncidentTakeFromQueueAction] [bit] NOT NULL,
    [WfDisableIncidentFinishSolutionAction] [bit] NOT NULL,
    [WfDisableServiceRequestAssignedState] [bit] NOT NULL,
    [WfDisableServiceRequestConfirmedState] [bit] NOT NULL,
    [WfDisableServiceRequestTakeFromQueueAction] [bit] NOT NULL,
    [WfDisableServiceRequestFinishSolutionAction] [bit] NOT NULL,
    [WfDisableServiceRequestPostponeAction] [bit] NOT NULL,
    [ChangeLimitedAccess] [bit] NOT NULL,
    [ChangeManager] [varchar](184) NOT NULL,
    [WfDisableChangeTakeFromQueueAction] [bit] NOT NULL,
    [WfDisableChangeAssignedState] [bit] NOT NULL,
    [WfDisableChangeStartPreparationAction] [bit] NOT NULL,
    [IsDepartment] [bit] NOT NULL,
    [InheritsFromDepartment] [bit] NOT NULL,
    [AllowSelectSolverBySubmitterForIncidents] [bit] NOT NULL,
    [AllowSelectSolverBySubmitterForServiceRequests] [bit] NOT NULL,
    [AllowSelectSolverBySubmitterForProblems] [bit] NOT NULL,
    [AllowSelectSolverBySubmitterForChanges] [bit] NOT NULL,
    [AllowAccessToOtherOperatorsIncidents] [bit] NOT NULL,
    [AllowAccessToOtherOperatorsServiceRequests] [bit] NOT NULL,
    [AllowAccessToOtherOperatorsProblems] [bit] NOT NULL,
    [AllowAccessToOtherOperatorsChanges] [bit] NOT NULL,
    [AllowChangeDeadlineForIncidents] [bit] NOT NULL,
    [AllowChangeDeadlineForServiceRequests] [bit] NOT NULL,
    [AllowChangeDeadlineForProblems] [bit] NOT NULL,
    [AllowChangeDeadlineForChanges] [bit] NOT NULL,
    [AllowSelectPriorityForServiceRequests] [bit] NOT NULL,
    [WfDisableIncidentCompletedState] [bit] NOT NULL,
    [WfDoIncidentCompleteActionBySubmittersMessage] [bit] NOT NULL,
    [WfDisableServiceRequestCompletedState] [bit] NOT NULL,
    [WfDoServiceRequestCompleteActionBySubmittersMessage] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.Services] PRIMARY KEY CLUSTERED
    [Id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[TicketInvitations] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[TicketInvitations](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [TicketId] [int] NOT NULL,
    [InitiatorUserProviderKey] [varchar](184) NULL,
    [InitiatorFullName] [nvarchar](max) NULL,
    [InvitedUserProviderKey] [varchar](184) NULL,
    [InvitedFullName] [nvarchar](max) NULL,
    [Type] [int] NOT NULL,
    [CreatedUTC] [datetime] NOT NULL,
    CONSTRAINT [PK_dbo.TicketInvitations] PRIMARY KEY CLUSTERED
    [Id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Table [dbo].[Tickets] Script Date: 7.5.2014 8:39:38 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[Tickets](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ParentId] [int] NULL,
    [ServiceId] [int] NULL,
    [ServiceMailboxId] [int] NULL,
    [TicketTypeValue] [int] NOT NULL,
    [TicketTypeIdREF] [int] NOT NULL,
    [SubmitterKey] [varchar](184) NOT NULL,
    [SubmitterFullName] [nvarchar](256) NULL,
    [CustomerId] [int] NULL,
    [SolverKey] [varchar](184) NULL,
    [SolverFullName] [nvarchar](256) NULL,
    [Subject] [nvarchar](max) NULL,
    [CreatedUTC] [datetime] NOT NULL,
    [Archived] [bit] NOT NULL,
    [MarkedAsSolvedUTC] [datetime] NULL,
    [ArchivedUTC] [datetime] NULL,
    [TicketSourceValue] [int] NOT NULL,
    [OperatorKey] [varchar](184) NULL,
    [DeadlineUTC] [datetime] NULL,
    [DeadlineLastNotificatedPercentage] [int] NULL,
    [UrgencyValue] [int] NULL,
    [ImpactValue] [int] NULL,
    [PriorityValue] [int] NULL,
    [TicketStateValue] [int] NOT NULL,
    [IncidentFunctionEscalatedTo2ndLineSupport] [bit] NOT NULL,
    [Incident2ndLineSupportKey] [varchar](184) NULL,
    [Incident2ndLineSupportFullName] [nvarchar](max) NULL,
    [TicketType] [int] NOT NULL,
    [Source] [int] NOT NULL,
    [TicketState] [int] NOT NULL,
    [Urgency] [int] NULL,
    [Impact] [int] NULL,
    [TicketSummaryState] [int] NOT NULL,
    [ResolutionText] [nvarchar](max) NULL,
    [ResolutionModifiedUTC] [datetime] NULL,
    [ResolutionEdited] [bit] NOT NULL,
    [ResolutionUserProviderKey] [varchar](184) NULL,
    [ResolutionFullName] [nvarchar](max) NULL,
    [TicketSubType] [int] NULL,
    [ChangeRiskProbabilityValue] [int] NULL,
    [ChangeImpactValue] [int] NULL,
    [ChangeRiskCategoryValue] [int] NULL,
    [RfcText] [nvarchar](max) NULL,
    [RfcModifiedUTC] [datetime] NULL,
    [RfcEdited] [bit] NOT NULL,
    [RfcUserProviderKey] [varchar](184) NULL,
    [RfcFullName] [nvarchar](max) NULL,
    [ManualDeadline] [bit] NOT NULL,
    [ContactInformation] [nvarchar](256) NULL,
    [Imported] [bit] NOT NULL,
    [ForceClosed] [bit] NOT NULL,
    CONSTRAINT [PK_dbo.Tickets] PRIMARY KEY CLUSTERED
    [Id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object: Index [IX_CustomerId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_CustomerId] ON [dbo].[CustomerUsers]
    [CustomerId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_InternalGroupId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_InternalGroupId] ON [dbo].[InternalGroupUsers]
    [InternalGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_OperatorGroupId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_OperatorGroupId] ON [dbo].[OperatorGroupUsers]
    [OperatorGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceId] ON [dbo].[ServiceDeputyManagers]
    [ServiceId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_OperatorGroupId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_OperatorGroupId] ON [dbo].[ServiceOperatorGroups]
    [OperatorGroupId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceId] ON [dbo].[ServiceOperatorGroups]
    [ServiceId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceId] ON [dbo].[ServiceOperators]
    [ServiceId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ParentId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ParentId] ON [dbo].[Services]
    [ParentId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_TicketId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_TicketId] ON [dbo].[TicketInvitations]
    [TicketId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_TicketInvitations_InvitedUserProviderKey_TicketId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE UNIQUE NONCLUSTERED INDEX [IX_TicketInvitations_InvitedUserProviderKey_TicketId] ON [dbo].[TicketInvitations]
    [InvitedUserProviderKey] ASC,
    [TicketId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_CustomerId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_CustomerId] ON [dbo].[Tickets]
    [CustomerId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ParentId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ParentId] ON [dbo].[Tickets]
    [ParentId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceId] ON [dbo].[Tickets]
    [ServiceId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_ServiceMailboxId] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_ServiceMailboxId] ON [dbo].[Tickets]
    [ServiceMailboxId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_SolverFullName] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_SolverFullName] ON [dbo].[Tickets]
    [SolverFullName] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_SolverKey] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_SolverKey] ON [dbo].[Tickets]
    [SolverKey] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_SubmitterFullName] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_SubmitterFullName] ON [dbo].[Tickets]
    [SubmitterFullName] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    SET ANSI_PADDING ON
    GO
    /****** Object: Index [IX_SubmitterKey] Script Date: 7.5.2014 8:39:38 ******/
    CREATE NONCLUSTERED INDEX [IX_SubmitterKey] ON [dbo].[Tickets]
    [SubmitterKey] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    /****** Object: Index [IX_Tickets_TicketType_TicketTypeIdREF] Script Date: 7.5.2014 8:39:38 ******/
    CREATE UNIQUE NONCLUSTERED INDEX [IX_Tickets_TicketType_TicketTypeIdREF] ON [dbo].[Tickets]
    [TicketType] ASC,
    [TicketTypeIdREF] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO
    ALTER TABLE [dbo].[CustomerUsers] WITH CHECK ADD CONSTRAINT [FK_dbo.CustomerUsers_dbo.Customers_CustomerId] FOREIGN KEY([CustomerId])
    REFERENCES [dbo].[Customers] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[CustomerUsers] CHECK CONSTRAINT [FK_dbo.CustomerUsers_dbo.Customers_CustomerId]
    GO
    ALTER TABLE [dbo].[InternalGroupUsers] WITH CHECK ADD CONSTRAINT [FK_dbo.InternalGroupUsers_dbo.InternalGroups_InternalGroupId] FOREIGN KEY([InternalGroupId])
    REFERENCES [dbo].[InternalGroups] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[InternalGroupUsers] CHECK CONSTRAINT [FK_dbo.InternalGroupUsers_dbo.InternalGroups_InternalGroupId]
    GO
    ALTER TABLE [dbo].[OperatorGroupUsers] WITH CHECK ADD CONSTRAINT [FK_dbo.OperatorGroupUsers_dbo.OperatorGroups_OperatorGroupId] FOREIGN KEY([OperatorGroupId])
    REFERENCES [dbo].[OperatorGroups] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[OperatorGroupUsers] CHECK CONSTRAINT [FK_dbo.OperatorGroupUsers_dbo.OperatorGroups_OperatorGroupId]
    GO
    ALTER TABLE [dbo].[ServiceDeputyManagers] WITH CHECK ADD CONSTRAINT [FK_dbo.ServiceDeputyManagers_dbo.Services_ServiceId] FOREIGN KEY([ServiceId])
    REFERENCES [dbo].[Services] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[ServiceDeputyManagers] CHECK CONSTRAINT [FK_dbo.ServiceDeputyManagers_dbo.Services_ServiceId]
    GO
    ALTER TABLE [dbo].[ServiceOperatorGroups] WITH CHECK ADD CONSTRAINT [FK_dbo.ServiceOperatorGroups_dbo.OperatorGroups_OperatorGroupId] FOREIGN KEY([OperatorGroupId])
    REFERENCES [dbo].[OperatorGroups] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[ServiceOperatorGroups] CHECK CONSTRAINT [FK_dbo.ServiceOperatorGroups_dbo.OperatorGroups_OperatorGroupId]
    GO
    ALTER TABLE [dbo].[ServiceOperatorGroups] WITH CHECK ADD CONSTRAINT [FK_dbo.ServiceOperatorGroups_dbo.Services_ServiceId] FOREIGN KEY([ServiceId])
    REFERENCES [dbo].[Services] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[ServiceOperatorGroups] CHECK CONSTRAINT [FK_dbo.ServiceOperatorGroups_dbo.Services_ServiceId]
    GO
    ALTER TABLE [dbo].[ServiceOperators] WITH CHECK ADD CONSTRAINT [FK_dbo.ServiceOperators_dbo.Services_ServiceId] FOREIGN KEY([ServiceId])
    REFERENCES [dbo].[Services] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[ServiceOperators] CHECK CONSTRAINT [FK_dbo.ServiceOperators_dbo.Services_ServiceId]
    GO
    ALTER TABLE [dbo].[Services] WITH CHECK ADD CONSTRAINT [FK_dbo.Services_dbo.Services_ParentId] FOREIGN KEY([ParentId])
    REFERENCES [dbo].[Services] ([Id])
    GO
    ALTER TABLE [dbo].[Services] CHECK CONSTRAINT [FK_dbo.Services_dbo.Services_ParentId]
    GO
    ALTER TABLE [dbo].[TicketInvitations] WITH CHECK ADD CONSTRAINT [FK_dbo.TicketInvitations_dbo.Tickets_TicketId] FOREIGN KEY([TicketId])
    REFERENCES [dbo].[Tickets] ([Id])
    ON DELETE CASCADE
    GO
    ALTER TABLE [dbo].[TicketInvitations] CHECK CONSTRAINT [FK_dbo.TicketInvitations_dbo.Tickets_TicketId]
    GO
    ALTER TABLE [dbo].[Tickets] WITH CHECK ADD CONSTRAINT [FK_dbo.Tickets_dbo.Customers_CustomerId] FOREIGN KEY([CustomerId])
    REFERENCES [dbo].[Customers] ([Id])
    GO
    ALTER TABLE [dbo].[Tickets] CHECK CONSTRAINT [FK_dbo.Tickets_dbo.Customers_CustomerId]
    GO
    ALTER TABLE [dbo].[Tickets] WITH CHECK ADD CONSTRAINT [FK_dbo.Tickets_dbo.ServiceMailboxes_ServiceMailboxId] FOREIGN KEY([ServiceMailboxId])
    REFERENCES [dbo].[ServiceMailboxes] ([Id])
    GO
    ALTER TABLE [dbo].[Tickets] CHECK CONSTRAINT [FK_dbo.Tickets_dbo.ServiceMailboxes_ServiceMailboxId]
    GO
    ALTER TABLE [dbo].[Tickets] WITH CHECK ADD CONSTRAINT [FK_dbo.Tickets_dbo.Services_ServiceId] FOREIGN KEY([ServiceId])
    REFERENCES [dbo].[Services] ([Id])
    GO
    ALTER TABLE [dbo].[Tickets] CHECK CONSTRAINT [FK_dbo.Tickets_dbo.Services_ServiceId]
    GO
    ALTER TABLE [dbo].[Tickets] WITH CHECK ADD CONSTRAINT [FK_dbo.Tickets_dbo.Tickets_ParentId] FOREIGN KEY([ParentId])
    REFERENCES [dbo].[Tickets] ([Id])
    GO
    ALTER TABLE [dbo].[Tickets] CHECK CONSTRAINT [FK_dbo.Tickets_dbo.Tickets_ParentId]
    GO

  • Advance Data Grid - Flat Query Array Collection To Grouping Collection Issue

    Currently I have a Coldfusion CFC returning a flat query. The query is flat but returns data with four levels. For simplicity let's say Region, Territory, Title, and Person.
    I place the data into an Array Collection in Flex and then use the Advanced Data Grid (ADG) with a Grouping Collection to create a hierarchy of Region, Territory, Title. At the lowest level of the ADG I have the Person data and that data is summarized up to the Region node. The problem I am having is that sometimes I do not have any person data but have data at the Title level that can be summarized up to the Region node.
    For data where the Title does not have any Person information the Title node still can be expanded to show a blank Person row. How can I prevent blank Person rows from showing up while still maintaining the ability to properly show available Person rows?
    Would using an XML Collection accomplish this?

    Currently I have a Coldfusion CFC returning a flat query. The query is flat but returns data with four levels. For simplicity let's say Region, Territory, Title, and Person.
    I place the data into an Array Collection in Flex and then use the Advanced Data Grid (ADG) with a Grouping Collection to create a hierarchy of Region, Territory, Title. At the lowest level of the ADG I have the Person data and that data is summarized up to the Region node. The problem I am having is that sometimes I do not have any person data but have data at the Title level that can be summarized up to the Region node.
    For data where the Title does not have any Person information the Title node still can be expanded to show a blank Person row. How can I prevent blank Person rows from showing up while still maintaining the ability to properly show available Person rows?
    Would using an XML Collection accomplish this?

  • SQL Statement -- Throws GROUP BY issue on NVL

    Hey All,
    This is pretty much my first time tackling a very complex SQL Statement but the boss is adamant about it.
    Anyways here is the information. I have 3 Tables. Table one Contains Work Order Infromation that will be listed or grouped. Table 2 and Table 3 house information that needs to be summed (Estimate hours and Actual Hours).
    So my SQL Statement looked something like this... (Its a mess)
    select workorder.wolo3, workorder.wojp1, workorder.wonum, workorder.description, workorder.location, workorder.status, workorder.wopriority, workorder.targstartdate, workorder.reportdate, workorder.pmnum, workorder.schedstart, sum(labtrans.regularhrs + labtrans.premiumpayhours) as actualhours, sum(wplabor.quantity * wplabor.laborhrs) as estimatehrs
    from maximo.workorder left outer join maximo.labtrans on labtrans.regularhrs > 0 and labtrans.refwo = workorder.wonum and labtrans.siteid = workorder.siteid or labtrans.premiumpayratetype = 'MULTIPLIER' and labtrans.refwo = workorder.wonum and labtrans.siteid = workorder.siteid left outer join maximo.wplabor on wplabor.wonum = workorder.wonum and wplabor.siteid = workorder.siteid
    where workorder.istask=0 and workorder.siteid='NTS' and workorder.status in ('INPRG','SCHED') and workorder.crewid in ('MAINT','DAF') and workorder.schedstart <= TO_DATE('12-13-2009','MM-DD-YYYY') and wolo3 is not null group by workorder.wonum, workorder.status, workorder.wopriority, workorder.pmnum, workorder.location, workorder.description, workorder.targstartdate, workorder.reportdate, workorder.schedstart, workorder.wolo3, workorder.wojp1
    Now the problem is that the 2nd sum (Estimate Hrs) gets HUGE numbers if there are multiple rows in Actual Hours. So I did some searching. Seems that if I used the NVL function in the SQL Statement it would fix the issue...
    select workorder.wolo3, workorder.wojp1, workorder.wonum, workorder.description, workorder.location, workorder.status, workorder.wopriority, workorder.targstartdate, workorder.reportdate, workorder.pmnum, workorder.schedstart, sum(labtrans.regularhrs + labtrans.premiumpayhours) as actualhours,
    nvl((select sum(wplabor.quantity * wplabor.laborhrs) from maximo.wplabor where wplabor.wonum = workorder.wonum and wplabor.siteid = workorder.siteid),0)
    from maximo.workorder left outer join maximo.labtrans on labtrans.regularhrs > 0 and labtrans.refwo = workorder.wonum and
    labtrans.siteid = workorder.siteid or labtrans.premiumpayratetype = 'MULTIPLIER' and labtrans.refwo = workorder.wonum and
    labtrans.siteid = workorder.siteid
    where workorder.istask=0 and workorder.siteid='NTS' and workorder.status in ('INPRG','SCHED') and
    workorder.crewid in ('MAINT','DAF') and workorder.schedstart <= TO_DATE('12-13-2009','MM-DD-YYYY') and wolo3 is not null
    group by workorder.wonum, workorder.status, workorder.wopriority, workorder.pmnum, workorder.location, workorder.description, workorder.targstartdate, workorder.reportdate, workorder.schedstart, workorder.wolo3, workorder.wojp1
    However If get the error not a GROUP BY expression.
    I really need this to work. How can I get the NVL to work with out having to define a Group for it. The problem is that it is a "alias column" and you cannot Group by those fields. Frustation sets in... UGH!
    Thanks in Advance, Ben.

    Hi, Ben,
    Wlecome to the forum!
    user12273726 wrote:
    So my SQL Statement looked something like this... (Its a mess)You're right. Fix that first.
    Put each SELECT item, each condition, and each GROUP BY expression on a separate line. Indent to show here the clauses (SELECT, FROM, WHERE) are.
    For example:
    SELECT    workorder.wolo3
    ,       workorder.wojp1
    ,       workorder.wonum
    ,       workorder.description
    ,       workorder.location
    ,       workorder.status
    ,       workorder.wopriority
    ,       workorder.targstartdate
    ,       workorder.reportdate
    ,       workorder.pmnum
    ,       workorder.schedstart
    ,       SUM (labtrans.regularhrs + labtrans.premiumpayhours)     AS actualhours
    ,       SUM (wplabor.quantity * wplabor.laborhrs)          AS estimatehrs
    FROM                  maximo.workorder
    LEFT OUTER JOIN   maximo.labtrans     ON      labtrans.regularhrs         > 0
                                       AND     labtrans.refwo              = workorder.wonum
                             AND     labtrans.siteid              = workorder.siteid
                             OR     labtrans.premiumpayratetype = 'MULTIPLIER'     -- WARNING!  Don't mix AND and OR
                             AND     labtrans.refwo              = workorder.wonum
                             AND     labtrans.siteid              = workorder.siteid
    LEFT OUTER JOIN   maximo.wplabor     ON     wplabor.wonum              = workorder.wonum
                                       AND     wplabor.siteid              = workorder.siteid
    WHERE     workorder.istask      = 0
    AND       workorder.siteid       = 'NTS'
    AND       workorder.status       IN ('INPRG','SCHED')
    AND       workorder.crewid       IN ('MAINT','DAF')
    AND       workorder.schedstart   <= TO_DATE ('12-13-2009','MM-DD-YYYY')
    AND       wolo3                       IS NOT NULL
    GROUP BY  workorder.wonum
    ,            workorder.status
    ,       workorder.wopriority
    ,       workorder.pmnum
    ,       workorder.location
    ,       workorder.description
    ,       workorder.targstartdate
    ,       workorder.reportdate
    ,       workorder.schedstart
    ,       workorder.wolo3
    ,       workorder.wojp1
    ;When you post formatted text on this site, type these 6 characters:
    &#123;code&#125;
    (small letters only, inside curly brackets) before and after sections of formatted text, to preserve spacing.
    Now the problem is that the 2nd sum (Estimate Hrs) gets HUGE numbers if there are multiple rows in Actual Hours. Huge numbers are not necessarily wrong. Do yo mean the numbers computed by your query are much bigger than they are supposed to be?
    So I did some searching. Seems that if I used the NVL function in the SQL Statement it would fix the issue...
    select workorder.wolo3, workorder.wojp1, workorder.wonum, workorder.description, workorder.location, workorder.status, workorder.wopriority, workorder.targstartdate, workorder.reportdate, workorder.pmnum, workorder.schedstart, sum(labtrans.regularhrs + labtrans.premiumpayhours) as actualhours,
    nvl((select sum(wplabor.quantity * wplabor.laborhrs) from maximo.wplabor where wplabor.wonum = workorder.wonum and wplabor.siteid = workorder.siteid),0)
    ...It looks like you have a Chasm Trap, where you have multiple, independent one-to-many relationships on the same table, and the solution to aggregate them in separate queruies.
    It looks like you're trying to aggregate wplabor in scalar sub-queries, which makes the GROUP BY more complicated (as you discovered), and is also very inefficient.
    It would be easier to code and more efficient to run if you aggregated wplabor in a sub-query, and then joined to this sub-query (and not the actual wplabor table) in your main query.
    If you'd like help, post a little sample data (CREATE TABLE and INSERT statements) for all three tables, and the results you want from that data.
    Simplify as much as possible. Instead of GROUPing BY 11 columns, just GROUP BY, say, 3 columns: the 2 involved in the join condidiotns, and one more. Once you understand how to do this, adding the other expressions will be trivial.
    You can also simplify by ignoring, for now, the WHERE clause, and, therefore, the columns involved in the WHERE clause. It looks like that has nothing to do with the problem. Again, adding the conditions later will be trivial.

  • SQL Query Having performance issues

    I need help on rewriting this query, I am having performance issues with the way it is now. I create a temporary table and query against it. Any help will be appreciated. Thanks, Antonio
    Here it is:
    create global temporary table pr_php_elig_tmp
    on commit preserve rows as
    SELECT UNIQUE A.SAK_PROV, A.CDE_SERVICE_LOC, a.sak_short
    FROM T_PR_PHP_ELIG A,
    T_PR_TYPE C
    WHERE C.SAK_PROV = A.SAK_PROV
    AND C.CDE_SERVICE_LOC = A.CDE_SERVICE_LOC
    AND C.CDE_PROV_TYPE not in ('01', '03', '08', '24', '27', '31')
    AND A.SAK_PROV_PGM not in (11, 13)
    AND A.DTE_END >= :il_current_date
    AND A.DTE_EFFECTIVE <= :il_prev_18_months
    AND NOT EXISTS (
    SELECT 'X'
    FROM T_PR_GRP_MBR T
    WHERE T.SAK_PROV = A.SAK_PROV
    AND T.CDE_SVC_LOC_MBR = A.CDE_SERVICE_LOC)
    AND NOT EXISTS (
    select a.sak_prov, a.cde_service_loc
    from t_pmp_svc_loc d
    where a.sak_prov = d.sak_prov
    and a.cde_service_loc = d.cde_service_loc
    and d.dte_end >= :il_current_date)
    ORDER BY A.SAK_PROV, A.CDE_SERVICE_LOC;
    select * from pr_php_elig_tmp
    minus
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_deny_dntl_dtl temp01
                   WHERE temp01.sak_prov_perf = a.sak_prov
                   and temp01.cde_svc_loc_perf = a.cde_service_loc
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
                   HAVING MAX(temp01.DTE_FIRST_SVC) >= :il_prev_18_months
                   OR NVL(MAX(temp01.DTE_FIRST_SVC),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_deny_dntl_hdr temp02
                   WHERE ( (temp02.prov_billing     = a.sak_prov
                   and temp02.cde_service_loc = a.cde_service_loc)
                   OR     (temp02.sak_prov_perf     = a.sak_prov
                   and temp02.cde_perf_svc_loc = a.cde_service_loc)
                   OR     (temp02.sak_prov_referring = a.sak_prov
                   and temp02.cde_svc_loc_ref_1 = a.cde_service_loc)
                   OR     (temp02.sak_prov_referring_2 = a.sak_prov
                   and temp02.cde_svc_loc_ref_2 = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
                   HAVING MAX(temp02.DTE_FINAL)     >= :il_prev_18_months
                   OR NVL(MAX(temp02.DTE_FINAL),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_deny_phrm_hdr temp03
                   WHERE ( (temp03.prov_billing     = a.sak_prov and
                        temp03.cde_service_loc = a.cde_service_loc)
                   OR     (temp03.sak_prov_rendering = a.sak_prov and
                        temp03.cde_svc_loc_rend = a.cde_service_loc)
                   OR     (temp03.sak_presc_prov = a.sak_prov and
                        temp03.cde_svc_loc_presc = a.cde_service_loc)
                   OR     temp03.id_prov_prescrb = to_char(a.sak_prov) || a.cde_service_loc
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
                   HAVING     MAX(temp03.DTE_FINAL) >= :il_prev_18_months
                   OR     NVL(MAX(temp03.DTE_FINAL),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_deny_phys_dtl temp04
              WHERE ( (temp04.sak_prov_perf     = a.sak_prov
                   and     temp04.cde_svc_loc_rend = a.cde_service_loc)
                   OR (temp04.sak_prov_referring = a.sak_prov
                   and     temp04.cde_svc_loc_ref_1 = a.cde_service_loc)
                   OR (temp04.sak_prov_referring_2 = a.sak_prov
                   and     temp04.cde_svc_loc_ref_2 = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING     MAX(temp04.DTE_LAST_SVC)     >= :il_prev_18_months
                   OR     NVL(MAX(temp04.DTE_LAST_SVC),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_deny_phys_hdr temp05
              WHERE ( (temp05.prov_billing     = a.sak_prov
                   and     temp05.cde_service_loc     = a.cde_service_loc)
                   OR (temp05.sak_prov_perf     = a.sak_prov
                   and     temp05.cde_perf_svc_loc = a.cde_service_loc)
                   OR (temp05.sak_prov_referring = a.sak_prov
                   and     temp05.cde_svc_loc_ref_1 = a.cde_service_loc)
                   OR (temp05.sak_prov_referring_2 = a.sak_prov
                   and     temp05.cde_svc_loc_ref_2 = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING     MAX(temp05.DTE_FINAL) >= :il_prev_18_months
                   OR     NVL(MAX(temp05.DTE_FINAL),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_deny_ub92_dtl temp06
              WHERE ( (temp06.sak_pr_other     = a.sak_prov
                   and     temp06.cde_scv_loc_other = a.cde_service_loc)
                   OR (temp06.sak_pr_other_2     = a.sak_prov
                   and     temp06.cde_svc_loc_other_2 = a.cde_service_loc)
                   OR (temp06.sak_pr_attend     = a.sak_prov
                   and     temp06.cde_svc_loc_attend = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING     MAX(temp06.DTE_LAST_SVC)     >= :il_prev_18_months
                   OR     NVL(MAX(temp06.DTE_LAST_SVC),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_deny_ub92_hdr temp07
              WHERE ( (temp07.prov_billing     = a.sak_prov
                   and     temp07.cde_service_loc     = a.cde_service_loc)
              OR (temp07.sak_pr_facility     = a.sak_prov
                   and     temp07.cde_svc_loc_fa     = a.cde_service_loc)
              OR (temp07.sak_pr_other     = a.sak_prov
                   and     temp07.cde_svc_loc_other = a.cde_service_loc)
              OR (temp07.sak_pr_other_2     = a.sak_prov
                   and     temp07.cde_svc_loc_other_2 = a.cde_service_loc)
              OR (temp07.sak_pr_attend     = a.sak_prov
                   and     temp07.cde_svc_loc_attend = a.cde_service_loc)
              OR     temp07.id_prov_attend     = to_char(a.sak_prov) || a.cde_service_loc
              OR     temp07.id_prov_other     = to_char(a.sak_prov) || a.cde_service_loc
              OR     temp07.id_prov_other_2     = to_char(a.sak_prov) || a.cde_service_loc
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp07.DTE_FINAL)     >= :il_prev_18_months
                   OR     NVL(MAX(temp07.DTE_FINAL),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_pd_dntl_dtl temp08
              WHERE temp08.sak_prov_perf     = a.sak_prov
                   and temp08.cde_svc_loc_perf     = a.cde_service_loc
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp08.DTE_FIRST_SVC) >= :il_prev_18_months
                   OR     NVL(MAX(temp08.DTE_FIRST_SVC),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_pd_dntl_hdr     temp09
              WHERE ( (temp09.prov_billing     = a.sak_prov
                   and     temp09.cde_service_loc     = a.cde_service_loc)
              OR (temp09.sak_prov_perf     = a.sak_prov
                   and     temp09.cde_perf_svc_loc = a.cde_service_loc)
              OR (temp09.sak_prov_referring = a.sak_prov
                   and     temp09.cde_svc_loc_ref_1 = a.cde_service_loc)
              OR (temp09.sak_prov_referring_2 = a.sak_prov
                   and     temp09.cde_svc_loc_ref_2 = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp09.DTE_FINAL)     >= :il_prev_18_months
                   OR     NVL(MAX(temp09.DTE_FINAL),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_pd_pharm_hdr temp10
              WHERE ( (temp10.prov_billing     = a.sak_prov
                   and     temp10.cde_service_loc     = a.cde_service_loc)
              OR (temp10.sak_prov_rend     = a.sak_prov
                   and     temp10.cde_svc_loc_rend = a.cde_service_loc)
              OR (temp10.sak_presc_prov     = a.sak_prov
                   and     temp10.cde_svc_loc_presc = a.cde_service_loc)
              OR     temp10.id_prov_prescrb     = to_char(a.sak_prov) || a.cde_service_loc
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp10.DTE_FINAL)     >= :il_prev_18_months
                   OR     NVL(MAX(temp10.DTE_FINAL),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_pd_phys_dtl temp11
              WHERE ( (temp11.sak_prov_perf     = a.sak_prov
                   and     temp11.cde_svc_loc_rend = a.cde_service_loc)
              OR (temp11.sak_prov_referring = a.sak_prov
                   and     temp11.cde_svc_loc_ref_1 = a.cde_service_loc)
              OR (temp11.sak_prov_referring_2 = a.sak_prov
                   and     temp11.cde_svc_loc_ref_2 = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp11.DTE_LAST_SVC)     >= :il_prev_18_months
                   OR     NVL(MAX(temp11.DTE_LAST_SVC),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_pd_phys_hdr     temp12
              WHERE ( (temp12.prov_billing     = a.sak_prov
                   and     temp12.cde_service_loc     = a.cde_service_loc)
              OR (temp12.sak_prov_perf     = a.sak_prov
                   and     temp12.cde_perf_svc_loc = a.cde_service_loc)
              OR (temp12.sak_prov_referring = a.sak_prov
                   and     temp12.cde_svc_loc_ref_1 = a.cde_service_loc)
              OR (temp12.sak_prov_referring_2 = a.sak_prov
                   and     temp12.cde_svc_loc_ref_2 = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp12.DTE_FINAL)     >= :il_prev_18_months
                   OR     NVL(MAX(temp12.DTE_FINAL),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_pd_ub92_dtl temp13
              WHERE ( (temp13.sak_pr_other     = a.sak_prov
                   and     temp13.cde_scv_loc_other = a.cde_service_loc)
                   OR (temp13.sak_pr_other_2     = a.sak_prov
                   and     temp13.cde_svc_loc_other_2 = a.cde_service_loc)
                   OR (temp13.sak_pr_attend     = a.sak_prov
                   and     temp13.cde_svc_loc_attend = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING     MAX(temp13.DTE_LAST_SVC)     >= :il_prev_18_months
                   OR     NVL(MAX(temp13.DTE_LAST_SVC),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim01.t_pd_ub92_hdr     temp14
              WHERE ( (temp14.prov_billing     = a.sak_prov
                   and     temp14.cde_service_loc     = a.cde_service_loc)
              OR (temp14.sak_pr_facility     = a.sak_prov
                   and     temp14.cde_svc_loc_fa     = a.cde_service_loc)
              OR (temp14.sak_pr_other_2     = a.sak_prov
                   and     temp14.cde_svc_loc_other_2 = a.cde_service_loc)
              OR (temp14.sak_pr_attend     = a.sak_prov
                   and     temp14.cde_svc_loc_attend = a.cde_service_loc)
              OR (temp14.sak_pr_other     = a.sak_prov
                   and     temp14.cde_svc_loc_other = a.cde_service_loc)
              OR     temp14.id_prov_attend     = to_char(a.sak_prov) || a.cde_service_loc
              OR     temp14.id_prov_other     = to_char(a.sak_prov) || a.cde_service_loc
              OR     temp14.id_prov_other_2     = to_char(a.sak_prov) || a.cde_service_loc
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp14.DTE_FINAL)     >= :il_prev_18_months
                   OR     NVL(MAX(temp14.DTE_FINAL),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim00.t_susp_dental_dtl temp15
              WHERE temp15.sak_prov_perf     = a.sak_prov
                   and temp15.cde_svc_loc_perf     = a.cde_service_loc
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp15.DTE_FIRST_SVC) >= :il_prev_18_months
                   OR     NVL(MAX(temp15.DTE_FIRST_SVC),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim00.t_susp_dental_hdr temp16
              WHERE ( (temp16.sak_prov_perf     = a.sak_prov
                   and     temp16.cde_perf_svc_loc = a.cde_service_loc)
              OR (temp16.sak_prov_referring = a.sak_prov
                   and     temp16.cde_svc_loc_ref_1 = a.cde_service_loc)
              OR (temp16.sak_prov_referring_2 = a.sak_prov
                   and     temp16.cde_svc_loc_ref_2 = a.cde_service_loc)
              OR (temp16.prov_billing     = a.sak_prov
                   and     temp16.cde_service_loc     = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING ((MAX(temp16.DTE_LAST_SVC) >= :il_prev_18_months
                   OR     NVL(MAX(temp16.DTE_LAST_SVC),0) = 0)
                   and (MAX(temp16.DTE_BILLED) >= :il_prev_18_months
                   OR     NVL(MAX(temp16.DTE_BILLED),0) = 0) )
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim00.t_susp_phrm_hdr temp17
              WHERE ( (temp17.prov_billing     = a.sak_prov
                   and     temp17.cde_service_loc     = a.cde_service_loc)
              OR (temp17.sak_prov_rend     = a.sak_prov
                   and     temp17.cde_svc_loc_rend = a.cde_service_loc)
              OR (temp17.sak_presc_prov     = a.sak_prov
                   and     temp17.cde_svc_loc_presc = a.cde_service_loc)
              OR     temp17.id_prov_prescrb     = to_char(a.sak_prov) || a.cde_service_loc
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp17.DTE_BILLED)     >= :il_prev_18_months
                   OR     NVL(MAX(temp17.DTE_BILLED),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim00.t_susp_phys_dtl temp18
              WHERE ( (temp18.sak_prov_perf     = a.sak_prov
                   and     temp18.cde_svc_loc_rend = a.cde_service_loc)
              OR (temp18.sak_prov_referring = a.sak_prov
                   and     temp18.cde_svc_loc_ref_1 = a.cde_service_loc)
              OR (temp18.sak_prov_referring_2 = a.sak_prov
                   and     temp18.cde_svc_loc_ref_2 = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp18.DTE_LAST_SVC)     >= :il_prev_18_months
                   OR     NVL(MAX(temp18.DTE_LAST_SVC),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim00.t_susp_phys_hdr temp19
              WHERE ( (temp19.prov_billing     = a.sak_prov
                   and     temp19.cde_service_loc     = a.cde_service_loc)
              OR (temp19.sak_prov_perf     = a.sak_prov
                   and     temp19.cde_perf_svc_loc = a.cde_service_loc)
              OR (temp19.sak_prov_referring = a.sak_prov
                   and     temp19.cde_svc_loc_ref_1 = a.cde_service_loc)
              OR (temp19.sak_prov_referring_2 = a.sak_prov
                   and     temp19.cde_svc_loc_ref_2 = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING ((MAX(temp19.DTE_LAST_SVC) >= :il_prev_18_months
                   OR     NVL(MAX(temp19.DTE_LAST_SVC),0) = 0)
                   and (MAX(temp19.DTE_BILLED)     >= :il_prev_18_months
                   OR     NVL(MAX(temp19.DTE_BILLED),0) = 0) )
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim00.t_susp_ub92_dtl temp20
              WHERE ( (temp20.sak_pr_other     = a.sak_prov
                   and     temp20.cde_scv_loc_other = a.cde_service_loc)
                   OR (temp20.sak_pr_other_2     = a.sak_prov
                   and     temp20.cde_svc_loc_other_2 = a.cde_service_loc)
                   OR (temp20.sak_pr_attend     = a.sak_prov
                   and     temp20.cde_svc_loc_attend = a.cde_service_loc)
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING     MAX(temp20.DTE_LAST_SVC)     >= :il_prev_18_months
                   OR     NVL(MAX(temp20.DTE_LAST_SVC),0) = 0
    union
    select a.sak_prov, a.cde_service_loc, a.sak_short from pr_php_elig_tmp a, aim00.t_susp_ub92_hdr temp22
              WHERE ( (temp22.prov_billing     = a.sak_prov
                   and     temp22.cde_service_loc     = a.cde_service_loc)
              OR (temp22.sak_pr_facility     = a.sak_prov
                   and     temp22.cde_svc_loc_fa     = a.cde_service_loc)
              OR (temp22.sak_pr_other_2     = a.sak_prov
                   and     temp22.cde_svc_loc_other_2 = a.cde_service_loc)
              OR (temp22.sak_pr_attend     = a.sak_prov
                   and     temp22.cde_svc_loc_attend = a.cde_service_loc)
              OR (temp22.sak_pr_other     = a.sak_prov
                   and     temp22.cde_svc_loc_other = a.cde_service_loc)
              OR     temp22.id_prov_attend     = to_char(a.sak_prov) || a.cde_service_loc
              OR     temp22.id_prov_other     = to_char(a.sak_prov) || a.cde_service_loc
              OR     temp22.id_prov_other_2     = to_char(a.sak_prov) || a.cde_service_loc
                   GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
              HAVING MAX(temp22.DTE_BILLED)     >= :il_prev_18_months
                   OR     NVL(MAX(temp22.DTE_BILLED),0) = 0
    );

    Perhaps an approach like this would work better. No promises, but it's worth a try.
    SELECT *
      FROM pr_php_elig_tmp a
    WHERE NOT EXISTS (
             SELECT   a.sak_prov,
                      a.cde_service_loc,
                      a.sak_short
                 FROM aim01.t_deny_dntl_dtl temp01
                WHERE temp01.sak_prov_perf = a.sak_prov
                  AND temp01.cde_svc_loc_perf = a.cde_service_loc
             GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
               HAVING MAX (temp01.dte_first_svc) >= :il_prev_18_months
                   OR NVL (MAX (temp01.dte_first_svc), 0) = 0)
       AND NOT EXISTS (
             SELECT   a.sak_prov,
                      a.cde_service_loc,
                      a.sak_short
                 FROM aim01.t_deny_dntl_hdr temp02
                WHERE (   (    temp02.prov_billing = a.sak_prov
                           AND temp02.cde_service_loc = a.cde_service_loc)
                       OR (    temp02.sak_prov_perf = a.sak_prov
                           AND temp02.cde_perf_svc_loc = a.cde_service_loc)
                       OR (    temp02.sak_prov_referring = a.sak_prov
                           AND temp02.cde_svc_loc_ref_1 = a.cde_service_loc)
                       OR (    temp02.sak_prov_referring_2 = a.sak_prov
                           AND temp02.cde_svc_loc_ref_2 = a.cde_service_loc))
             GROUP BY a.sak_prov, a.cde_service_loc, a.sak_short
               HAVING MAX (temp02.dte_final) >= :il_prev_18_months
                   OR NVL (MAX (temp02.dte_final), 0) = 0)

Maybe you are looking for

  • Can't open project, "catalog is too new"

    Error message reads: The document "CurrentVersion.fcpproject" could not be opened. The catalog is too new. So...how do I open the project?

  • Can't send mail due to local network IP

    I bought and installed a new Airport Extreme base station, everything's working so far with 2 wireless computers connecting online and an ethernet connection as well. The problem is with sending mail via entourage, the error message is "Entourage can

  • 10.1.3 EA - AppModule Quirks in BC Diagram

    Create an AppModule in a BC Diagram, drag a View Object with an attached View Link into the Module, then drag the View Link into the View Object Instance in the Module. What's supposed to happen is that the View Link brings with it the "other end", i

  • Program only gets focus if I click in the dock Icon

    If I have multiple programs visible (i.e. safari, firefox, skype) and click on one of them, it moves to the front, but I can NOT type in it, and the border does not turn grey, UNTIL I click on the icon in the dock. (using built in trackpad)

  • SAP PM or MM Which will be better

    Hello Everyone. I m Yash From Guj. I had done B.E.(I.T.) And workign in IT industry as a Software engg. I want to do SAP ,My programming level is not so good so in Technical I choose either BI/ Basis.. But i heard in BI does not have a lot opening an