Having trouble understanding how to nest this query

I have a table
Events:
folder (Primary Key), other attributes
Reservations:
resid and folder create the primary key
I want to get attributes from the events table as well as the COUNT in the reservations table where folder=folder
So that the query returns the events attribute and the number of reservations associated with that event.
Here's what I tried ...
SELECT name, folder, start_date, end_date,
LEFT JOIN
(SELECT COUNT(*),reservations.folder FROM reservations
LEFT JOIN events WHERE events.folder=reservations.folder
GROUP BY reservations.folder)
ON reservations.folder=events.folder
FROM events WHERE events.status='Live'
ORDER BY events.folderEdited by: 928591 on Apr 21, 2012 12:31 PM

1. Inline View to get the reservation count per folder
2. Outer Join with events to get folder and there reservation count. Set reservation count = 0 if any event folder does not have any reservation count
3. Used oracle syntax instead of ANSI SQL to get better readability.
SELECT ev.name,
       ev.folder,
       ev.start_date,
       ev.end_date,
       NVL(res.cnt,0) reservation_count
FROM events ev,
     ( SELECT /*+ NO_MERGE */
              COUNT(*) cnt,
              folder
       FROM  reservations
       GROUP BY folder
      ) res
WHERE events.status='Live'
AND   events.folder = res.folder(+)
ORDER BY events.folder
{code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • I'm having trouble understanding how the Address Book "works".

    I like to have one card for each organization not each  Person, if that is possible.
    For example,   Thunderbirds Organization:   Jeff Tracy,  Lady Penelope, Mr hackenbaker.  On the same card.
    Is it possible to have all 3 names on the same card? I am successfully able to enter all this into the Address Book. However, when using the addresses to prepare e-mails I often find that the names get mixed up. ( Apple Mail usually puts the name of the 1st person in the group in front of the e-mail address that I select, rather than the actual name of the person that goes with that e-mail address.)
    Having multiple people from the same organization on the same card seems to make sense for me. Some have advised me that I need to create a separate card for each and every person! Is that true?
    Suggestions appreciated.
    HackenbackerHackenbackerssss

    Ziatron wrote:
    I like to have one card for each organization not each  Person, if that is possible.
    For example,   Thunderbirds Organization:   Jeff Tracy,  Lady Penelope, Mr hackenbaker.  On the same card.
    Is it possible to have all 3 names on the same card? I am successfully able to enter all this into the Address Book. However, when using the addresses to prepare e-mails I often find that the names get mixed up. ( Apple Mail usually puts the name of the 1st person in the group in front of the e-mail address that I select, rather than the actual name of the person that goes with that e-mail address.)
    Having multiple people from the same organization on the same card seems to make sense for me. Some have advised me that I need to create a separate card for each and every person! Is that true?
    Suggestions appreciated.
    HackenbackerHackenbackerssss
    In order for mail to work correctly you will need a seperate card for each individual.

  • Trouble understanding how to obtain this package

    https://aur.archlinux.org/packages/toilet/
    https://aur.archlinux.org/packages/automake-1.9/
    I am trying to get both of these packages, for some reason pacman just won't do it.
    pacman -S automake, will return with automake 1.12.5 and not 1.9.
    pacman -S toilet, will return a message saying the package can't be found.
    I tried manually creating the packages but when i run makepkg there is no .xz file, I just get directories with source/binaries.
    I would really prefer to just have pacman pull the packages as it normally does, is it possible with those packages? If so, how do I accomplish that?
    Thanks,
    Last edited by theoddsarebad (2012-12-06 15:16:42)

    2ManyDogs wrote:Welcome to the forum. These are AUR packages. Read the wiki: https://wiki.archlinux.org/index.php/AUR.
    Oh, I see... "A good number of new packages that enter the official repositories start in the AUR".
    So AUR isn't where pacman grabs packages, I see, I assumed it was. Thanks.
    So my real issue is that I'm not getting .xz files from makepkg, actually my real issue is much more complex and I should just create a new thread for it since this thread was focused on trying to get pacman to do something it obviously can't. : )

  • I'm having trouble understanding how to upload a web gallery?

    I've been trying to upload a web gallery for weeks, I just can't seem to get it right.
    Does anyone have any tips or advice? I use godaddy as my host.
    Thanks!!

    What have you tried so far and where are you struggling?
    Have you followed the various online tutorials?
    http://tv.adobe.com/watch/learn-lightroom-4/posting-a-web-photo-gallery-to-your-website/
    http://help.adobe.com/en_US/lightroom/using/WSA20E5E2B-1F61-4c59-A3F4-13C1AEF9EF56.html#WS 41FEE1CD-1062-4214-88A0-198706E0BC2B

  • HT4623 I upgraded my iphone4S yesterday and Siri response with "having trouble understanding".  Is there an upgrade for Siri?

    I upgraded my iPhone4S to iOs6 and now Siri does not understand me.  Siri responses "having trouble understanding you, try again".  Is there an updgrade for Siri?  I have been searching all afternoon for a glimmer of a solution.  Any suggestions?

    Sure...
    See this Apple article for a Hard reset of a Factory reset.
    http://support.apple.com/kb/HT3728

  • How to rewrite this query without sub query please help me

    Hello All Good Evening,
    Could you please help me with this query, how can i write this query without sub query, or how can write this query another ways
    please help me
    select planno, status1, count(*) Counts from
    select a.ValetNO PlanNo  ,
    case 
         when JoinCode in ('00', '01', '02') then 'Actcess'
         when JoinCode in ('20', '21', '22', '23','38', '39') then
         'Secured' else 'Other' end Status1 ---, COUNT (*)
       from  dbo.ppt a(NOLOCK)  left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO  = b.ValetNO
    --group by a.ValetNO
      a group by planno, status1
    order by 2
    Thank you in Advance
    Milan

    Whats your objective here? Sorry, am not able to understand the reason for this change. 
    Try the below:(Not tested)
    ;With cte
    As
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 ---, COUNT (*)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    select planno, status1, count(*) Counts from cte
    a group by planno, status1
    order by 2
    Even below:
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 , COUNT (1)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    Group by a.ValetNO ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end

  • How to write this query ?

    how to write this query ?
    list the emp name who is working for the highest avg sal department.
    I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?
    the row_number version is like this
    select emp.* from emp ,
    select deptno, row_number() over (order by avg(sal) desc) r from emp
    group by deptno
    )e
    where e.r = 1
    and emp.deptno = e.deptno

    Hi,
    806540 wrote:
    how to write this query ?
    list the emp name who is working for the highest avg sal department.
    I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?ROW_NUMBER is just plain SQL, and has been since Oracle 8.1.
    ROW_NUMBER (or its close relative, RANK) is the simplest and most efficient way to solve this problem. Why not do this the right way?
    the row_number version is like this
    select emp.* from emp ,
    select deptno, row_number() over (order by avg(sal) desc) r from emp
    group by deptno
    )e
    where e.r = 1
    and emp.deptno = e.deptno
    If there happens to be a tie (that is, two or more departments have the same average sal, and it is the highest), then the query above will only arbitrarily treat one of them (no telling which one) as the highest. Change ROW_NUMBER to RANK to get all departments with a claim to having the highest average sal.
    You could use the ROWNUM pseudo-column instead of ROW_NUMBER, if all you want to do is avoid ROW_NUMBER.
    Without using ROW_NUMBER or RANK, there are lots of ways involving other analytic functions, such as AVG and MAX.
    If you really, really don't want to use analytic functions at all, you can do this:
    SELECT     *
    FROM     scott.emp
    WHERE     deptno     IN  (
                      SELECT       deptno
                      FROM       scott.emp
                      GROUP BY  deptno
                      HAVING       AVG (sal) =  (
                                                       SELECT    MAX (AVG (sal))
                                               FROM          scott.emp
                                               GROUP BY  deptno
    ;

  • SQLite3 and BlobsI am having trouble learning how insert blobs into SQLite

    I am having trouble learning how insert blobs into SQLite for the iphone. Is there a way to insert a blob into sqlite3 from the command line? I know it was possible with sql using
    INSERT INTO temponly(name , pic) values( 'velan',
    load_file('e:/mysql/images/Click.gif')) ;
    but I don't think load_file is supported on sqlite.
    Also, I need to insert the blobs from my iphone app. I saw a C program called eatblob.c which goes through the following just to INSERT a blob into sqlite
    1. read the jpeg image file into memory , doing appropriate malloc based on size
    2. prepare an sqlite statement with ? values
    3. bind the malloc'd memory to the ?
    4. execute an sqlite step
    etc. etc. etc....
    This is quite a bit of work just to do an INSERT ???
    The reason I'm doing this is my iphone app has a lot of tiny (20x20) images that get downloaded to the phone periodically from the server. Plus, there are a number of images that come loaded with the application. I was going to save all of these in SQLite. So, I need the command line tool to load up the initial images that come with the app, then I need code to download the images from the server and load them into the database. Of course, reading them out is necessary!
    Please help if you can!? Its late..and I'm blurry eyed from googling SQLIte3 blobs...

    Thank you for your quick reply Liam! You know, I checked the web forms list when I got that error message and the form isn't there. I was having trouble the other day when another of my forms kept getting deleted.
    Sorry if this is a stupid question (I'm a BC newbie) but I want to be sure so I can get this project finished... The proper workflow would be to create this form myself in Web Forms then insert it into the Registration - Buy template using the modules toolbox?
    Thanks to you I may get this done on time still... if my PayPal set up works

  • TS3276 Hi there I am having trouble with my outlook express this is the first time it has happened it keeps telling me can't connect to the SMTP server and every password I have tried is incorrect this is for 'iCloud' very frustrating as all our phones ar

    Hi there I am having trouble with my outlook express this is the first time it has happened it keeps telling me can't connect to the SMTP server and every password I have tried is incorrect this is for 'iCloud' very frustrating as all our phones are ok

    Well I am feeling a bit silly now because that was all I had to do. In the past Workgroup Manager had always opened right up to the list of my users, I never even thought to click on that but apparently that was all that was needed.
    I still am not sure how the iChat Server preferences changed themselves on me, but it is not the first time I have had weird "settings" issues with our Snow Leopard Server.
    Thanks!

  • How to use this query in R12

    the query below , i am using in valueset :
    select * from AR_LOCATION_VALUES
    Where ar_location_values.location_segment_qualifier =
    'COUNTRY' and ar_location_values.location_structure_id in ( select location_structure_id from ar_system_parameters )
    how to use this query to set in R12?
    Thanks

    hi
    i am using the following query in 11i and i want to use the same in R12 :
    SELECT ar_location_values.location_segment_description,ar_location_values.location_segment_value,location_segment_id
    FROM AR_LOCATION_VALUES
    WHERE ar_location_values.location_segment_qualifier = 'COUNTRY'
    AND ar_location_values.location_structure_id IN
    (SELECT location_structure_id FROM ar_system_parameters)
    note: the table ar_location_values is obsolette in R12 so what is the replacement table in R12 for this?

  • How to modify this query to get the desired output format

    I hv written a Query to display all the parent table names and their primary key columns(relevant to this foreign key of the child table).The query is given below...
    SELECT DISTINCT(TABLE_NAME) AS PARENT_TABLE,COLUMN_NAME AS PARENT_COLUMN
    FROM ALL_CONS_COLUMNS
    WHERE CONSTRAINT_NAME IN (SELECT AC.R_CONSTRAINT_NAME
    FROM ALL_CONSTRAINTS AC
    WHERE AC.TABLE_NAME=TABLE_NAME
    AND AC.TABLE_NAME='&TABLE'
    AND AC.R_CONSTRAINT_NAME IS NOT NULL);
    This query will display all the parent tables and their primary key columns.Now my problem is that how to modify this query to also display the foreign key column name of the child table.
    I want the query result in the following format.The query should display the following columns.1)child table's name,2)child table's foreign key column name,3)the corresponding parent table's name,4)the parent table's primary key column name(which is the foreign key in the child table).
    For Example I want the output as follows...
    TAKE THE CASE OF SCOTT.EMP(AS INPUT TO YOUR QUERY)
    CHILD_TABLE CHILD_COLUMN PARENT_TABLE PARENT_COLUMN
    EMP DEPTNO DEPT DEPTNO
    In this result I hv used alias name for the columns.The query should display this only for the foreign keys in the child table.In the query which I sent to you earlier will give the parent table and the parent column names,But I also want to append the child table and child column names there.
    any help on how to tackle would be appreciated.

    Try this query
    SELECT c.table_name child_table,
         c.column_name child_column,
         p.table_name parent_table,
         p.column_name parent_column
    FROM user_constraints a,user_constraints b,user_cons_columns c,
         user_cons_columns p
    WHERE a.r_constraint_name=b.constraint_name and
          a.constraint_name=c.constraint_name and
          b.constraint_name=p.constraint_name and
          c.position=p.position
    ORDER BY c.constraint_name,c.position
    Anwar

  • How to solve this query

    hi,
    i have a product table like
    product month1 month2 month3 .................
    soap 1200 1256 1895 ............
    i want use a query where i can select column name with a parameter.
    like
    select month||:num from product;
    in num variable it cud be 1 to 10 of value that is dependent on my program.
    so how to make this query .
    thxs

    Hi,
    Here is an example that i am helpful.
    In the example , I am using a table 'table_name' which contains columns like
    assign_attribute1
    assign_attribute2
    assign_attribute15
    Now I will pass any number from 1 to 15 to the function.
    create or replace procedure pass_col_number(v_number varchar2) as
    v_sql varchar2(2000);
    v_assign_attribute1   varchar2(150);
    begin
    v_sql := 'select  assign_attribute'||v_number||'  from  table_name where person_id = 1345';
    execute immediate v_sql into v_assign_attribute1;
    dbms_output.put_line('v_assign_attribute1='||v_assign_attribute1);
    end;Edited by: Sreekanth Munagala on Dec 18, 2008 1:18 AM

  • How to combine this query so that i can display the ouput together

    I have no idea how to combine this query together.Someone please help.I want the ouput to display oni 1 result combining all together.
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('6910','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('6912','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('7344','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('8344','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    Edited by: 965547 on Nov 5, 2012 12:55 AM

    The only difference which i am seeing in your queries is the Operation ('6910', '6912','7344','8344')
    Then why don't you put all values in One like this
    Select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in *('6910', '6912','7344','8344','7976')* AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    Hope this will resolve your problem.
    Oracle-911

  • How to tune this query for the improve performance ?

    Hi All,
    How to tune this query for the improve performance ?
    select a.claim_number,a.pay_cd,a.claim_occurrence_number,
    case
    when sum(case
    when a.payment_status_cd ='0'
    then a.payment_est_amt
    else 0
    end
    )=0
    then 0
    else (sum(case
    when a.payment_status_cd='0'and a.payment_est_amt > 0
    then a.payment_est_amt
    else 0
    end)
    - sum(case
    when a.payment_status_cd<>'0'
    then a.payment_amt
    else 0
    end))
    end as estimate
    from ins_claim_payment a
    where a.as_of_date between '31-jan-03' and '30-aug-06'
    and ( a.data_source = '25' or (a.data_source between '27' and '29'))
    and substr(a.pay_cd,1,1) IN ('2','3','4','8','9')
    group by a.claim_number, a.pay_cd, a.claim_occurrence_number
    Thank you,
    Mcka

    Mcka
    As well as EXPLAIN PLAN, let us know what proportion of rows are visited by this query. It may be that it is not using a full table scan when it should (or vice versa).
    And of course we'd need to know what indexes are available, and how selective they are for the predicated you have in this query ...
    Regards Nigel

  • How to write this query to filter combination of few values

    Hi,
    I have a table CHIMM which is a transaction table and contains information of the vaccines given to a child.
    columns are: child_id, vacc_id, vacc_given_dt. I have to query for remaining vaccines.
    HEXA is a vaccine_id which is composite vaccine of DPT1,POL1,HBV1 & HIB1 (vaccine ids).
    I want to write to query if any of DPT1,POL1,HBV1 & HIB1 given then HEXA should not be displayed in the result.
    OR
    if HEXA is given then of course any of DPT1,POL1,HBV1 & HIB1 should not be displayed in the result.
    How to write this query?
    Regards

    Hi,
    I'm still not sure what the output you want from that sample data is. Do you just want the child_ids, like this
    CHILD_ID
           3
           4? If so, here's one way to get them:
    WITH     all_vacc_ids     AS
         SELECT     c.child_id
         ,     c.vacc_id          AS child_vacc_id
         ,     v.vacc_id
         ,     COUNT ( CASE
                             WHEN  c.vacc_id = 'HEXA'
                       THEN  1
                         END
                    )       OVER ( PARTITION BY  c.child_id
                                       )    AS hexa_itself
         FROM          vacc   v
         LEFT OUTER JOIN     chimm  c     PARTITION BY (c.child_id)
                          ON     c.vacc_id     = v.vacc_id
         WHERE   v.vacc_desc       = 'HEXA'     -- See note below
    SELECT       child_id
    FROM       all_vacc_ids
    WHERE       child_vacc_id     IS NULL
      AND       vacc_id     != 'HEXA'
      AND       hexa_itself     = 0
    GROUP BY  child_id
    rha2 wrote:there are alot of vaccines, i just put 3 for example. this query gives error: invalid relational operatorAre you saying that the vacc table contains other rows, but those other rows are not needed for this problem? It would be good if you included an example in the sample data. The query above considers only the rows in vacc where vacc_desc='HEXA'. You can have other rows in the vacc table, but they won't affect the output of this query. The query above makes no assumptions about the number of rows that have vacc_desc='HEXA'; it will report all child_ids who are missing any of them, regardless of the number (assuming the child does not have the 'HEXA' vacc_id itself, like child_id=1).
    You still haven't said which version of Oracle you're using. The query above will work in Oracle 10 (and higher).

Maybe you are looking for