Click here to Skip to main content
Licence 
First Posted 12 Aug 2004
Views 66,792
Bookmarked 22 times

Retrieving Mutually Exclusive Records using Seagate Crystal Reports

By | 12 Aug 2004 | Article
An article explaining the retrieval of mutually exclusive records using Crystal Reports with VC++, and unleashing the problems with solutions.
 
Part of The SQL Zone sponsored by
See Also

Introduction

I had a requirement in my application to retrieve mutually exclusive or disjoint sets of data into our system. Two sets A and B are said to be mutually exclusive or disjoint if they have nothing in common. Our system later would execute these queries to retrieve disjoint data.

Background

Data Retrieval and Manipulation in SQL

There are three different but related forms of relational calculus: tuple calculus, domain calculus, and transform languages. SQL is based on the transform language SEQUEL.

Commands in SQL specify in a specific syntax which columns to manipulate, from what tables, and for what rows.

Basic Data Retrieval and Manipulation in SQL

SQL data retrieval statements include the following three distinct clauses:

SELECT

Lists the columns (including expressions involving columns) from base tables or views to be projected into the tables that will be the result of the command.

FROM

Identifies the tables or views from which columns will be chosen to appear in the result table, and includes the tables or views needed to join tables to process the query.

WHERE

Includes the conditions for row selection within a single table or view, and the conditions between tables or views for joining.

Example Database

Before getting into the details, let me illustrate it with a simple example. Suppose that we have the following database:

There are a few remarks to make about it:

  • All tables have primary keys.
  • PAYMENT_DETAIL has a multiple field or composite primary key.
  • BILL_PAYMENT.PAYMENT_MODE contains either a value Cash (0) or a value Other (1).
  • Data is stored in PAYMENT_DETAIL table, if and only if BILL_PAYMENT.PAYMENT_MODE value is Other.
  • The data is divided into two mutually exclusive or disjoint sets based on the value of BILL_PAYMENT.PAYMENT_MODE.

  • We connect to the database using ODBC.
  • The User DSN is Bills.

Table Operations

In a relational system, data from related tables are combined into one table (or view), and then displayed, or used as input to a form or report definition. Thus, the majority of relational database programming involves combining into one table, data from two, three, or more related tables.

Retrieving Mutually Exclusive Records

Once the database is constructed, now is the time to retrieve records from the database. The simplest solution will be:

SELECT BP.BILL_ID, BP.INSTALLMENTNO, BP.INSTALLMENT_AMOUNT, 
                   BP.INSTALLMENT_PAID_DATE, BP.PAYMENT_MODE,
    (
        SELECT PD.DESCRIPTION
        FROM PAYMENT_DETAIL AS PD
        WHERE 
            PD.BILL_ID = BP.BILL_ID
            AND
            PD.INSTALLMENTNO = BP.INSTALLMENTNO
            AND
            BP.PAYMENT_MODE = 1
    )
FROM BILL_PAYMENT AS BP
WHERE
    BP.BILL_ID IN     (
                SELECT BI.BILL_ID
                FROM BILL_INFO AS BI
            );

In the preceding subquery solution, it was necessary to examine the second inner query before considering the outer query. That is, the result of the inner query is used to limit the processing of the outer query. In contrast, for other kinds of queries, called correlated subqueries, such as the first inner query, the processing of the inner query depends on data from the other query.

Correlated subqueries may seem difficult to write at first, but once you understand that your query needs to process one table for each row of another table, the use of correlated subqueries becomes clear. Sometimes, depending on how you conceive a query, you can answer the same question by either non-correlated or correlated subqueries or even without using subqueries. Unfortunately, that's what we shall do here. Because, the encircled column in the following snapshot is actually an expression involving column. And Seagate Crystal Reports 8.0 doesn't allow us to use expressions involving columns as fields in a report. Although Seagate Crystal Reports 8.0 supports a special field called the "Formula Field" which allows expressions involving columns, it doesn't allow the use of SQL in Formula Field, which is our requirement here.

Power of SQL

Because relational query languages like SQL are set-oriented languages (that is, commands operate on and generate sets of rows), the equivalent of various set operations may also be available.

Appending Query Results Together

