SQL statement that includes LEFT & RIGHT JOIN in the same statement??? Help

Hi,
How an I write an SQL statement with a GROUP BY that will both (a) include the NULL value from the left hand table, but also (b) include ALL columns from the right hand table. It's like I need a LEFT JOIN and a RIGHT JOIN in the query at the same time.
Here's an example of the 2 tables I have and the result I'm after. As you can see I want the NULL's from Expenses Table summed, as well as include all categories from the right hand table (i.e. even if there are no expenses against them)
Table = Expenses
Amount Category
$10 1
$20 2
$30 1
$40 NULL {i.e. not yet categorised}
Table = Categories
ID Title
1 Food
2 Entertainment
3 Travel
4 Personal
REQUIRED RESULT
Category Total
Food 40
Entertainment 20
Travel 0
Personal 0
NULL 40
Thanks

SQL> create table expenses (amount,category)
  2  as
  3  select 10, 1 from dual union all
  4  select 20, 2 from dual union all
  5  select 30, 1 from dual union all
  6  select 40, null from dual
  7  /
Table created.
SQL> create table categories (id,title)
  2  as
  3  select 1, 'Food' from dual union all
  4  select 2, 'Entertainment' from dual union all
  5  select 3, 'Travel' from dual union all
  6  select 4, 'Personal' from dual
  7  /
Table created.
SQL> select c.title category
  2       , nvl(sum(e.amount),0) total
  3    from expenses e
  4         full outer join categories c on (e.category = c.id)
  5   group by c.id
  6       , c.title
  7   order by c.id
  8  /
CATEGORY                                       TOTAL
Food                                              40
Entertainment                                     20
Travel                                             0
Personal                                           0
                                                  40
5 rows selected.Regards,
Rob.

