Cognos Analytics

 View Only
  • 1.  SQL Exists within a Report

    Posted Thu October 10, 2024 11:53 AM

    Hi all,

    I know I can do a filter such as 

    [Order Method Code] in ( [Query2].[Order Method Code] )

    within a report.

    Traditionally in SQL such a filter would be written as an EXISTS statement as it usually faster than an IN statement when the query referenced within the IN is a large result set.

    Is there anyway of writing a SQL EXISTS within a CA report query?

    Thanks



    ------------------------------
    Marc Reed
    ------------------------------


  • 2.  RE: SQL Exists within a Report

    Posted Fri October 11, 2024 10:27 AM

    Hi Mark,

    you can link qeueries in report studio. Just create two queries and place the needed data items in them and then drag one next to the other:

    You can now use the data from Querey1 in the filter area in Query2:

    Another way is to use {}. Everything between them will be pushed directly to the database system without any interpretation from Cognos. Yo oyu can place any valid manual written SQL in there. Give it a try.



    ------------------------------
    Robert Dostal
    Team Leader BI
    GEMÜ Gebr. Müller Apparatebau GmbH & Co. KG
    Ingelfingen
    ------------------------------



  • 3.  RE: SQL Exists within a Report

    Posted Mon October 14, 2024 07:27 AM

    @Robert Dostal - i am intrigued by your two suggestions but do not really understand either one. if you have time, can you mock up a simple example?



    ------------------------------
    brenda grossnickle
    BI Programmer Analyst
    FIS
    ------------------------------



  • 4.  RE: SQL Exists within a Report

    Posted Mon October 14, 2024 07:58 AM
    Edited by Marc Reed Mon October 14, 2024 08:00 AM

    @brenda grossnickle I believe Robert is explaining this behaviour in reporting:  


    you can use data items from Query 2 in Query 1. Technically you don't need to link the queries as shown you can just use the syntax [Query2].[data item] in query 1. You have to be careful in doing this though as you have to understand the implications of the SQL you are generating. 

    Filters, such as an IN are usually no cause for concern. But using objects from Q2 in Q1 as data items, as well as other data items in Q1 can result in unintended sql.



    ------------------------------
    Marc Reed
    ------------------------------



  • 5.  RE: SQL Exists within a Report

    Posted Mon October 14, 2024 07:52 AM

    Hi Robert,

    Yes - fully aware that you can data items from one query into another. The example in my initial post ( [Order Method Code] in ( [Query2].[Order Method Code] ) shows how to do that. You could also achieve similar with a join between two queries.

    Within SQL the rule of thumb is that EXISTS is usually better than an IN when there are a large number of rows in the lower query, so was looking to see if anyone had found a method of using the SQL exists syntax within CA reports.



    ------------------------------
    Marc Reed
    ------------------------------



  • 6.  RE: SQL Exists within a Report

    Posted Mon October 14, 2024 08:17 AM

    Hi,
    is it just me or is the website missing all uploaded screenshots? I don't see them in my older posts nor in the latest ones...

    Regarding the discussion topic: you can wrap the whole filter text into {} and write the statement manually. Should work as Cognos will pass the whole text right next to "Where" in the generated SQL query.



    ------------------------------
    Robert Dostal
    Team Leader BI
    GEMÜ Gebr. Müller Apparatebau GmbH & Co. KG
    Ingelfingen
    ------------------------------