A query containing a subquery and Group By all-in-one

Hi people,
Any interesting fact but nerve racking situation I have here. I have a question that I have to do counts and amounts and I also need to have a Sub inside query. When I run my application, I get an error like this:

Error in the command line column: 4:36
Error report:
SQL error: ORA-00979: not a GROUP BY expression
00979 00000 - "not a GROUP BY expression"
* Cause:
* Action:

Here's my query:
select distinct to_char(c.start_date,'YYYY') start_date,
         substr(acc.account_num,1,12) account_number,
       ( select acc2.account_desc from student_account  acc2
         where  acc2.account_num = acc.account_num 
         and    acc2.account_type = 'A'
       ),
       COUNT(distinct(s.student_id))
from student s,
       student_account acc,
       course   c
where s.course_id = c.course_id
and     s.account_num = acc.account_num
and    to_char(c.start_date, 'YYYY/MM/DD') between '2004/01/01' and '2009/12/31'
group by to_char(c.start_date,'YYYY'), substr(acc.account_num,1,12)
order by 1;
If I replace it with the acc.account_num (subquery) with a number that is in the database, then my request works well.

Published by: Roxyrollers on March 29, 2011 16:45

Hello

We don't know what you want. It would really help if you posted some examples of data (CREATE TABLE and INSERT statements) and the results desired from these data.

Are you sure that the scalar subquery will return ever more than one line? If so, you can wrap in a MIN or MAX aggregate function, as I've suggested before:

 select distinct to_char(c.start_date,'YYYY') start_date,
         substr(acc.account_num,1,12) account_number,
   MIN ( ( select acc2.account_desc from student_account  acc2     -- Changed
         where  acc2.account_num = acc.account_num
         and    acc2.account_type = 'A'
       ) ),                                   -- Changed
       COUNT(distinct(s.student_id))
from student s,
       student_account acc,
       course   c
where s.course_id = c.course_id
and     s.account_num = acc.account_num
and    to_char(c.start_date, 'YYYY/MM/DD') between '2004/01/01' and '2009/12/31'
group by to_char(c.start_date,'YYYY'), substr(acc.acct_acc_num,1,12)
order by 1;

It seems that you could also simplify the query and make more fast, eliminating the scalar subquery. The table there is already part of the main query, you can do something like this:

SELECT    TO_CHAR (c.start_date, 'YYYY')     AS start_date
,         SUBSTR (acc.account_num, 1, 12)      AS account_number
,       MIN ( CASE
                    WHEN  acc.account_type = 'A'
              THEN  account_desc
                END
           )                         AS a_desc
,         COUNT (DISTINCT s.student_id)          AS student_id_cnt
FROM       student            s
,       student_account     acc
,       course          c
WHERE       s.course_id          = c.course_id
AND       s.account_num          = acc.account_num
AND       c.start_date          BETWEEN DATE '2004-01-01'
                      AND     DATE '2009-12-31'
GROUP BY  TO_CHAR (c.start_date, 'YYYY')
,            SUBSTR (acc.acct_acc_num, 1, 12)
ORDER BY  start_date;

I guess that course.start_date is a DATE. Instead of converting each start_date to string (so you can see if it is in the right range), it is more efficient and less prone to error, compared to other DATEs, as shown above.

It is very strange that you

substr(acc.account_num,1,12)

in the select clause, but

substr(acc.acct_acc_num,1,12)

in the GROUP BY clause. Did you mean having the same column in both places?

Yet once, without seeing some examples of data (CREATE TABLE and INSERT statements) and the results desired from this data, I can't do much.

Tags: Database

