Link to home
Start Free TrialLog in
Avatar of mojeaux
mojeauxFlag for United States of America

asked on

SQL Query/Nested subquery in Crystal Reports command Edit

I am running into a problem with an annual report that is hogging all my resources and causing my server to crash.   It's a very large query that includes about 8 tables and one view that hits another 6 tables.  

My objective is to obtain annual counts for columns in one table and group them by fields from another table.

The sql code I'm currently running from the command edit in crystal reports is...

SELECT COLUMN2 as FOLDID,
       COLUMN1 as APPLIDCD,
       COLUMN11 as USERID,
       COLUMN21 as DOCTYPE,
       COLUMN22 as FILETAB,
       COLUMN32 as NUMPAGES,
       VERSIONID,
       CREATETS
    FROM TABLE2299
     WHERE VERSIONID = 1
      AND  YEAR(CREATETS) = YEAR(CURRENT TIMESTAMP) - 1
      AND SUBSTR(COLUMN2,3)  IN ( SELECT POL_NO, COMP_NO, MKG_ORG
        FROM TABLE.POLCY                      
        WHERE YEAR(POL_ISSUE_DATE) > 2012 )  
UNION ALL
SELECT COLUMN2 as FOLDID,
       COLUMN1 as APPLIDCD,
       COLUMN11 as USERID,
       COLUMN21 as DOCTYPE,
       COLUMN22 as FILETAB,
       COLUMN32 as NUMPAGES,
       VERSIONID,
       CREATETS
    FROM TABLE2301
     WHERE VERSIONID = 1
      AND  YEAR(CREATETS) = YEAR(CURRENT TIMESTAMP) - 1
      AND SUBSTR(COLUMN2,3)  IN ( SELECT POL_NO, COMP_NO, MKG_ORG
        FROM TABLE.POLCY                      
        WHERE YEAR(POL_ISSUE_DATE) > 2012 )  
UNION ALL
SELECT COLUMN2 as FOLDID,
       COLUMN1 as APPLIDCD,
       COLUMN11 as USERID,
       COLUMN21 as DOCTYPE,
       COLUMN22 as FILETAB,
       COLUMN32 as NUMPAGES,
       VERSIONID,
       CREATETS
    FROM TABLE2302
     WHERE VERSIONID = 1
      AND  YEAR(CREATETS) = YEAR(CURRENT TIMESTAMP) - 1
      AND SUBSTR(COLUMN2,3)  IN ( SELECT POL_NO, COMP_NO, MKG_ORG
        FROM TABLE.POLCY                      
        WHERE YEAR(POL_ISSUE_DATE) > 2012 )  
UNION ALL
SELECT COLUMN2 as FOLDID,
       COLUMN1 as APPLIDCD,
       COLUMN11 as USERID,
       COLUMN21 as DOCTYPE,
       COLUMN22 as FILETAB,
       COLUMN32 as NUMPAGES,
       VERSIONID,
       CREATETS
    FROM TABLE2303
     WHERE VERSIONID = 1
      AND  YEAR(CREATETS) = YEAR(CURRENT TIMESTAMP) - 1
      AND SUBSTR(COLUMN2,3)  IN ( SELECT POL_NO, COMP_NO, MKG_ORG
        FROM TABLE.POLCY                      
        WHERE YEAR(POL_ISSUE_DATE) > 2012 )  
WITH UR;      

Can this be rewritten so that i only pull the counts for doctype and sum for numpages where it's grouped by comp_no and/or mkg_org?   Rather than pull all record information back and then calculate counts and groups?

Any help would be greatly appreciated.   Thanks!
Avatar of vasto
vasto
Flag of United States of America image

I would try this:

Create a view to union the data from the 3 tables
SELECT COLUMN2 as FOLDID,
       COLUMN1 as APPLIDCD,
       COLUMN11 as USERID,
       COLUMN21 as DOCTYPE,
       COLUMN22 as FILETAB,
       COLUMN32 as NUMPAGES,
       VERSIONID,
       CREATETS
FROM TABLE2301
WHERE VERSIONID = 1
UNION ALL
SELECT COLUMN2 as FOLDID,
       COLUMN1 as APPLIDCD,
       COLUMN11 as USERID,
       COLUMN21 as DOCTYPE,
       COLUMN22 as FILETAB,
       COLUMN32 as NUMPAGES,
       VERSIONID,
       CREATETS
FROM TABLE2302
WHERE VERSIONID = 1
UNION ALL
SELECT COLUMN2 as FOLDID,
       COLUMN1 as APPLIDCD,
       COLUMN11 as USERID,
       COLUMN21 as DOCTYPE,
       COLUMN22 as FILETAB,
       COLUMN32 as NUMPAGES,
       VERSIONID,
       CREATETS
FROM TABLE2303
WHERE VERSIONID = 1

Open in new window


Then use this code to join the new view and Policies:
SELECT FOLDID,APPLIDCD, USERID,DOCTYPE, FILETAB,NUMPAGES, VERSIONID, CREATETS
FROM NewView nv INNER JOIN (SELECT POL_NO
        FROM TABLE.POLCY                      
        WHERE YEAR(POL_ISSUE_DATE) > 2012) temp ON SUBSTR(nv.FOLDID,3) = temp.POL_NO

Open in new window



My guess is that COLUMN2 contains part of POL_NO. You should change this part if this is wrong.
Avatar of mojeaux

ASKER

Thank you.   Your guess is correct about the partial POL_NO in Column2.  Still having issues with creating views due to permissions of the report ID on the server.  

What if I changed the query and only brought back the count of column21 and not all the records?   Would this be more effiecient?

Could it then be written..

SELECT  COUNT(COLUMN21) AS DOCCNT
    FROM TABLE2299
     WHERE VERSIONID = 1
      AND APPLIDCD = 2
      AND  YEAR(CREATETS) = YEAR(CURRENT TIMESTAMP) - 1
      AND SUBSTR(COLUMN2,3)  IN ( SELECT POL_NO, COMP_NO, MKG_ORG
        FROM TABLE.POLCY                      
        WHERE YEAR(POL_ISSUE_DATE) > 2012 )  
UNION ALL
SELECT  COUNT(COLUMN21) AS DOCCNT
    FROM TABLE2301
     WHERE VERSIONID = 1
      AND APPLIDCD = 2
      AND  YEAR(CREATETS) = YEAR(CURRENT TIMESTAMP) - 1
      AND SUBSTR(COLUMN2,3)  IN ( SELECT POL_NO, COMP_NO, MKG_ORG
        FROM TABLE.POLCY                      
        WHERE YEAR(POL_ISSUE_DATE) > 2012 )  
UNION ALL
... AND SO ON...
WITH UR;
ASKER CERTIFIED SOLUTION
Avatar of vasto
vasto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mojeaux

ASKER

Thank you!   I was able to get this going.   It was also having an issue with the functions being used in the query which is why it was being slowed down so much.   I had already removed count but also had to remove the year as well.   Thanks again!