Comparing two queries yields one result too many

I have a problem that I'm pretty sure is resident in the
structure of a loop, but I'm not quite sure how to fix it.
All of this is being done within a cfc. The cfc calls the
first method for Query1, then calls the second method for Query2.
Query1 has 173 records, Query2 has 117 records. Technically the
difference should be 56 records.
However, the result of myquery (below) is giving me 57
records. And every one of them is a real record. Code as follows:
<CFSET myquery = QueryNew("var1, var2, var3, var4,
var5")>
<CFLOOP INDEX="i" FROM="1" TO="#Query1.recordcount#">
<CFQUERY NAME="checkJob" DBTYPE="query">
SELECT var1
FROM Query2
WHERE var1 = <cfqueryparam cfsqltype="cf_sql_varchar"
value="#Query1.var1
#">
</CFQUERY>
<CFIF checkJob.recordcount lte 0>
<cfset newRow = QueryAddRow(myQuery, 1)>
<cfset temp = QuerySetCell(myQuery, "var1",
Query1.var1)>
<cfset temp = QuerySetCell(myQuery, "var2", Query1.var2
)>
<cfset temp = QuerySetCell(myQuery, "var3",
Query1.var3)>
<cfset temp = QuerySetCell(myQuery, "var4", Query1.var4
)>
<cfset temp = QuerySetCell(myQuery, "var5",
Query1.var5)>
</CFIF>
</CFLOOP>
<CFRETURN myQuery>
So if all is done correctly, I should be getting the results
from Query1 that are NOT in Query2.
It's *almost* right.
Since var1 exists in all of the records returned by myquery,
one presumes that there's an extra record being returned that *IS*
in Query2.
I'm not sure why, though.
The resultant screen needs to print out all of the variables
from query1 that do not exist in query2. Hence, myquery.
Anyone have a better recommendation on how to fix this
problem? I feel like a goober for even asking it, but it's been
annoying me all afternoon.
Rizados

Simon, I would appreciate not being called lazy and yet
overly complicated in the same sentence. Ignorant, perhaps, overly
complicated, very likely, but lazy isn't the word I'd use.
When it comes to MSSQL, I was actually unaware I could use a
query of queries approximation without writing the whole thing out.
Thank you so much for correcting my ignorance. My apologies if this
isn't the type of database I've been using for the last few years,
and if my familiarity with the database is not as great as yours.
I have been trying to componentize this application where
applicable, and the queries I speak of were already componentized
prior to me coming into this. Both the primary and secondary
queries are being used by multiple applications on the same server,
and multiple times in the same page with different parameters (the
same page is being used several times). This third query is
obviously the difference between the two. If the second query has
to change at any time, then changing it only once within a
component makes more sense than having to find it multiple times
within the code to change it again, since the business rules on
said query have had to change a few times before. Better to have it
in one place than to change it in multiple places and risk having
one be changed while the others are not.
Lazy has nothing to do with it so much as accuracy. Multiple
instances of the same code means having to find it multiple times
when the code needs to change, and risking that you may miss one.
If they allowed me to use a view to do it as recommended by
cf_dev2, I would do so, and it would make my life a lot less
complicated. However, that option is not available to me, therefore
I have to go the "more complicated" route.

Similar Messages

  • How to Combine two queries into One

    Hi Gurus,
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Release 10.2.0.4.0 - 64bit Production
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for Linux: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    SQL>
    I have following two queries. How I can convert these two queries into one with the same result ?
    Q1
    SELECT id,vdate,vendorname,venddate,vid
    FROM VENDOR
    WHERE processid=32
    AND venddate is null
    AND vid in(select vid from view_vid where type='PDX')
    AND (id,vdate) not in (select id,vdate from vkfl_is where task_id=55)
    AND (id,vdate) in (select id,vidate from market_data where marketed_date is null)
    AND id not in (select id from location)
    Q2
    SELECT id,vdate,vendorname,venddate,vid
    FROM VENDOR
    WHERE processid=20
    AND venddate is null
    AND vid in(select vid from view_vid where type='PBX')
    AND (id,vdate) not in (select id,vdate from vkfl_is where task_id=40)
    AND (id,vdate) in (select id,vidate from market_data where region_date is null)
    AND id not in (select id from location)
    I can UNION these two queries, but, is there any way I can write a single query with which gives me same result as above two queries?
    Any help would be highly appreciated
    Thanks in advance

    Hi,
    You can do something like this, which will be more efficient than a UNION:
    SELECT  id, vdate, vendorname, venddate, vid
    FROM  vendor
    WHERE  (   (    -- Conditions that apply only to q1
                               processid = 32
                AND  vid       IN (SELECT vid FROM view_vid WHERE type = 'PDX')
                AND (id,vdate) NOT IN ( SELECT id, vdate
                                        FROM   vkfl_is
                                        WHERE  task_id = 55
                AND (id,vdate) IN ( SELECT id, vidate
                                    FROM   market_data
                                    WHERE  marketed_date IS NULL
            OR  (  -- Conditions that apply only to q2
                     processid = 20
                AND  vid       IN (SELECT vid FROM view_vid WHERE type = 'PBX')
                AND (id,vdate) NOT IN ( SELECT id, vdate
                                        FROM   vkfl_is
                                        WHERE  task_id = 40
                AND (id,vdate) IN ( SELECT  id, vidate
                                    FROM  market_data
                                    WHERE  region_date IS NULL
                   -- The remaining conditions apply to both q1 and q2
    AND     venddate   IS NULL
    AND     id         NOT IN (SELECT id FROM location)
    I hope this answers your question.
    If not, post  a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • Two Queries in one XSQL-Template

    Hi there,
    is it possibile to use two queries in one xsql-template, which are generated two seperated trees of XML?
    I think about something like that
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsql:query connection="bynet"
    null-attribute-indicator="true"
    xmlns:xsql="urn:oracle-xsql"
    rowset-element = "COLLECTION"
    tag-case="upper"
    row-element = "FIELD"
    id-attribute = "">
    select c_formtype from offer_collection
    </xsql:query>
    <xsql:query connection="bynet"
    null-attribute-indicator="true"
    xmlns:xsql="urn:oracle-xsql"
    rowset-element = "COLLECTION2"
    tag-case="upper"
    row-element = "FIELD"
    id-attribute = "">
    select c_formtype from offer_collection2
    </xsql:query>
    which should give me
    <COLLECTION>
    <FIELD>
    <C_FORMTYPE>lala</C_FORMTYPE>
    </FIELD>
    </COLLECTION>
    <COLLECTION2>
    <FIELD>
    <C_FORMTYPE>lala</C_FORMTYPE>
    </FIELD>
    </COLLECTION2>
    Any way to do this?
    Thanx for help,
    Christian Hartmann

    Sure, just enclose two <xsql:query> tags by any other outer tag:
    <something xmlns:xsql="urn:oracle-xsql" connection="demo">
    <xsql:query> select * from emp </xsql:query>
    <xsql:query> select * from dept</xsql:query>
    </something>

  • Combining two queries in one..

    Hi,
    Can someone tell me how to combine follwoing two queries in one so that it will increase performance..
    SELECT single PLNNR from AFKO
           into T1
           WHERE AUFNR = '007200000100'.
    SELECT single STATU from PLKO
           into T2
           WHERE PLNNR = T1.

    using subqueries it is possible
    select single statu from plko into t2 where plnnr in (select single plnnr from afko where aufnr eq '007200000100').
    read about subqueries at this link...
    http://help.sap.com/saphelp_erp2004/helpdata/en/dc/dc7614099b11d295320000e8353423/frameset.htm
    regards,
    PJ

  • Comparing two queries with VBA macro

    Hi,
    I have a workbook woth 2 sheets. Every sheet has a BEx query (so 2 queries).
    I need, for every column in the first query, to check if it is present in the second query, to generate a list with the NOT PRESENT values (or simply, marking the not present values).
    I tried with Searching excel functions by column, but every query hs more that 30.000 rows, so it takes a long time.
    I think I have to use an excel macro. I have been lookong for in the forum but my doubt is that I have to execute when the two queries has been refreshed.
    How can I know when the two queries has been refreshed?
    Is there any other way to do it?
    Any idea of how to reference a query from the other?
    I think the code shoud be in this way:
    for every row in the first colum of 1st query
    look in every row in the first column of 2nd query
    mark in first query if not present.
    If I put the macro in 1st query, how can I reference the 2nd one?

    Hi Oscar, there are several ways to do this.
    The easiest way would be:
    1.  in the SAPBEXonRefresh subroutine (which you will find in the SAPBEX Module attached to your query-containing workbook), add code that looks something like this:
    Set ws = resultArea.parent
    if ws is nothing then exit sub
    resultArea.name = ws.codename & "_results"
    ws.Range("D2") = Date
    if queryID = "SAPBEXq0002" then Call CompareQueries(queryID)
    'make the query ID that of the second query
    This will create named ranges in Excel so that you can easily locate the result tables later.  And, ensure that you know the date that each query was refreshed.  And, after the second query is refreshed, it will call your query comparison routine.
    2.  In the comparison routine, you will need something like this:
    set ws1 = Query1 'assuming that you have set the code name of the first query sheet as Query1
    set ws2 = Query2 'assuming that you have set the code name of the second query sheet as Query2
    'set ranges for the two result tables
    set Range1 = ws1.codename & "_results"
    set Range2 = ws2.codename & "_results"
    'locate first and last Row and Column for result tables
    firstRow1 = Range1.cells(1).row
    firstCol1 = Range1.cells(1).column
    numCells = Range1.cells.count
    lastRow1 = Range1.cells(numCells).row
    lastCol1 = Range1.cells(numCells).column
    'do same for Range2
    'locate the columns you want to compare
    searchCol = 0
    for j = firstCol1 to lastCol1
        if cells(firstRow1, j) like "some text here" then
            searchCol = j
            exit for
        end if
    next j
    if searchCol = 0 then
        msgbox "Did not find ""some text here"".", vbCritical
        exit sub
    Else:
        set SearchRange = ws1.cells(1, searchCol).entireColumn
    end if
    for j = firstCol2 to lastCol2
        if cells(firstRow2, j) like "some text here" then
            searchCol = j
            exit for
        end if
    next j
    'do the search
    for i = firstRow2 to lastRow2
        searchFor = cells(i, searchCol)
        matchRow = 0
        on error resume next
        matchRow = Application.worksheetfunction.match(searchFor, SearchRange, 0)
        if matchRow > 0 then
            cells(i, lastCol2+1) = "match found on row " & matchRow
        Else:
            cells(i, lastCol2+1) = "NOT PRESENT"
        End if
    next i
    - Pete

  • How to combine two queries in one EXCEL

    hi i got one requirement
    i got one excel sheet  from user which contains two  report s(hoe we know whether it is worknook or not) of  2011 data and now user wants 2012 data .now i icluded jan 2012 to dec 2012 in two reports and sent to user.But user is asking these two reports want to see in one sheet.
    so how can i proceed: if i go for work  book  how can i include these two reports in workbook and here my doubt is if i create a workbook based on  these two querie which name i have to give user.Please help me out on the same.
    Regards,
    Madhu.

    Hi Madhu
    Run one query. Click on a empty space in the excel
    From the BEx Design Toolbar -
    >Insert Analysis Grid -
    > Right click on the Design Item
    Edit the DataProvider.....You can ran same or different query with selection. 
    Save the result
    Check this link for detail....I am also searching for something with screenshot
    Regards
    Anindya
    Edited by: Anindya Bose on Feb 14, 2012 3:04 AM

  • Two Queries in one Worksheet

    Hi,
    I'd like to know what the disadvantages are of having two queries in the same worksheet of a workbook. Is it correct that the results of one query can overflow to the area of the second? How about issues with authorization, are there any?
    Thanks!

    HI,
    Yes, result of one can flow over the other if you are using simple report with characteristics and output may vary.
    Actually you apply basic Excel logic and its like 2 diff report on it.
    Mostly you add more Fixed format reports to a single sheet. So that  number of report rows dont increase and also you dont have any free characteristics to drilldown.
    About authorization, that depends on individual reports. If a user doesnt have proper authorization for 1 of the reports then that report would throw error while other would show result.

  • Combining two queries to one

    I have two queries and I would like to output the results as
    one and sort by Name ASC
    Any help would be great thanks

    Either use union in your SQL query or use Coldfusion to
    combine exisitng queries and create a new query resultset.
    Something like this:
    <CFQUERY NAME="getDetailsQuery1"
    DATASOURCE="#DSNNAME#">
    SELECT * FROM getDetailsA
    </CFQUERY>
    <CFQUERY NAME="getDetailsQuery2"
    DATASOURCE="#DSNNAME#">
    SELECT * FROM getDetailsB
    </CFQUERY>
    <CFQUERY NAME="getDetails" DBTYPE="query">
    SELECT * FROM getDetailsQuery1
    UNION
    SELECT * FROM getDetailsQuery2
    ORDER BY Name ASC
    </CFQUERY>
    Hope this helps!
    Cheers / Manu.

  • Two queries in one output in BEx is it possibl ?

    Hi,
               Is there a possibility in Bex Excel screen that we can output two queries at the same time , like 1st query in first half and second query 2nd half of the screen .I think its not possible but just wanna know if anyone came across such an issue
    I know it is possible with WAD ...

    Hi Raj,
    You can execute 2 queries in one work book.First execute one query,place the cursor under query results area,go to tools->Insert query->execute.Save->Save existing work book.
    When ever u open the work book u have to execute both the queries.Instead of executing twice you can use some options, so that it saves and reuses the input values and automatically refresh the queries when u open the work book(If the input values are fixed).
    For that right click->Properties->Intraction tab
    You can see 4 check boxes.check all boxes,ok for all queries and save.So when u open the work book it retrieves data.No need to give input values

  • Two queries in one report?

    I have Three tables like
    Device_table columns are
    d_id, d_name, date
    Software_table
    sw_id, software, version, serial_no, sw_type, d_id(foreign_key)
    hardware_table
    hw_id, hardware, specification, d_id(foriegn_key)
    now in reports i want to attach both hardware and software, I have made two quires like
    SELECT d.d_name, sw.software, sw.version, sw.sw_type from
    device_table d, software_table sw
    where d.d_id = sw.d_id
    and second query is
    SELECT d.d_name, hw.hardware, hw.specification
    from device_table d, hardware_table hw
    where d.d_id = hw.d_id
    Now my problem is that how can I add both queries in one report so that i can get a list of software and hardware attached to one device.
    Regards
    Maz

    I want to see the report as follow.
    d_name: CPU-1
    date: -----------
    Software installed:
    sw_id, software, version, serial_no, sw_type
    (list of softwares attached, can be more then one)
    Hardware attached:
    hw_id, hardware, specification (same like software list, it will contain hardware list attached to specific device),
    Please if you can help me that how will i make a query for this.
    Thanks and Regards
    Maz

  • Combining result set of two services into one result set

    Hi,
    I have a model where I am getting one result set (6 Fields) from BI query and another result set (3 Fields which gives real time values) from OLTP system. I want to show the output of these two result sets into single result set (7 Fields). I have a common field in both the result sets as key field.
    I tried this using UNION operator but I am not able to set the Key fields and also in the table view of UNION operator I am getting any field to be added to table.
    Can anyone help me to solve this problem?
    Any help is appreciated.
    Regards,
    Amit

    Hi Amit,
    in this case, you have to use the combine-operator. However, this operator needs a key-field in each of these sets. E.g. Combine set (<u>customer-id</u>, order-id, week, delivery-state) with set (<u>customer-id</u>, city, client-class).
    The combine operator might not be available when compiling to webdynpro, I did not check that.
    Best Regards, Benni

  • Compare two queries of two different system.

    Hello all,
           I need to certify the some queries, the process we follow is as below.
    1. The query will be created in Production system, we will transport back to Development server.
    2. Compare the query (Characterstics, Key Figures and Definition), In other words I need to compare and validate each and every aspect of Query with the Development and Production.
    3. I need to change the query if I find anything difference in Prod and Dev system.
    4. Transport to the Production.
    Here my problem is I'm comparing manually by opening RRMX in each of the system and each variable, its time consuming as it is a manual process there is a chance of missing some important. So I would like to know is there any way to compare the queries Prod and Dev server in easier way than manually.
    I hope requirement is clear to you or else just let me know your concerns and thoughts on this.
    Thanks in advance,
    Umashankar

    Umashankar,
    I dont think there is an easy way to do this as you are asking for - however you can do a basic level comparison as to number of query ellements , variables that are there etc - this can be done using the table rszcompdir which has all the query elements of the query but then a direct one to onw comparison may not be possible since the query elements are identified by GUIDs which usually will be different across systems.

  • Combining two tables into one result

    Hi all, I am fairly inexperienced with Crystal but have gained good results from the reports I have created, I am fluent in SQL and Basic to an extent.  I am interigating a Pronto database and I want concatenate two tables into one report.
    Table One: Customer Orders
    Fields:  Customer No, Invoice Number, Invoice date, Invoice Amount, Cost Amount.
    Table Two: Archived Customer Orders
    Fields:  Customer No, Invoice Number, Invoice date, Invoice Amount, Cost Amount.
    I want to complete a report that will tell me the sales for a given period and the outstanding orders for each customer total for all fields in each table combined into one figure.
    Any help would be greatly appreciated.
    Thanks in advance

    Hi Grant,
    As you said you are goot at writing SQL, in Crystal write a SQL statement in Add Command to join both table using Union / Union all
    If you use Union then it will filter duplicate records and pull the data from database, If you use Union All then it will include all your duplicate records.  Please use Union / Union All as required.
    Once you get all data into the report create a parameter for date range and generate the summaries as required.
    Thanks,
    Sastry

  • Two Queries In One Workbook With Calculations

    Hi Guys,
    Can you have 2 queries in one worksheet, either ran as a query or save as a workbook and calculation done at runtime.  For example, query A and query B to show up in one sheet when you run query A.  You now want a simple calculation done......for example.......A1 row from query A divided by B1 row from query B.  The result to show up on the Key Figure structure in query A.
    Thanks,
    Recca
    Edited by: Recca Recca on Jul 11, 2011 4:45 PM

    Hi,
    I am not sure about this but we had similar kind of requirement and what we did was,
    One query in one tab, another query on second tab and we did key figure calculations on third tab of the workbook eg. Sheet1 A1 - Sheet2-A1 and the result was stored on sheet3.
    We saved the workbook and used to refresh the same whenever we want to see new data.
    Regards,
    Durgesh.

  • How to fire two queries in one bean

    When I am trying to fire two queries using statement and result set
    it is showing error "result set is closed".
    How to fire two queries?

    Just do it. The ResultSet should be the result of the query. So you're working on a wrong reference or something.

Maybe you are looking for