Similar Questions

  • Can someone tell me why I lost all my accurate color between iPhoto and my C8180 All-in-one?

    I was full of pictures from iPhoto printing yesterday and all of a sudden the color accuracy went in the toilet.

    Everything is green and stupid.  It's terrible.  The strange part... it allows to print very well everywhere else.

    This morning, I slipped a photo from iPhoto to the desktop and then print it from there.  Went well.

    I lost something between iPhoto and my C8180 All-in-one printer.  I've never encountered this problem

    front.  I tried all of the suggestions of the printer, but I'm sure that's not the problem.  The printer and

    iPhoto do communicate specifically.  Someone?

    Thanks for the 2 responses I received.  Unfortunately, neither contributed to my problem.  The problem

    was not with my version of OS X or the HP software that has been installed.  As I have explained, this set-up has been

    works perfectly for the last 3-4 years.  With the help of my long-time friend and Mac user many, many

    years, we determined that it was a corrupt iPhoto preferences file.  We have replaced the files preference and

    It works perfectly again.  The problem had nothing to do the printer or it's software.  I am back

    in the business.  Hope this might help someone else.

  • Just upgraded to Windows 7 and my Dell All IN One Printer does not print. It is said that the pilot of the AIO 942 not found

    Just upgraded to Windows 7 and my Dell All IN One Printer does not print.  It is said that the pilot of the AIO 942 not found.

    Anyone where I can download this driver?

    See http://www.microsoft.com/windows/compatibility/Windows-7/en-us/Details.aspx?type=Hardware&p=Dell%20942%20Photo%20All-In-One%20Printer&v=Dell&uid=942STD&pf=0&pi=1&c=Printers%20%26%20Scanners&sc=Multifunction&os=32-bit.

    There are only drivers for Vista at http://www.dell.com/support/drivers/us/en/04/DriverDetails/DriverFileFormats?c=us&s=bsd&cs=04&l=en&DriverId=R151559.

    They are supposed to work if installed in compatibility mode. http://en.community.dell.com/support-forums/peripherals/f/3528/t/19319527.aspx.

  • [8i] need help on query with a subquery/inline using UNION ALL view

    OK, first of all let's start with some background info:
    (1) I work in 8i
    (2) some samples for my problem (please excuse the additional columns of data not used to this problem; I already had the sample laying around tables):
    CREATE TABLE     vbom
    (
         part_nbr     char(25)     
    ,     bom_doc_nbr     char(25)     
    ,     bill_level     number          
    ,     comp_part_nbr     char(25)     
    ,     qty_per          number
    );
    --technically has primary and foreign keys, but whatever...
    --part_nbr is your top level (0) parent
    --comp_part_nbr is the specific child
    --bom_doc_nbr is the parent of the child (comp_part_nbr) and may be either part_nbr or a child of part_nbr that is also a parent
    
    INSERT INTO vbom
    VALUES ('SAMPLE-1','SAMPLE-1', 1,'SAMPLE-2',1);
    
    CREATE TABLE     rqmt
    (
         comp_part_nbr     char(25)     
    ,     prnt_part_nbr     char(25)
    ,     ord_nbr          char(10)     
    ,     sub_ord_nbr     char(3)
    ,     qty_reqd     number
    ,     qty_issued     number
    ,     date_reqd     date
    ,     rqmt_stat     char(2)
    ,     rqmt_type     char(2)
    );
    
    INSERT INTO rqmt
    VALUES ('SAMPLE-1','','S0000TEST1',001,30,0,TO_DATE('06/01/2010','mm/dd/yyyy'),'AL','ID');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000054963',001,15,10,TO_DATE('04/01/2010','mm/dd/yyyy'),'CL','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000032562',001,5,5,TO_DATE('04/15/2010','mm/dd/yyyy'),'IS','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000022341',001,5,4,TO_DATE('04/20/2010','mm/dd/yyyy'),'SH','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000043469',001,10,0,TO_DATE('04/30/2010','mm/dd/yyyy'),'AL','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000071235',001,10,0,TO_DATE('05/01/2010','mm/dd/yyyy'),'OP','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000061224',001,5,0,TO_DATE('05/15/2010','mm/dd/yyyy'),'FP','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000033294',001,5,0,TO_DATE('05/25/2010','mm/dd/yyyy'),'PL','DD');
    So, my first question was that I needed to find in the table RQMT who corresponded with everything on my VBOM have vbom.part_nbr table ' SAMPLE-1'. '. (Please note, in reality the VBOM table has thousands of rows of data, rather than just the 1 I have planned, some of them sharing the same vbom.part_nbr, others not). In addition to finding all RQMT data corresponding to the vbom.comp_part_nbr to vbom.part_nbr (where vbom.comp_part_nbr = rqmt.comp_part_nbr), I also need to find RQMT data for vbom.part_nbr itself (then, records where vbom.part_nbr = rqmt.comp_part_nbr).

    To resolve this problem, I started with the following query:
    SELECT     v.bill_level          AS bill_lvl
    ,     v.comp_part_nbr          AS component
    ,     v.bom_doc_nbr          AS parent
    FROM     VBOM v
    WHERE     v.part_nbr     ='SAMPLE-1'
    UNION ALL
    SELECT     0               AS bill_lvl
    ,     'SAMPLE-1'          AS component
    ,     NULL               AS parent
    FROM DUAL
    It allows me to add a line to the top of the page parent (vbom.part_nbr), which does not exist in vbom.
    My first question is, is UNION ALL of the best way to do it, or could I do a kind of instruction based on the line number box (or something) that would be better? (in this application, or in the final query)

    Then, I used the above query as a point of view online:
    SELECT     a.bill_lvl
    ,     a.component
    ,     a.parent
    ,     r.ord_nbr
    ,     r.sub_ord_nbr
    ,     r.qty_reqd
    FROM     (
         SELECT     v.bill_level          AS bill_lvl
         ,     v.comp_part_nbr          AS component
         ,     v.bom_doc_nbr          AS parent
         FROM     VBOM v
         WHERE     v.part_nbr     ='SAMPLE-1'
         UNION ALL
         SELECT     0               AS bill_lvl
         ,     'SAMPLE-1'          AS component
         ,     NULL               AS parent
         FROM DUAL
         ) a
    ,     RQMT r
    WHERE     a.component     = r.comp_part_nbr
    The problem here is that I have the same results (7 files) with the above query as I do with:
    SELECT     v.bill_level
    ,     v.comp_part_nbr
    ,     v.bom_doc_nbr
    ,     r.ord_nbr
    ,     r.sub_ord_nbr
    ,     r.qty_reqd
    FROM     VBOM v
    ,     RQMT r
    WHERE     v.comp_part_nbr     = r.comp_part_nbr
    AND     v.part_nbr     = 'SAMPLE-1'
    .. .it does not include RQMT recording for rqmt.comp_part_nbr = "SAMPLE-1'.

    To understand what was going on, I ran:
    SELECT     a.bill_lvl
    ,     a.component
    ,     a.parent
    FROM     (
         SELECT     v.bill_level          AS bill_lvl
         ,     v.comp_part_nbr          AS component
         ,     v.bom_doc_nbr          AS parent
         FROM     VBOM v
         WHERE     v.part_nbr     ='SAMPLE-1'
         UNION ALL
         SELECT     0               AS bill_lvl
         ,     'SAMPLE-1'          AS component
         ,     NULL               AS parent
         FROM DUAL
         ) a
    .. .and I get exactly this that I wait, 1 card for the SAMPLE-2 component and 1 plug for SAMPLE-1 (i.e. the recording I generated with the UNION ALL).

    Then I ran:
    SELECT     r.comp_part_nbr
    ,     r.ord_nbr
    ,     r.sub_ord_nbr
    ,     r.qty_reqd
    FROM     RQMT r
    WHERE     r.comp_part_nbr like 'SAMPLE-%'
    .. .and I get all 8 records, whose SAMPLE-1.

    So, it seems that the problem occurred when I join my opinion RQMT one online, although both separately seem to work very well.

    A point to note: all parts of parent and child may not be appointed in the same way as in my example. SAMPLE-1 could have children of the SAMPLE-2, abc123, 20735, for example.

    Any help on this would be appreciated, thanks!

    Hello

    Concerns of problem by comparing 8 literal characters ' SAMPLE-1' for the 25-character CHAR column that contains "SAMPLE 1".»
    17 spaces including the literal or use LPAD or CAST to 25 characters.
    For example:

    SELECT     a.bill_lvl
    ,     a.component
    ,     a.parent
    ,     r.ord_nbr
    ,     r.sub_ord_nbr
    ,     r.qty_reqd
    FROM     (
         SELECT     v.bill_level          AS bill_lvl
         ,     v.comp_part_nbr          AS component
         ,     v.bom_doc_nbr          AS parent
         FROM     VBOM v
         WHERE     v.part_nbr     ='SAMPLE-1'
         UNION ALL
         SELECT     0               AS bill_lvl
         ,     CAST ('SAMPLE-1' AS CHAR (25))               -- CAST added here
                                      AS component
         ,     NULL               AS parent
         FROM DUAL
         ) a
    ,     RQMT r
    WHERE     a.component     = r.comp_part_nbr
    ;
    

    Is there a reason why you use CHAR instead of VARCHAR2? (One reason other than "I can't change it now.", otherwise said.)

  • Portable printing and Scaning between an HP Pavilion dv6 PC and F310 HP all in one printer / Scanner

    I'm trying to interface a new HP laptop that has a 64-bit (HP Pavilion dv6-701) operating system and running the operating system Windows 7 on a HP All in one series F310 printer / Scanner

    HP staff helped get printing to work via a 6980 HP printer Driver software download, try to download the software 310 driver didn't work so we tried this driver 6980 credits and print function works now

    However I can't scan a document on the laptop of the 310, when I attmpt one scan, I receive an error message

    Given that the printer uses a generic driver and is not a software that is supported on Windows 7, you can scan only programs such as paint, Windows Fax and Scan and image viewer.

  • Impossible to copy and scan on all-in-one printer, HP Deskjet F4180.

    I can't make a copy and scan on the surface of the glass of my printer HP deskjet F4180 all-in-one. The printer says: error in the copy menu and seeing red flashes on color and black. However, I am able to print from my laptop and a pop-up message would be in my laptop showing the ink cartridges (black and color) are placed incorrectly. Check the ink cartridges, both are in their right place and broken up. Need help... Please.

    I found this document which decodes what mean flashing lights.  There are a lot of different codes described, but I found a heading "question: one or two of the lights blink check print cartridge" which seems to be what's going on your printer.  Basically it's saying that the cartridges are out of alignment or defective.  If the document is not help clarify, let me know and I'll see what I can find.

  • Windows 7 and scanning. All-in-one printer, HP Photosmart C5180

    I had some experience in the past with an upgrade of Windows 2000, XP and a printer/Scanner, I remember more that now, but I managed to get an upgrade for the printer but the scanner never worked despite the many phone calls from HP again. I have a HP Photosmart C5180 all-in-One printer and I'm about to upgrade my XP computer to Windows 7. Y at - it an upgrade that will manage the printer and the Scanner?

    Thank you. New PC comes next week. I will be presenting on the question of whether it works or not

  • How the program REDOWNLOAD SHREDDER64. MSI? Windows 7 and HP 4620 all-in-One Printer

    I have to clean programs unused and deleted the Shredder4.msi, which I just found out that I must have to operate properly the functions of my

    4620 all-in-One Priner/Fax/Scanner HP.

    I looked and looked and can't find the program to reinstall.

    I would appreciate any direction that could help me in this company?

    Kind regards

    Judy

    * E-mail address is removed from the privacy * or

    E-mail address is removed from the privacy *.

    I recommend that you check with HP.

    You still have the CD that came with the printer?

    It is suitable for this topic.

    Edit: You have an ACER computer?

    They have a preinstalled by Egistec shredder program.

  • find emails in email group, by sending one, update contact and map Data - all in one

    Hello

    I wanted to know if I'll be able to do the following:

    1. pull the names of all emails in a given e-mail group

    2 extract contact data

    3. send an email from (1)

    4 put to update the data of contact and one or two were data cards

    All before I would need to do a program step, with custom cloud connector / API.

    Possible? Any guidance welcome.

    Thanks for your help,

    Adam

    Hi Adam,.

    (1) you can search by name, or list all emails and filter them.  You are not able to pass a name or a group id and retrieve emails within the group.

    (2) you can use the standard or DTS WSDL to extract data contact (E9 and E10).

    With a (generator program with external services) cloud connector you first list of contacts in the status stage and pull their contact ID. You can then use the Standard Service extract to extract contact data.

    (3) you can send an email to 1 contact both using electronic mail (E10 and E9) using the id contact comes from the stage of the program or send to an existing distribution list (E9 only)

    (4) you can do so via the standard WSDL (E9 and E10) or DTS WSDL (E9 only)

    Cheers, Aaron.

  • Vista and Dell A920 all-in-one

    I've asked this question before and received the response on the withdrawal of the old driver and install the driver updated from the Web site.  I now have the problem of no communication between the computer and the printer/scanner.  I've done all the troubleshooting section for this problem of the printer page and looked in Device Manager stating that the printer/scanner is connected and ready for use according to the diagnostic test... firewall is configured to allow the printer to go through it, etc...   Any suggestions other than that?    Thank you

    chrysiis-chrysiisart,

    Can you please run the search for system information REMOVE YOUR PRODUCT ID and post the rest of feedback here. If the information is in another language, please do NOT translate it. It is important information that I need.

    You have a Dell A920 printer and what program anti-virus and firewall are you using?

    Rick

  • And clearing memory all-in-one HP Deskjet 5440 and Officejet 7310xi

    How to erase the memory of the printer on a HP Deskjet 5440 and an Officejet 7310xi before recycling?

    dgilchri wrote: How do I clear the memory of the printer on a HP Deskjet 5440 and an Officejet 7310xi before recycling?

    Hi dgilchri, when you unplug the power cord on the printers, all memory stored in the printers are refreshed (disabled).

  • updated to correct driver for windows 7 and photosmart c6280 all-in-one

    I bought the new computer with windows 7.  Tried to download recommended driver [a/o 02 Netwired_Full_Win_WW_130_140.exe.], but got the message that it is not commonly downloaded and it may harm your computer is not sure as what to do now?  Bought printer Jan 2008

    Thanks for the info.  I found myself just try the download again and it went through smoothly and works!  I used the HP website for support.

  • Sorting and grouping - two months in this query

    Hi all

    Thanks a lot for JAC

    I am doing a project for the construction company, I faced this problem by grouping points according to the relationship between these points the
    Relationship is from 1 to 100. If the point between what means there is relationship between these points has come.

    resolve this question already, but the results does not correct when the table contains more data.

    SQL - sorting and grouping.
    Jac and thanks a lot to him.

    This example for more details

    for example, I have these points
    id   location         percentage   comments 
    1     loc 1,2          20%                that mean point  1 and 2 close to each other by 20%
    2     loc 1,3          40%              that mean point 1 and 3 close to each other byy 40%
    3     Loc 8,6          25%               that mean point 8 and 6 close to each other by 25% 
    4     Loc  6,10        20%
    5     LOC 11,10        10 %
    6     LOC 15,14         0%
    In addition, we can see the relationship between these points as follows
    -points 1,2,3 in a group why because close 1.2 and 1.3 a relationship which means 1.3 also hid the relationship.
    -Points 6,8,10,11 in the second group there are the relationships between them.
    - but no relationship between 1, 2 or 3 with any point of 6,8,9,10,11
    -as well as no relationship between 15, 14, which means 14 in the third group and 15 in the fourth group.


    Whati need?

    to group the points that growing a relationship according to the percentage value


    The most important part is to group the points. For EXAMPLE, the query below, the gropuing is not correct.

    I have the following table with data
    drop table temp_value; 
    create table temp_value(id number(10),location varchar2(20), percentage number(9)); 
     
     
    insert into temp_value values  (1,'LOC 1,2',10); 
    insert into  temp_value values (2,'LOC 1,3',0); 
    insert into  temp_value values (3,'LOC 1,4',0); 
    insert into  temp_value values (4,'LOC 1,5',0); 
    insert into  temp_value values (5,'LOC 1,6',0); 
    insert into  temp_value values (6,'LOC 2,3',0); 
    insert into  temp_value  values(7,'LOC 2,4',0); 
    insert into  temp_value values (8,'LOC 2,5',30); 
    insert into  temp_value values (9,'LOC 2,6',0); 
    insert into  temp_value values (10,'LOC 3,4',0); 
    insert into  temp_value values (11,'LOC 3,5',0); 
    insert into  temp_value values (12,'LOC 4,5',40); 
    insert into  temp_value values (13,'LOC 4,6',0); 
    insert into  temp_value values (14,'LOC 6,7',40);
    insert into  temp_value values (15,'LOC 7,2',0);
    insert into  temp_value values (16,'LOC 8,2',60);
    insert into  temp_value values (17,'LOC 8,3',0);
    insert into  temp_value values (18,'LOC 3,1',0);
    insert into  temp_value values (19,'LOC 9,6',30);
    insert into  temp_value values (20,'LOC 11,2',0);
    insert into  temp_value values (22,'LOC 12,3',10);
    insert into  temp_value values (23,'LOC 19,3',0);
    insert into  temp_value values (24,'LOC 17,3',0);
    insert into  temp_value values (24,'LOC 20,3',0);
    When I used this query, the results is not correct

     with t as
        (select percentage,loc1,loc2,sum(case when percentage = 0 then 1
                           when loc1 in (l1,l2) then 0
                       when loc2 in (l1,l2) then 0
                       when l1 is null and l2 is null then 0
                       else 1
                  end) over(order by rn) sm
        from (     select id,location,percentage,
                           regexp_substr(location,'\d+',1,1) LOC1,
                          regexp_substr(location,'\d+',1,2)  LOC2,
                         lag(regexp_substr(location,'\d+',1,1))
                          over(order by percentage desc) l1,
                          lag(regexp_substr(location,'\d+',1,2))
                          over(order by percentage desc) l2,
                  row_number() over(order by percentage desc) rn
          from temp_value
          order by percentage desc
            )
      )
       select loc,min(sm)+1 grp
         from(
           select loc,rownum rn,sm
           from(
           select percentage,decode(rn,1,loc1,loc2) loc,sm
           from t a,
                (select 1 rn from dual union all
                 select 2 from dual ) b
           order by percentage desc,decode(rn,1,loc1,loc2) asc
          )
       )
        group by loc
       order by min(sm),min(rn);
    results


    SQL > /.
    LOC                         GRP
    -------------------- ----------
    2                             1
    8                             1
    6                             2
    7                             2
    4                             3
    5                             3
    9                             4
    1                             5
    12                            6
    3                             6
    11                           13
    
    LOC                         GRP
    -------------------- ----------
    19                           14
    17                           15
    20                           22
    
    14 rows selected.
    SQL >


    but the just is
    Location        group No
    2                  1
    8                  1
    4                  1
    5                  1
    1                  1
    6                  2
    7                  2
    9                  2
    12                 3
    3                  3
    19                 4
    17                 5
    20                 6
    Thanks in advance.

    Published by: Isabelle on November 30, 2012 03:07

    OK, I thought an it once again and found out how to include with any such percentage points.
    In your example expected output you missed the 11 that's why we got 7 groups now.
    The order of the Group 2 and 3 is ambiguous, because the highest percentage of these groups is the same.

    with connects as (
    select distinct
     loc1
    ,loc2
    ,connect_by_root(loc1) grp
    ,percentage per
    from
    temp_value
    --start with
    --percentage != 0
    connect by nocycle
    (prior loc2 = loc1
    or
    prior loc1 = loc2
    or
    prior loc1 = loc1
    or
    prior loc2 = loc2)
    and
    percentage != 0
    and
    prior percentage != 0
    )
    
    select
     loc
    ,dense_rank() over (order by decode(per,0,1,0), grp) grp
    from (
    select
     loc
    ,max(grp) keep (dense_rank first order by per desc, grp) grp
    ,max(per) keep (dense_rank last order by per nulls first) per
    from (
    select
     loc1 loc
    ,grp
    ,per
    from connects
    union
    select
     loc2
    ,grp
    ,per
    from connects
    )
    group by
    loc )
    order by 2,per desc,1
    
    LOC     GRP
    2     1
    8     1
    4     1
    5     1
    1     1
    12     2
    3     2
    6     3
    7     3
    9     3
    11     4
    17     5
    19     6
    20     7
    

    Think we are done now ;-)
    Edited by: chris227 at 30.11.2012 16:46

    Edited by: chris227 at 30.11.2012 17:12
    order corrected

    Edited by: chris227 at 30.11.2012 17:15
    simplification, no need to rank in linking subquery

    Edited by: chris227 at 30.11.2012 17:26

  • Rewrite the query with joins, and group by

    Hello

    It's an interview question.

    Table names: bookshelf_checkout
    virtual library

    And the join condition between these two tables is title

    We need to rewrite under request without using the join condition and group by clause?

    SELECT b.title,max(bc.returned_date - bc.checkout_date) "Most Days Out"
               FROM bookshelf_checkout bc,bookshelf b
               WHERE bc.title(+)=b.title
               GROUP BY b.title;
    When I was in College, I read most of SELECT statements can be replaced by operations base SQL (DEFINE the OPERATORS). Now, I am rewriting the query with SET operators, but not able to get the exact result.

    Kindly help me on this.

    Thank you
    Suri

    Something like that?

      1  WITH books AS (
      2  SELECT 'title 1' title FROM dual UNION ALL
      3  SELECT 'title 2' FROM dual UNION ALL
      4  SELECT 'title 3' FROM dual ),
      5  bookshelf AS (
      6  SELECT 'title 1' title, DATE '2012-05-01' checkout_date, DATE '2012-05-15' returned_date FROM dual UNION ALL
      7  SELECT 'title 1' title, DATE '2012-05-16' checkout_date, DATE '2012-05-20' returned_date FROM dual UNION ALL
      8  SELECT 'title 2' title, DATE '2012-04-01' checkout_date, DATE '2012-05-15' returned_date FROM dual )
      9  SELECT bs.title, MAX(bs.returned_date - bs.checkout_date) OVER (PARTITION BY title) FROM bookshelf bs
     10  UNION
     11  (SELECT b.title, NULL FROM books b
     12  MINUS
     13* SELECT bs.title, NULL FROM bookshelf bs)
    SQL> /
    
    TITLE   MAX(BS.RETURNED_DATE-BS.CHECKOUT_DATE)OVER(PARTITIONBYTITLE)
    ------- ------------------------------------------------------------
    title 1                                                           14
    title 2                                                           44
    title 3
    

    Lukasz

  • Cases and Group by

    I tried to understand what is the problem with a large sql query that does not change what I do.
    Have managed to isolate the part which is a failure, but I get the error message:

    ORA-00979: not a GROUP BY expression

    is not very useful.

    I suspect there is something fundamentally wrong in what I'm trying to do.

    I had to anonimise the names of the tables and fields that the data that I use is quite sensitive, but it should not forget the problem.

    Here is the query:
    select
    case when a='100' and cost in (select cost from lookup) then '100' else b end as main,
    count(*)
    from 
    data_table
    group by 
    case when a='100' and cost in (select cost from lookup) then '100' else b end
    DATA_TABLE has (among others) of fields:
    a, b, and the cost

    search contains the field:
    cost

    All fields are VARCHAR2 (255)


    If I remove the count (*) and any group of the query statement runs it is to say:
    select
    case when a='100' and cost in (select cost from lookup) then '100' else b end as main
    from 
    data_table
    This shows that the stated case is valid - then why I can not combine the count and group of?
    If this is a reason of syntax is there another solution that I can possibly use subqueries to work around the problem - I prefer to avoid this and can't really make the case in a decoding without him make mess.

    Oracle version:
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.

    TIA

    Published by: user8378443 on May 11, 2011 10:26

    Try like this,

    SELECT MAIN, COUNT (*)
      FROM (SELECT CASE
                      WHEN A = '100'
                       AND COST IN (SELECT COST FROM lookup)
                      THEN '100' ELSE b
                   END
                      AS MAIN
              FROM data_table)
    GROUP BY MAIN
    

    G.

Maybe you are looking for

  • Noisy fan of Satellite C660

    Hello! I have a laptop Toshiba C660-1DN, I bought it in October. My problem is, that the cooling fan starts within the first 2-3 minutes, and it continues to turn, until I have shut down the computer. It is loud and very annoying.I installed all the

  • Maximum RAM for Y460N

    Hi all I want to update my RAM for my Lenovo Y460N, but I do not know the RAM max it can supports. Can someone advise me on this... COS of the user manual doesn't say RAM max... Thank you very much! Martin

  • Diode circuit does not

    Hi all! I'm having a problem getting my simulated circuit to translate in real life. Please see image of the circuit or circuit attached. I'm just checking the voltage at the point of 'test '. When I run a simulation, I get a voltage square wave at t

  • Everything on the screen went super-sized! How can I restore back to normal?

    Everything on the screen went super-sized! How can I restore back to normal? * original title - Super-sized icons, cursor, etc. ! *

  • Officejet Pro 8600 custom paper sizes

    I just replaced my Officejet Pro 8500 with a 8600.  I NEED to print on paper that is 4.88 "x 8.25".  How can I get the printer to accept this paper?  I use Word 2010 and that you set the paper size in Word, but the printer indicates the paper size do