Solved

SQL Query/Nested subquery  in Crystal Reports command Edit

Posted on 2014-01-22
4
1,471 Views
Last Modified: 2014-01-22
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!
0
Comment
Question by:mojeaux
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:vasto
ID: 39800210
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.
0
 

Author Comment

by:mojeaux
ID: 39801016
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;
0
 
LVL 18

Accepted Solution

by:
vasto earned 500 total points
ID: 39801190
Try to use a subquery instead
SELECT FOLDID,APPLIDCD, USERID,DOCTYPE, FILETAB,NUMPAGES, VERSIONID, CREATETS
FROM (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) 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


What I am trying to do is to avoid using.
WHERE ....  SUBSTR(COLUMN2,3)  IN ( SELECT POL_NO, COMP_NO, MKG_ORG
        FROM TABLE.POLCY                      
        WHERE YEAR(POL_ISSUE_DATE) > 2012 )

and to add the TABLE.POLCY in a join
0
 

Author Closing Comment

by:mojeaux
ID: 39801782
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!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question