The UNION command combines the result of two queries into one table as long as the two tables being combined have compatible corresponding columns. That is, the two tables must have the same number of columns, and the corresponding columns must have the same data type. The results from multiple queries may be combined by inserting UNION between each query.

  • SQL

    (
        SELECT BI.BILL_ID, BI.ISSUE_DATE, BI.CLIENT_NAME, 
            BI.TOTAL_AMOUNT, BI.DISCOUNT, BI.BALANCE,
            BP.INSTALLMENTNO, BP.INSTALLMENT_AMOUNT, 
            BP.INSTALLMENT_PAID_DATE, BP.PAYMENT_MODE,
            PD.DESCRIPTION
        FROM     (
                BILL_INFO AS BI INNER JOIN BILL_PAYMENT 
                AS BP ON BI.BILL_ID = BP.BILL_ID
            ) 
            INNER JOIN 
                PAYMENT_DETAIL AS PD ON BP.BILL_ID = PD.BILL_ID 
                AND 
                BP.INSTALLMENTNO = PD.INSTALLMENTNO
        WHERE 
            BP.PAYMENT_MODE = 1 
            AND 
            PD.BILL_ID = BP.BILL_ID 
            AND 
            PD.INSTALLMENTNO = BP.INSTALLMENTNO
        ORDER BY 
            BI.BILL_ID ASC, BI.ISSUE_DATE ASC
    )
    UNION
    (
        SELECT BI.BILL_ID, BI.ISSUE_DATE, BI.CLIENT_NAME, 
            BI.TOTAL_AMOUNT, BI.DISCOUNT, BI.BALANCE,
            BP.INSTALLMENTNO, BP.INSTALLMENT_AMOUNT, 
            BP.INSTALLMENT_PAID_DATE, BP.PAYMENT_MODE,
            ''
        FROM BILL_INFO AS BI, BILL_PAYMENT AS BP
        WHERE 
            BP.PAYMENT_MODE = 0 
            AND 
            BI.BILL_ID = BP.BILL_ID
        ORDER BY BI.BILL_ID ASC, BI.ISSUE_DATE ASC
    }
  • C++

    CString Query1, Query2;
    
    Query1 = "SELECT
            BI.`BILL_ID`, BI.`ISSUE_DATE`, BI.`CLIENT_NAME`, 
                 BI.`TOTAL_AMOUNT`, BI.`DISCOUNT`, BI.`BALANCE`, 
            BP.`INSTALLMENTNO`, BP.`INSTALLMENT_AMOUNT`, 
                 BP.`INSTALLMENT_PAID_DATE`, BP.`PAYMENT_MODE`,
            PD.`DESCRIPTION`
         FROM
            (`BILL_INFO` BI INNER JOIN 
            `BILL_PAYMENT` BP ON BI.`BILL_ID` = BP.`BILL_ID`) INNER JOIN
            `PAYMENT_DETAIL` PD ON BP.`BILL_ID` = PD.`BILL_ID` 
                  AND BP.`INSTALLMENTNO` = PD.`INSTALLMENTNO`
         WHERE
            BP.`PAYMENT_MODE` = 1 AND PD.`BILL_ID` = 
                 BP.`BILL_ID` AND PD.`INSTALLMENTNO` = BP.`INSTALLMENTNO`
         ORDER BY 
            BI.`BILL_ID` ASC, BI.`ISSUE_DATE` ASC";
    
    Query2 = "SELECT
            BI.`BILL_ID`, BI.`ISSUE_DATE`, BI.`CLIENT_NAME`, 
                 BI.`TOTAL_AMOUNT`, BI.`DISCOUNT`, BI.`BALANCE`, 
            BP.`INSTALLMENTNO`, BP.`INSTALLMENT_AMOUNT`, 
                 BP.`INSTALLMENT_PAID_DATE`, BP.`PAYMENT_MODE`,
            ''
         FROM 
            BILL_INFO BI, BILL_PAYMENT BP
         WHERE 
            BP.`PAYMENT_MODE` = 0 AND BI.`BILL_ID` = BP.`BILL_ID`
         ORDER BY BI.`BILL_ID` ASC, BI.`ISSUE_DATE` ASC";
    
    CString Query = Query1+" UNION "+Query2;
    
    m_CrystalReport.SetReportFileName("Report.rpt");
    m_CrystalReport.SetWindowTitle("Retrieving Mutually Exclusive 
                          Records with Seagate Crystal Reports");
    m_CrystalReport.SetSQLQuery(Query);
    m_CrystalReport.PrintReport();

The encircled portion of the report shows, that is just about it. Hope it helps some of you folks out there.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Muneeb Ahmed Awan



Pakistan Pakistan

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberali_gooly200123:47 16 Apr '11  
Generalhello......again Pinmemberlallaba0:37 5 Dec '05  
Generalplz help ..... Pinmemberlallaba1:00 30 Nov '05  
GeneralRe: plz help ..... PinmemberMuneeb Ahmed Awan19:19 2 Dec '05  
GeneralRe: plz help ..... PinmemberMuneeb Awan20:16 7 Dec '05  
Generalplease i need help.... Pinmemberlallaba23:35 12 Nov '05  
GeneralRe: please i need help.... PinmemberMuneeb Ahmed Awan19:35 18 Nov '05  
GeneralcRYSTAL REPORT NOT REFERESHING PinmemberShanmuga Sundar0:04 5 May '05  
GeneralRe: cRYSTAL REPORT NOT REFERESHING PinsussAnonymous2:44 6 May '05  
Questionhow to get seagate reports into crystal? PinsussAnonymous23:54 25 Apr '05  
AnswerRe: how to get seagate reports into crystal? PinsussAnonymous18:24 29 Apr '05  
Generalswitch between db servers PinmemberTomSyk13:26 19 Mar '05  
GeneralHelp me quering another file Pinmemberop_oap16:44 3 Mar '05  
GeneralRe: Help me quering another file PinmemberMuneeb Ahmed Awan17:22 3 Mar '05  
GeneralRe: Help me quering another file Pinmemberop_oap16:29 6 Mar '05  
GeneralRe: Help me quering another file PinmemberMuneeb Ahmed Awan17:14 6 Mar '05  
GeneralRe: Help me quering another file Pinmemberop_oap16:33 10 Mar '05  
GeneralNeed help! PinmemberBlackDice10:57 19 Nov '04  
GeneralRe: Need help! PinmemberMuneeb Ahmed Awan17:14 20 Nov '04  
GeneralCannot run in Visual c++.NET PinmemberBahrudin Hrnjica7:00 19 Nov '04  
GeneralMutiple line PinmemberSébastien Lorion13:45 18 Aug '04  
GeneralFacing problem in Crystal report Pinmemberraghute_h22:42 15 Aug '04  
GeneralRe: Facing problem in Crystal report PinmemberMax Santos1:59 25 Aug '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 13 Aug 2004
Article Copyright 2004 by Muneeb Ahmed Awan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid