Solved

SQL Query/Nested subquery  in Crystal Reports command Edit

Posted on 2014-01-22
4
1,505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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