Problem selecting records from multiple tables

Good day
I am writing  a report showing actual vs budget amount per period.  The actual amounts are in one table and the budget amount are in another table.  In this example it is for vehicle sales.  The problem is that in the budget table, there are no records for a particular make if it was not budgeted to make a sale.  If I try to get the records using the select expert it matches the key in actual table with key in budget table and retrieve.  What happens then is that the records in the actual table get excluded if there was not a record for it in the budget table.  How do I then do this selection, i.e. if there is a record in the budget table, then retrieve, otherwise make the budget amount 0?
Thanks
Wilhelm

This is always difficult the best way is to use a command and union the two sets of data.
First part uses a left outer join from Actual to Budget and brings back all actual data with and without a budget.
Second parts brings back data from budegt where nothing exists in actual
select (field list) from
Actual a
left outer join Budget b on a.field = b.field
where (any other filter condition)
union all
select (field list) from
budget b
left outer join actual a on b.field = a.field and a.field is null
where (any other filter condition)
You can get the first query from your current report and the modify to build the second unioned query.
Ian

Similar Messages

  • Records from multiple tables

    Hi,
    I have a method that has to access records from multiple tables and store them all in a file. It's something like this:
    switch (tableId){
    case 'A' : query="SELECT * FROM TABLE_A
    WHERE ID = '" + tempId + "'";
    ResultSet rs = stmt.executeQuery (query);
    rs.close();
    break;
    case 'B' : query="SELECT * FROM TABLE_B
    WHERE ID = '" + tempId + "'";
    rs = stmt.executeQuery (query);
    rs.close();
    break;
    case 'C' : query="SELECT * FROM TABLE_C
    WHERE ID = '" + tempId + "'";
    rs = stmt.executeQuery (query);
    rs.close();
    break;
    My problem is that I get an "Invalid cursor" error the second time I enter the loop. I've been reading up on cursors but I still don't know what to do. Can I use ResultSet to return multiple rows from multiple tables? If not, what can I use? Please help!
    Thanks in advance.

    remove the "rs.close()" from the switch statement. use "rs.close()" after your loop ends.
    prem

  • Delete records from multiple table

    Hi,
    I need to delete records from multiple tables using a single delete statement. Is it possible ? If so please let me know the procedure.
    Kindly Help.
    Thanks,
    Alexander.

    Hi Tim,
    Syntax of DELETE statement does not allow for multiple tables to be specified in this way. Infact, none of the DMLs allow you to specify table names like this.
    Technically, there are other ways of deleting from multiple tables with one statement.
    1. "Use a trigger":
    What was probably meant by this is that you have a driving-table on which you create a on-delete trigger. In this trigger, you write the logic for deleting from other tables that you want to delete from.
    This does mean a one-time effort of writing the trigger. But the actual DML operation of deleting from all the tables would be simply triggered by a delete on driving-table.
    2. Dynamic SQL:
    Write a PL/SQL code to open a cursor with table-names from which you want the data to be deleted from. In the cursor-for loop, write a dynamic SQL using the table-name to delete from that table.
    3. Using Foreign-Key constraint with Cascade-Delete:
    This I feel is a more 'cleaner' way of doing this.
    Having to delete data from multiple tables means that there is some kind of parent-child relationship between your tables. These relationships can be implemented in database using foreign-key constraints. While creating foreign-key constraint give the 'on delete cascade' clause to ensure that whenever data is deleted from parent-table, its dependent data is deleted from child-table.
    Using foreign-key constraint you can create a heirarchy of parent-child relationships and still your DELETE would be simple as you would only have to delete from parent-table.
    IMPORTANT: Implementing foreign-key constraints would also impact other DML operations that you should keep in mind.

  • ** Is it possible to give select command from multiple tables in JDBC

    Hi Friends,
    Is it possible to give the select command to select data from multiple tables directly in the 'Query SQL statement' in JDBC sender communication channel ? (Instead of Stored Procedure)
    Thanking you.
    Kind Regards,
    Jeg P.

    Hi,
    here is a sample:
    Table #1
    Header
    Name EmpId Status
    Jai 5601 0
    Karthik 5579 0
    Table #2
    Name Contactnumber
    Jai 9894268913
    Jai 04312432431
    Karthik 98984110335
    Karthik 04222643993
    select Header.Name, Header.EmpId, Item.Contactnumber from Header,Item where Header.Name = (select min(Header.Name) from Header where Header.Status = 0) and Header.Name = Item.Name
    Regards Mario

  • Need assistance searching records from multiple tables - Please Help!

    Hi, I've been trying to solve this problem for several weeks now, and I have exhausted all of my knowledge and experience.  I need help, and I hope someone here can give me some direction.
    I am using VB 2008, and the CR that comes bundled with VS 2008 Pro.  My database is a SQL Server 2005 CE v3.5 (a *.sdf file).  I am connecting to the database through a dataset, and I am displaying the report in a CrystalReportViewer.
    My dataset consistes of two tables:
    1) tblCustomers which has a primary key "CustID", and contains only customer contact and personal information. 
    2) tblDateVisited which has a primary key of "VisitID", but it also has a column titled "CustID". Basically, every time a customer visits the business, details of that visit are recorded in tblDateVisited, and that record is associated with the customer by their CustID.
    Here's what I'm trying to accomplish:  I want to be able to display only Customer records when the customer has visited and that visit matches certain criteria.  Right now, I am trying to match visits from the "tblVisitDate.PlayerType" column.  If the customer has ever had a visit where they matched a particular player type, I want to see those customer records.
    I don't know what I'm doing wrong, though.  I can search a dataset if I am only querying one table and pulling records from that table.  However, whenever I try to add a second table and perform queries on that table to get records from the first table, I can't return any records. 
    If it helps, I am trying to use one CrystalReportViewer to display multiple reports (user choice) and here's how I'm loading the report into the viewer:
    Me.tblCustomersTableAdapter.Fill(Me.dsPlayerTypeReports.tblCustomers)
    Me.tblDateVisitedTableAdapter.Fill(Me.dsPlayerTypeReports.tblDateVisited)
    Me.ReportFile.SetDataSource(dsPlayerTypeReports.Tables(1))
    I am suspicious that my problem is in the Tables(1) method.  It confuses me that I can only assign one table as a datasource when I obviously need access to two tables to make this selection work. 
    Whatever the case, I'm at the end of my rope with this one.  I'm not prone to giving up, but I'm at a dead end currently. 
    Any attempt to assist me with this will be greatly appreciated, successful or not.
    Thanks in advance!
    -Will

    No, I am connected via ADO.NET.  I don't think SQL CE can connect through ODBC (or if it can, I haven't been able to figure out how, yet).  So this isn't a stored procedure.
    When I examine the link, I can only choose an Inner Join or a Left Outer Join.  Right Outer and Full Outer are not available.  Could this be a problem with the dataset I'm using?
    Could you explain what you mean by display all of the records and then choose the selection criteria?

  • Sender JDBC Adapter : Fetch records from multiple tables

    Hi Friends,
    I am using sender JDBC adapter to select few records from DB2 database table.
    This scenario is working fine.
    Now my requirement is to fetch records from 3 tables. These table are independent to each other. There is no primary key or foreign key.
    Please let me know how to write the sql in sender JDBC adapter to fetch records from these 3 tables.
    Thanks,
    Sandeep Maurya

    hi sandeep...
    if the tables are completely independent and do not share any primary / foreign key relation ship...
    why dont u think towards creating a seperate interface for each of them..
    or if u still want to select from multiple table at once..the best way would be to write a stored procedure on the sender side which do all the fetching n processing and pass the final resultset to PI
    or u can think towards fetching the data from 1 table and then in UDF do lookup from other tables..which again is tricky and performace intensive

  • Extracting Records from multiple tables

    hi
    I have to extract almost 1 lack  records from 3 tables.
    Please help me in ABAP CODE.
    PLease it is very urgent.

    Which tables are they ?  Refer to link below for more hints abt performance.
    Re: select query - optimization

  • Selecting records from multiple dates but specific time

    Hi:
    I've to select records of multiple dates but the time should be 00:00:00 to 06:00:00 (i.e. 12 AM - 6 AM)
    For date part this can be done:
    WHERE
    START_TIME BETWEEN TO_DATE('04-01-2012', 'MM-DD-YYYY') AND TO_DATE('04-05-2012', 'MM-DD-YYYY')
    But how can I fix the time mentioed above.
    Please let me know.
    Thanks/Tanvir

    WHERE
    START_TIME BETWEEN TO_DATE('04-01-2012', 'MM-DD-YYYY') AND TO_DATE('04-05-2012', 'MM-DD-YYYY')
    and start_time - trunc(start_time) between 0 and 6/24start_time - trunc(start_time) will give you the fraction of a day. So 6/24 means 06:00.
    (When you use to_char(trunc(sysdate,'hh'),'hh24') between 0 and 6 you will also retrieve records with start_time 06:01 for example. So this would not meet your requirements as far as I understood.)
    Edited by: hm on 10.04.2012 01:26

  • Using ResultSet to read records from multiple tables

    Im using a ResultSet object to read records from tables.
    I have a database with 5 tables. Initially, I got it to work fine with one of those tables. What Im exactly trying to do is check if there is a record in a table which matches a given string, and if it does, display some text on some JTextFields. I got that to work fine with one table.
    Problem is, how do I get it to work with all 5 tables at the same time?
    i.e. instead of searching through one table, I want it to look through all 5 tables and look for the matches.
    Any help greatly appreciated.
    Thanks

    After the first interaction with the first table, I
    used a stmt.getMoreResults() followed by instructions
    to get data from the second table, and so on.
    Worked fine.Sounds remarkably inefficient. (Potential for lots of network traffic.)
    is this another way of doing it?Sounds more like a document search type of thing than a database query. What about Lucene? Could it help here?
    %

  • Selecting Records from one table where records don't exist in a linked table

    Post Author: RMC
    CA Forum: Crystal Reports
    Tables:
    Stock (Main Table)
    Transactions (Left Outer Join)
    Criteria:
    Select all items with no transactions between a selected date range.
    The items will exist in Main table but I want to select all items that haven't been in a transaction between a date range so I can identify no movement with stock items just sitting in the database doing nothing.
    Any suggestions??

    Post Author: yangster
    CA Forum: Crystal Reports
    Sorry for the delay.  Dam meetings all day yesterday.You are essentially looking for something that doesn't exist in the db so you need to look at it from a different approach.You definitely don't want to use a full outer join.  From what i can tell from your description above you are looking for stocks that are inactive/idle.So you need not look at data that doesn't exist but the last activity date.So you'd want something along the lines ofselect s.stock_name, s.stock_blah, max(t.transaction_date) Transaction_datefrom stocks sjoin transactions t on s.itemid = t.itemidwhere whatever restrictions you need to addgroup by s.stock_name, s.stock_blahthis will produce one record for each stock you have in the stock table showing you only the last date of activityput that in a command in crystalin the selection expert in crystal reports put in a simple criteriatransaction_date <= some datethis will only pull in records based on your date selection so if you say give me all records from dec 31, 2006  you will know all the records being pulled in are stocks that have been idle since dec 31, 2006 or earlierlet me know if this doesn't work

  • Problem fetching data from multiple tables

    Hi,
    I am facing a problem in fetching data from 3 different tables. i am able to get the data from 2 tables but when i try accessing data from other table, i am getting redundant data. please help, my query is as under.
    select a.*,b.*, c.BidAmount from carregister a , watchlist b, Bidmaster c where a.CarID= b.CarID and b.UserID = 23 and c.BidAmount=(select max(BidAmount) from BidMaster where carid=a.carid group by c.BidAmount);
    Regards,
    Ujjwal B Soni
    <Software Developer>
    Baroda Gujarat India

    Hi,
    I got the solution. The solved query is as under :
    select a.*,b.*,(select max(BidAmount) from BidMaster where carid=b.carid) as MAX_AMT from carregister a , watchlist b where a.CarID= b.CarID and b.UserID=23;
    Thank you all for replying me.
    Warm Regards,
    Ujjwal B Soni
    <Software Developer>
    <Baroda Gujarat India>

  • Can i select Records from two tables into an itab.

    Hi,
        Suppose that i have two tables spfli and sflight.
        Now i want the records from both the tables into a single internal table.
        If so please let me know how this can be done.
        itab should consist of all the columns in sflight and spfli.
    Regards,
    Sai

    Sai,
    Yes u can do it using JOINs.
    Use this link for ref
    http://www.sap-img.com/abap/inner-joins.htm
    INNER JOIN results are an intersection of the tables being joined where in only if both the tables havethe data the result is pused onto the result set.
    WHERE as in LEFT OUTR JOIN you can push the data of the LEF T table on the resultset even when the join condition is not met.
    The use is that you wantto have all the data that is there in left table and also the right table if the join condition is a success then right table fileds will have data else they are initial.
    OUTJOIN's are used in MAINTENANNCE VIEWS, HELP VIEWS .
    INNER JOINS are used DATABSE VIEWS.
    Inner Join:-
    SELECT pcarrid pconnid ffldate bbookid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM ( ( spfli AS p
    INNER JOIN sflight AS f ON pcarrid = fcarrid AND
    pconnid = fconnid )
    INNER JOIN sbook AS b ON bcarrid = fcarrid AND
    bconnid = fconnid AND
    bfldate = ffldate )
    WHERE p~cityfrom = 'FRANKFURT' AND
    p~cityto = 'NEW YORK' AND
    fseatsmax > fseatsocc.
    Left Outer Join
    SELECT scarrid scarrname p~connid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM scarr AS s
    LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid AND
    p~cityfrom = 'FRANKFURT'.
    Reward if helpful,
    Karthik

  • Select record from database table

    how to select a record from data-base table for current month in module-pool, i.e if current month is august then records selected should between 1/08/10 to 31/08/10.
    And, i also want to select records for last month i.e if current month is august then records selected should be between 1/07/10 to 31/07/10.
    And one thing more, it should work for every scenario. Like for months are changing then it should work according to that.
    Example: If month changes to October then current month ll be October and last month ll become September.
    Plz help me out.
    Moderator message: you chose a better forum this time, but the question is still too basic to be asked here, please (re)search yourself first, these forums are not catering to beginners.
    locked by: Thomas Zloch on Aug 8, 2010 9:41 PM

    Hi Nishant,
    You can use the option <b>ORDER BY </b> in your SELECT clause.
    This is the SAP Documentation for <b>ORDER BY </b> option.
    <i>Variant 1</i>
    <b>...ORDER BY PRIMARY KEY</b>
    <b>Effect</b>
    Sorts the selected lines in ascending order by the primary key of the database table. This variant is only permitted for SELECT * ....
    <i>Notes</i>
    Since views do not have a primary key, specifying ORDER BY PRIMARY KEY only makes sense with database tables. If, however, you do specify ORDER BY PRIMARY KEY with a view, all fields of the view are sorted in ascending order.
    <i>Variant 2</i>
    <b>ORDER BY f1 ... fn</b>
    <i>Effect</i>
    Sorts the selected records in ascending order by the specified column references f1 ... fn. If a list is also specified in the SELECT clause, the column references f1, ..., fn must appear in this list.
    By supplementing the statement with DESCENDING, you can sort in descending order using any of the fields f1, ..., fn.
    The default sort sequence is ascending order, but you can make this explicit by adding the addition ASCENDING.
    Consider this code this will select the latest 10 records form DB table(Note can use <b>Variant 2</b> for DB tables also.
    REPORT zarun_1.
    DATA : it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE.
    START-OF-SELECTION.
      SELECT * FROM mara
                INTO TABLE it_mara
                <b>UP TO 10 ROWS                "No of rows you need(give 1 here)
               ORDER BY matnr DESCENDING.    "Specify the Key Fields here.</b>
      LOOP AT it_mara.
        WRITE : / it_mara-matnr,
                  it_mara-mtart.
      ENDLOOP.
    Regards,
    Arun Sambargi.
    Message was edited by: Arun Sambargi

  • Records from multiple table in Forms using Structure

    Hi all,
    I thought of bringing records from more than one table to smart forms.I have done the same by using two internal tables but not using with globally declared structure.Is it like this:
    1.Create the global structure
    2.In driver program create a internal table
    3.Pull the data using select statement
    4.Pass the internal table to smartforms-> Here I dont understand how.Usually In smartforms we declare internal tables of table type like 'itable type zgkmaster'
    How to pass the internal table we created using structure to smartforms?
    Thanks

    Hi Gopi,
    the procedure u r telling is correct.
    create the structure(ex:zstruc) in se11 as the structure of the internal table.
    *popluate the data into internal table.
    *in smartform, u declare the internal table as that structure i.e itab type zstruc.
    i.e in smartforms when u declare internal table, u r telling that ur internal table having the structure similar to the structure ZSTRUC.
    In smartforms u can declare the table type using either ...type <db table> or ... type <structure>.
    san

  • Search a record from Multiple Tables

    Is there a pl/sql command or way that searches for a specific record (text, number) in multiple tables? Given that you don't know the column you are searching from.

    There is no built in method, but you can roll your own.
    I posted one way Re: How to find out a tablename
    HTH
    John

Maybe you are looking for