Similar Messages

  • How to create an explain plan with rowsource statistics for a complex query that include multiple table joins ?

    1. How to create an explain plan with rowsource statistics for a complex query that include multiple table joins ?
    When multiple tables are involved , and the actual number of rows returned is more than what the explain plan tells. How can I find out what change is needed  in the stat plan  ?
    2. Does rowsource statistics gives some kind of  understanding of Extended stats ?

    You can get Row Source Statistics only *after* the SQL has been executed.  An Explain Plan midway cannot give you row source statistics.
    To get row source statistics either set STATISTICS_LEVEL='ALL'  in the session that executes theSQL OR use the Hint "gather_plan_statistics"  in the SQL being executed.
    Then use dbms_xplan.display_cursor
    Hemant K Chitale

  • [svn:fx-trunk] 8389: Removed a trace() statement that I left in TextFlowUtil's importFromXML() method.

    Revision: 8389
    Author:   [email protected]
    Date:     2009-07-06 13:19:11 -0700 (Mon, 06 Jul 2009)
    Log Message:
    Removed a trace() statement that I left in TextFlowUtil's importFromXML() method.
    QE Notes: None
    Doc Notes: None
    Bugs: None
    Reviewer: Carol to review
    Modified Paths:
        flex/sdk/trunk/frameworks/projects/spark/src/spark/utils/TextFlowUtil.as

    Revision: 8389
    Author:   [email protected]
    Date:     2009-07-06 13:19:11 -0700 (Mon, 06 Jul 2009)
    Log Message:
    Removed a trace() statement that I left in TextFlowUtil's importFromXML() method.
    QE Notes: None
    Doc Notes: None
    Bugs: None
    Reviewer: Carol to review
    Modified Paths:
        flex/sdk/trunk/frameworks/projects/spark/src/spark/utils/TextFlowUtil.as

  • My Ipod Touch is disabled and when I connect to Itunes to recover it, it states that itiTunes could not connect to the iPod touch "iPod touch" because it is locked with a passcode. You must enter your passcode on the iPod touch before it can recover.

    My Ipod Touch is disabled and when I connect to Itunes to recover it, it states that itiTunes could not connect to the iPod touch “iPod touch” because it is locked with a passcode. You must enter your passcode on the iPod touch before it can recover.

    Place the iOS device in Recovery Mode and then connect to your computer and restore via iTunes. The iPod will be erased.
    iOS: Forgot passcode or device disabled
    If recovery mode does not work try DFU mode.                        
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings        
    For how to restore:                                                             
    iTunes: Restoring iOS software
    To restore from backup see:
    iOS: Back up and restore your iOS device with iCloud or iTunes       
    If you restore from iCloud backup the apps will be automatically downloaded. If you restore from iTunes backup the apps and music have to be in the iTunes library since synced media like apps and music are not included in the backup of the iOS device that iTunes makes.
    You can redownload most iTunes purchases by:           
    Downloading past purchases from the App Store, iBookstore, and iTunes Store        
    If problem what happens or does not happen and when in the instructions? When you successfully get the iPod in recovery mode and connect to computer iTunes should say it found an iPod in recovery mode.

  • Delete statement that uses a sub-select with the statement in the cursor

    Hi all,
    How to write write a delete statement that uses a sub-select with the statement in the cursor?
    CURSOR excluded_dates IS         
           SELECT TO_TIMESTAMP(report_parameter_value, in_date_format_mask)
          INTO my_current_date_time
          FROM report_parameters
         WHERE report_parameters.report_parameter_id    = in_report_parameter_id
           AND report_parameters.report_parameter_group = 'DATE_TIME'
           AND report_parameters.report_parameter_name  = 'EXCLUDED_DATE';
    OPEN excluded_dates;
      LOOP
        FETCH excluded_dates INTO my_excluded_date;
        EXIT WHEN excluded_dates%NOTFOUND;
        DELETE FROM edr_rpt_tmp_inclusion_table
        WHERE TO_CHAR(date_time, 'mm/dd/yyyy') = TO_CHAR(my_excluded_date, 'mm/dd/yyyy');
      END LOOP;
      CLOSE excluded_dates;Thanks

    Hi,
    In such case I think is better to create a view an perform the delete using it. Example (using HR schema):
    Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
    Connected as hr
    SQL> create or replace view v_employees as select * from employees where first_name like 'J%';
    View created
    SQL> select * from v_employees;
    EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DATE   JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
            110 John                 Chen                      JCHEN                     515.124.4269         28/09/1997  FI_ACCOUNT    8200,00                       108           100
            112 Jose Manuel          Urman                     JMURMAN                   515.124.4469         07/03/1998  FI_ACCOUNT    7800,00                       108           100
            125 Julia                Nayer                     JNAYER                    650.124.1214         16/07/1997  ST_CLERK      3200,00                       120            50
            127 James                Landry                    JLANDRY                   650.124.1334         14/01/1999  ST_CLERK      2400,00                       120            50
            131 James                Marlow                    JAMRLOW                   650.124.7234         16/02/1997  ST_CLERK      2500,00                       121            50
            133 Jason                Mallin                    JMALLIN                   650.127.1934         14/06/1996  ST_CLERK      3300,00                       122            50
            139 John                 Seo                       JSEO                      650.121.2019         12/02/1998  ST_CLERK      2700,00                       123            50
            140 Joshua               Patel                     JPATEL                    650.121.1834         06/04/1998  ST_CLERK      2500,00                       123            50
            145 John                 Russell                   JRUSSEL                   011.44.1344.429268   01/10/1996  SA_MAN       14000,00           0,40        100            80
            156 Janette              King                      JKING                     011.44.1345.429268   30/01/1996  SA_REP       10000,00           0,35        146            80
            176 Jonathon             Taylor                    JTAYLOR                   011.44.1644.429265   24/03/1998  SA_REP        8600,00           0,20        149            80
            177 Jack                 Livingston                JLIVINGS                  011.44.1644.429264   23/04/1998  SA_REP        8400,00           0,20        149            80
            181 Jean                 Fleaur                    JFLEAUR                   650.507.9877         23/02/1998  SH_CLERK      3100,00                       120            50
            186 Julia                Dellinger                 JDELLING                  650.509.3876         24/06/1998  SH_CLERK      3400,00                       121            50
            189 Jennifer             Dilly                     JDILLY                    650.505.2876         13/08/1997  SH_CLERK      3600,00                       122            50
            200 Jennifer             Whalen                    JWHALEN                   515.123.4444         17/09/1987  AD_ASST       4400,00                       101            10
    16 rows selected
    SQL> delete from v_employees where hire_date >= to_date('01/06/1998', 'dd/mm/yyyy');
    2 rows deleted
    SQL> regards,

  • My iPhone 5c won't connect to home internet. I have an Actiontec router and my phone shows that I'm connected but no IP address comes up on my phone and Safari and other Apps state that I'm not connected to the internet.

    I recently purchased an iPhone 5c and for some unknown reason I can't connect to my wireless router's network at home. I've tried establishing a seperate IP address, manually adding the iPhone device, and even spent an hour with Centurylink's IT support and they couldn't figure it out. They said it was an iPhone error. I had an iPhone 3 about 1 1/2 years ago that had the same problem. I ended up getting rid of the phone because I couldn't solve this problem before. I thought a newer phone would not have the same problem.
    My phone shows that I'm connected to my home internet, but when I open up network information for my home internet there is no IP Address listed. Safari and my other apps also state that I'm not connected to the internet. I'm ready to sell my phone back and go to an Android since I didn't have trouble connecting with it before. We also have a Macbook and iPad that both connect to the internet just fine. Please....oh please help.

    I was having the same problem; I tried all the recommended fixes in the "more like this" and nothing worked, but finally found the answer.  Try this: http://themiddleofthestreet.blogspot.com/2013/11/if-your-iphone-5s-wont-connect- to-wifi.html

  • When I import a CD album that includes songs with co-artists, the album is saved once with all of the songs with the main artist only, and then one album for each song with a different co-artist.  How can I avoid that or fix that result after the fact.

    When I import a CD ablum that includes songs with co-artists, the album is saved once under the album name with all the songs from the main artist; then each song with a different co-artist is saved separately under the album title with just that song.  Who do I prevent that or fix it after the fact, i.e., copy the single songs to the main album?

    Why aren't songs with the same album art grouped together? - http://support.apple.com/kb/ts1468
    Compilation checkbox is under options tab when editing multiple tracks at once.
    https://discussions.apple.com/thread/4559720 - discussion of using compilation vs. album artist to group tracks
    Jan 2014 discussion: https://discussions.apple.com/thread/5775641

  • My computer broke, so I had to change the hardisk(?). Now iTunes dosen't work anymore. It says that the program is either lockes, on a locked unit or I don't have the right to open the library? Help please? :)

    My computer broke, so I had to change the hardrive-hardisk(?). Now iTunes dosen't work anymore. It says that the program is either locked, is on a locked unit or I don't have the right to open the library? Help please?

    Sometimes a restart can fix this issue, but sometimes you have to fix the iTunes Library.itl file.
    Try quitting iTunes, renaming the itunes file "iTunes Library.itl" to "iTunes Library Old.itl"
    then open itunes again
    This should generate a new library file.
    From this point you can try two different avenues.
    1. Re-add everything to your library by going to the iTunes file menu, and selecting "add to library"
    2. You can try deleting the iTunes Library.itl you created, changing the name of the old file back to iTunes Library.itl
    I hope this helps. I did something like this a while back and it worked for me.

  • Updated last night, my keynote presentation is gone. Now trying to recreate. Keynote states that animations are not available on master slides. Help?

    Updated last night, my keynote presentation is gone. Now trying to recreate. Keynote states that animations are not available on master slides. Help?

    I'm no help, but I lost a huge file too with the 10.9.2 update. It isn't ANYWHERE. Totally embarrassing in front of a large meeting.

  • When i try to download additional garageband sounds, it states that I am not connected to the internet but I am. How can I fix this?

    When i try to download additional garageband sounds, it states that I am not connected to the internet but I am. How can I fix this?
    I have a new Macbook Pro, OS X 10.9.3 and the latest Garageband that came installed.

    However, I can't see any of the new sounds in garageband??
    Have you restarted your computer after installing the additional content?
    How many drummers do you see, when you select a patch for a Drummer Track?
    If the inapt purchase has been installed, you should now be seeing 18 drum patches and not only SoCal:
    Also, if you open the "Lesson Store" view, there should be now several free basic guitar and piano lessons available for download.

  • Left and right click at the same time?

    Is there a way to get the magic mouse to do left and right click at the same time? Useful for well.. gaming.

    Check out the free Magicprefs software. You can then assign different functions to different clicks. swipes, taps, etc. There is an option for a two finger click and one of those options is "hold both right & left". It's free so you have noting to lose.
    HTH

  • Two diffferent type of joins on the same table

    I have a query that, it seems, requires me to two two different types of joins to the same table.
    from table t1
    LEFT OUTER JOIN table2 t2
    ON t1.col1= t2.col1
    and t1.col2 = t2.col2
    and t1.col5 = t2.col5
    LEFT OUTER JOIN table2 t3
    ON t1.col1= t3.col1
    and t1.col2 = t3.col2
    and t1.col4 = t3.col4
    would I run into a problem if I just did:
    LEFT OUTER JOIN table2 t2
    ON t1.col1= t2.col1
    and t1.col2 = t2.col2
    and t1.col3 = t2.col3
    and t1.col4 = t2.col4
    and t1.col5 = t2.col5
    And if I go with option 1 and I also create two different types of indexes to accommodate each join, will 10g know which ones to use?
    Message was edited by:
    user623359
    Message was edited by:
    user623359

    No version number and no statement of what it is you are trying to do ... but one solutions is to use the traditional Oracle syntax and the first part, then wraps it in parentheses turning it into an inline view, and then join that to the next part.

  • Illegal cross join within the same dimension caused by incorrect subject ar

    hi!
    Imagine the following BMM:
    There is one Logical Dimension Table "Service Account" with the following LTS:
    - LTS "D_SERVICE"
    - LTS "D_SERVICE_CLASS"
    - LTS "D_SERVICE_STATUS"
    This Logical Tabe "Service Account" joins (One-to-many) with several Fact Tables (other Logical Tables).
    In the Physical Layer the joins for the "Service Account" LTS are:
    - One-to-many between D_SERVICE_CLASS and D_SERVICE
    - One-to-many between D_SERVICE_STATUS and D_SERVICE
    The problem is that when I build a report in Answers using only the following columns of the Logical Table "Service Account":
    - Service Class Desc (which exists in the Logical Table "Service Account" and in the Physical Table D_SERVICE_CLASS)
    - Service Status Desc (which exists in the Logical Table "Service Account" and in the Physical Tabl D_SERVICE_STATUS)
    - MSISDN (which existis in the Logical Table "Service Account" and in the Phsyical Table D_SERVICE)
    the following error appears:
    Estado: HY000. Código: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 14065] Illegal cross join within the same dimension caused by incorrect subject area setup: [ (select * from prd.D_SERVICE where SOURCE_SYS in ('ARBOR','PPB') and DW_SERV_ST_ID in (100000003,100000009)) as T1836, D_SERVICE_CLASS T1916] with [ D_SERVICE_STATUS T1948] (HY000)
    SQL emitido: SELECT "SERVICE ACCOUNT"."TLC MSISDN9" saw_0, "SERVICE ACCOUNT"."IWS Service Class Desc" saw_1, "SERVICE ACCOUNT"."TLC Service Status Desc" saw_2 FROM "VFPT - Upgrade Siebel" WHERE "SERVICE ACCOUNT"."TLC MSISDN9" = '917330340' ORDER BY saw_0, saw_1, saw_2
    Help, please!
    Thanks.

    Physically, only D_SERVICE is joined to the facts. Then D_SERVICE_CLASS is joined to D_SERVICE (one-to-many) and D_SERVICE_STATUS is joined also to D_SERVICE (one-to-many).
    In the BMM, there is only one Logical Dimension Table for those 3 physical tables. This Logical Dimension Table is called "Service Account" and has 3 LTS: D_SERVICE, D_SERVICE_CLASS, D_SERVICE_STATUS. The Logical Dimension Table has several logical columns that are associated to those 3 LTS: SERVICE_KEY, SERVICE_CLASS_KEY, SERVICE_STATUS_KEY, SERVICE_MSISDN, SERVICE_CLASS_DESC, SERVICE_STATUS_DESC.
    The Logical Dimension Table "Service Account" is then joined to a fact table (one-to-many).
    I didn't understand the suggestion about the 3 LTS... Aren't we doing that already? Can you explain it better, please?
    thanks.

  • [nQSError: 14065] Illegal cross join within the same dimension

    Hey guys,
    I'm stumped. I have two dimension tables that are joined 1:N (there is NOT an M:N relationship between them) and I have them joined in the Physical Layer and the Business Model and Mapping Layer. The two tables are F4101 (the "1") and F4102 (the "N") in the 1:N relationship. F4102 then joins to a fact table, and F4101 joins to NOTHING else. So I don't believe I have a circular condition or a need for a bridge table. Both tables are published to the Presentation Layer for reporting.
    The error occurs in Answers when I want to do something as trivial as display the three primary key columns together from F4101: F4101.col1, F4101.col2, F4101.col3 (all three make up the PK). When I do that, the following error occurs:
    "nQSError: 14065] Illegal cross join within the same dimension caused by incorrect subject area setup: [ F4101 T28761] with [ F4102 T1805] "
    What I can't figure out is WHY the F4102 table is listed in this error. I didn't try to report on it at all. See the logical SQL below from my query:
    "SQL Issued: SELECT "Item Master (F4101)".IMITM saw_0, "Item Master (F4101)".IMLITM saw_1, "Item Master (F4101)".IMAITM saw_2 FROM "Sales Analysis" ORDER BY saw_0, saw_1, saw_2"
    As soon as I take out one of the three PK columns and add in another non-PK column from F4101, it works just fine. And reporting on each of the three PK columns individually works as well in Answers.
    Any ideas? I would greatly appreciate it.
    Thanks.

    Try this;
    1. In the logical layer, create one folder called F4101_F4102.
    2. Map both F4101 and F4102 as logical table sources in that folder.
    3. Join from the folder F4101_F4102 to the fact using a Logical (new complex join) join.
    Chris.

  • Illegal cross join within the same dimension

    Hi,
    When certain fields are selected within the presentation table an "illegal cross join" error is returned by the BI Server. However if a FACT is added from one of the other presentation tables the "illegal cross join" error goes away. we need to query without fact column.
    We are getting following error
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 14065] Illegal cross join within the same dimension caused by incorrect subject area setup: [ CALL_CENTER.COUNSELOR_MANAGER T782130] with [ CALL_CENTER.COUNSELOR_HR T781594 On CALL_CENTER.COUNSELOR_HR.MASTER_STAFF_COUNSELOR_ID = CALL_CENTER.MASTER_STAFF_COUNSELOR.MASTER_STAFF_COUNSELOR_ID, CALL_CENTER.MASTER_STAFF_COUNSELOR T781739] (HY000)
    Can anybody help me solving this issue.
    Thanks,
    KS.

    Please give us an example of what you need.
    OBIEE perform a query in the dimension or through the fact table.
    You can't join two dimensions in the repository without going through a fact table.
    If you need to query without fact column, it's because you have design two dimensions where
    normally you can do one.
    You have then two solutions :
    * change the design of your logical model to make only one dimension.
    * use the OBIEE logical SQL in answer.
    http://gerardnico.com/wiki/dat/obiee/bi_server/design/obiee_logical_sql
    Success
    Nico

Maybe you are looking for

  • Moving from Adobe Bridge CS5 to Aperture 3

    So I have been using Bridge since CS right through to CS5 for all my organising needs but I want something that is a little more organised in terms of photography requirements as my collection is getting larger and in need of higher organisation. I l

  • CS6 & Windows 8.1

    I have PS CS6 and just updated to Win 8.1.  Now my CS6 is not working correctly.  Bridge often comes up "Not Responding" and PS blinks with a b & w checker board while working within a photo.  I believe this to be a settings issue, however been unabl

  • Aluminium keyboard update failure

    When I switch the Mac on, it asks me to update the aluminum keyboard. Once I click on the keyboard to update, it says the keyboard is already updated.? When i press Quit, it says that the keyboard is not up to date..do you want to quit? It does not r

  • From hdd to ssd: what about os and all data?

    Hello everybody. I know there are a lot of discussion about it and i read lots of them, But maybe because english is not my first language, i have some problems in understanding the right things to do. Also, i am pretty new with mac world as i just h

  • SQL Query Urgent..pls help

    Dear Experts, Table. C_id Purity 1     100 2     100 10     100 11     100 12     100 Ranking Regions by Size You have to list all regions in the table, and you have to list them according to their size. you need to list all regions of two or more co