Solved

SQL Query/Nested subquery  in Crystal Reports command Edit

Posted on 2014-01-22
4
1,477 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RDBMS and No sql database 4 72
Where on a calculated field 1 31
MySQL recovery 7 29
Survey branching tutorial 11 39
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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