Solved

SQL Query/Nested subquery  in Crystal Reports command Edit

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now