Help with making SQL query references to column aliases in the Case statement

I need help with a sql query that I'm trying. I can go about it the wrong way, but I would be grateful if I could get any suggestions on possible solutions. This is my query:


SELECT DISTINCT spriden_pidm, spriden_id id, spriden_last_name | ',' | spriden_first_name name,

CASE
WHEN rcresar_comm_code_01 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_01
WHEN rcresar_comm_code_02 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_02
WHEN rcresar_comm_code_03 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_03
WHEN rcresar_comm_code_04 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_04
WHEN rcresar_comm_code_05 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_05
WHEN rcresar_comm_code_06 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_06
WHEN rcresar_comm_code_07 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_07
WHEN rcresar_comm_code_08 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_08
WHEN rcresar_comm_code_09 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_09
WHEN rcresar_comm_code_10 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_10
END acg_elig_comm_code

CASE
WHEN acg_elig_comm_code = ' 268' THEN 'rigorous HS course. "
WHEN acg_elig_comm_code = '269' THEN ' 2 or several AP or IB"
WHEN acg_elig_comm_code = '270' THEN 'NOC as possible ".
END comm_code_description

OF spriden, rcresar, rcrapp1

WHERE (rcresar_comm_code_01 IN ('268 ', '269', ' 270')

OR rcresar_comm_code_02 ('268 ', '269', ' 270')

OR rcresar_comm_code_03 ('268 ', '269', ' 270')

OR rcresar_comm_code_04 ('268 ', '269', ' 270')

OR rcresar_comm_code_05 ('268 ', '269', ' 270')

OR rcresar_comm_code_06 ('268 ', '269', ' 270')

OR rcresar_comm_code_07 ('268 ', '269', ' 270')

OR rcresar_comm_code_08 ('268 ', '269', ' 270')

OR rcresar_comm_code_09 ('268 ', '269', ' 270')

OR rcresar_comm_code_10 ('268 ', '269', ' 270'))


Rcresar_aidy_code = & aidy_code

AND rcrapp1_aidy_code = rcresar_aidy_code

AND rcrapp1_curr_rec_ind = 'Y '.

AND rcrapp1_seq_no = rcresar_seq_no


AND spriden_pidm = rcresar_pidm

AND rcrapp1_pidm = rcresar_pidm


AND spriden_change_ind IS NULL

ORDER BY name


The second case statement is where I don't know exactly what it takes to get what I want.

Output should be like:
spriden_pidm name ID acg_elig_comm_code comm_code_description
«0000000000', ' 1111111111 ","John Doe","268", «rigorous HS race"»

If I take the second case statement it works great except that I do not have my comm_code description column. My question is how can I use my first statement value box to determine this column? I think that I need a case statement as I have, but I don't know how to reference the value of acg_elig_comm_code. Any help would be greatly appreciated. Thank you.

Published by: blackhole82 on January 20, 2009 09:20

Hello

You cannot use the alias column in the query, even where it is set (except in the ORDER BY clause).
You can set the alias in a subquery and then use it in a great query, like this:

WITH  sub_q  AS
(
    SELECT DISTINCT spriden_pidm,spriden_id id, spriden_last_name||', '||spriden_first_name name,
        CASE
            WHEN rcresar_comm_code_01 IN ('268','269','270') THEN rcresar_comm_code_01
            WHEN rcresar_comm_code_02 IN ('268','269','270') THEN rcresar_comm_code_02
            WHEN rcresar_comm_code_03 IN ('268','269','270') THEN rcresar_comm_code_03
            WHEN rcresar_comm_code_04 IN ('268','269','270') THEN rcresar_comm_code_04
            WHEN rcresar_comm_code_05 IN ('268','269','270') THEN rcresar_comm_code_05
            WHEN rcresar_comm_code_06 IN ('268','269','270') THEN rcresar_comm_code_06
            WHEN rcresar_comm_code_07 IN ('268','269','270') THEN rcresar_comm_code_07
            WHEN rcresar_comm_code_08 IN ('268','269','270') THEN rcresar_comm_code_08
            WHEN rcresar_comm_code_09 IN ('268','269','270') THEN rcresar_comm_code_09
            WHEN rcresar_comm_code_10 IN ('268','269','270') THEN rcresar_comm_code_10
        END acg_elig_comm_code   -- Originally posted with , here (error)
    FROM spriden, rcresar, rcrapp1
    WHERE (rcresar_comm_code_01 IN ('268','269','270')
            OR rcresar_comm_code_02 IN ('268','269','270')
            OR rcresar_comm_code_03 IN ('268','269','270')
            OR rcresar_comm_code_04 IN ('268','269','270')
            OR rcresar_comm_code_05 IN ('268','269','270')
            OR rcresar_comm_code_06 IN ('268','269','270')
            OR rcresar_comm_code_07 IN ('268','269','270')
            OR rcresar_comm_code_08 IN ('268','269','270')
            OR rcresar_comm_code_09 IN ('268','269','270')
            OR rcresar_comm_code_10 IN ('268','269','270'))
    AND rcresar_aidy_code = &aidy_code
    AND rcrapp1_aidy_code = rcresar_aidy_code
    AND rcrapp1_curr_rec_ind = 'Y'
    AND rcrapp1_seq_no = rcresar_seq_no
    AND spriden_pidm = rcresar_pidm
    AND rcrapp1_pidm = rcresar_pidm
    AND spriden_change_ind IS NULL
)
SELECT    sub_q.*,
          CASE
              WHEN acg_elig_comm_code = '268' THEN 'Rigorous HS course'
              WHEN acg_elig_comm_code = '269' THEN '2 or more AP or IB'
              WHEN acg_elig_comm_code = '270' THEN 'ACG possible'
          END comm_code_description
FROM      sub_q
ORDER BY  name

Furthermore, you might think to rearrange your table, so that you do not have 10 columns (rcresar_comm_code_01, rcresar_comm_code_02,...) that essentially do the same thing. The usual way to handle this kind of one-to-many relationship is to have all rcresar_comm_codes in a separate table, one per line, with a pointer to the table where you have them now.

Published by: Frank Kulash, January 20, 2009 11:35
Syntax error has been corrected

Tags: Database

Similar Questions

  • Need help with a SQL query

    Hello

    I have a data in table (raj_table) with columns (char11) raj_id, raj_number (varchar2 (15)), raj_format (NUMBER), Primary_ID (identity with the values of the primary key column)

    Primary_ID raj_id Raj_number Raj_format

    1                            raj                 rajvend                      1

    2                            raj                 rajvend                      1

    3                            raj                 rajvendor1                 2

    4                            raj                 rajvendor1                 2

    5                            raj                 rajvendor1                 2

    6                            raj                 rajvendor2                 3

    I used under SQL to get query output as below, but has not achieved the required result:

    Select client_id vendor_number, vendor_format, primary_id, row_number() on sl_no (client_id partition, primary_id, vendor_format order of client_id primary_id, vendor_format, vendor_number, vendor_number)

    from raj_table by sl_no asc

    SL_NO raj_id raj_number raj_format primary_id

    1                   1                   raj              rajvendor                 1

    1                   2                  raj              rajvendor                 1

    2                   3                   raj              rajvendor1                2

    2                   4                   raj              rajvendor1                2

    2                   5                  raj               rajvendor1                2

    3                   6                    raj              rajvendor2                3

    I need help with a SQL query to get the result as above without using the group by clause. I want to bring together the combination of separate line of the three columns (raj_id, raj_number, raj_format) and add a unique serial number for each online game (SL_NO column below). So, above there are 3 unique set of (raj_id, raj_number, raj_format) I can get in a group by clause, but I can not add prmiary_id, SL_NO values if I group by clause. I used the analytical functions like row_number() but no luck. Need solution for this.

    with t as)

    Select 'raj' raj_id, 'rajvend' raj_number, 1 raj_format, 1 primary_id Union double all the

    Select option 2, 'raj', 'rajvend', 1 double Union all

    Select 3, 'raj', 'rajvendor1', 2 double Union all

    Select 4, 'raj', 'rajvendor1', 2 double Union all

    Select 5, 'raj', 'rajvendor1', 2 double Union all

    Select 6, 'raj', 'rajvendor2', 3 double

    )

    Select dense_rank() over (order of raj_id, raj_number, raj_format) sl_no,

    t.*

    t

    order by primary_id

    /

    PRIMARY_ID RAJ RAJ_NUMBER RAJ_FORMAT SL_NO
    ---------- ---------- --- ---------- ----------
    1 1 raj rajvend 1
    1 2 raj rajvend 1
    2 3 raj rajvendor1 2
    2 4 raj rajvendor1 2
    2 5 raj rajvendor1 2
    3 6 raj rajvendor2 3

    6 selected lines.

    SQL >

    SY.

  • Need help with PL/SQL query complex

    I need help with a query that need access to data from 3 tables. That's what I did

    I created 3 tables

    CREATE TABLE post_table
    (
    post_id varchar (20),
    datepost DATE,
    KEY (post_id) elementary SCHOOL
    ) ;

    CREATE TABLE topic
    (
    TOPIC_ID varchar (20),
    name varchar (20),
    PRIMARY KEY (topic_id)
    );

    CREATE TABLE blogpost_table
    (
    TOPIC_ID varchar (20),
    post_id varchar (20),
    PRIMARY KEY (topic_id, post_id);
    FOREIGN KEY (topic_id) REFERENCES topic (topic_id) ON DELETE CASCADE,
    FOREIGN KEY (post_id) REFERENCES post_table (post_id) ON DELETE CASCADE
    );


    Now, I inserted a few values in these tables as

    INSERT INTO post_table VALUES ('p1', to_date ('2009-09-14 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p2', to_date ('2009-07-18 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p3', to_date ('2009-07-11 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p4', to_date ('2009-03-11 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p5', to_date ('2009-07-13 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p6', to_date ('2009-06-12 18:00 "," MM/DD/YYYY mi:ss'));))
    INSERT INTO post_table VALUES ('p7', to_date ('2009-07-11 18:00 "," MM/DD/YYYY mi:ss'));))

    INSERT INTO VALUES subject ("t1", "baseball");
    INSERT INTO category VALUES ('t2', 'football');

    INSERT INTO blogpost_table VALUES ("t1", "p1");
    INSERT INTO blogpost_table VALUES ('t1', 'p3');
    INSERT INTO blogpost_table VALUES ("t1", "p4");
    INSERT INTO blogpost_table VALUES ('t1', 'p5');
    INSERT INTO blogpost_table VALUES ('t2', 'p2');
    INSERT INTO blogpost_table VALUES ('t2', 'p6');
    INSERT INTO blogpost_table VALUES ("t2", "p7");


    I'm launching SQL queries on the table in this topic.

    I want to write a SQL query that returns me the name of a topic (s) and the number of blog_post (s) associated with the topic in descending order of the number of blog posts created in July.

    Can someone please help me to write this query?

    Thank you

    Published by: user11994430 on October 9, 2009 07:24

    Thanks for the test of the configuration!

    SQL>SELECT   t.NAME, COUNT(*)
      2      FROM topic t, blogpost_table b, post_table p
      3     WHERE b.topic_id = t.topic_id
      4       AND p.post_id = b.post_id
      5       AND p.datepost >= DATE '2009-07-01'
      6       AND p.datepost < DATE '2009-08-01'
      7  GROUP BY t.NAME
      8  ORDER BY COUNT(*) desc;
    
    NAME                   COUNT(*)
    -------------------- ----------
    baseball                      2
    soccer                        2
    

    HTH, Urs

  • Help with a SQL query

    In the picture in the link below you can see a column called "retail date." As you can see there are two rows. I want to get is the difference between these two lines in another column... How can I do?
    http://img812.imageshack.us/img812/8237/captureivm.PNG

    Published by: Bernardo on 15/08/2012 11:50

    Dear Bernard,

    You must use the function analytic lag to select a value from a previous line.

    Example:

    with some_days as
    (
      select
        level           as id
      , sysdate + level as the_date
      from DUAL
      connect by level < 101
    )
    select
      id
    , the_date
    , lag(the_date) over (order by id) the_previous_date
    , the_date - lag(the_date) over (order by id) difference_in_days
    from some_days
    ;
    

    Concerning
    Marc

  • Please help build a sql query

    Hello

    Please help build a sql query


    My Table Test2015 has given below

    Header_id Line_id Ordered_item       

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887292 SH-POS-NO-BR POS-INS

    199926 723887293 MAIN2

    199927 723887294 IC-ENV-NON-BR-ENV-PXY

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199927 723887298 PRCSS SH-FAIRY-ELEC DISTR.

    199927 723887299 SH-FAIRY-SUM PRO-DE-CONS-HOUSE

    I am trying to query my Test2015 table to obtain the records with ordered_item containing 'MAIN1' and 'MAIN2' only. I tried to write a query as below

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1', 'MAIN2');

    But it gives me all the data with the MAIN2 records found but MAIN1 is absent, I want to retrieve only records to both 'MAIN1' and 'MAIN2' present for Header_id.

    While the result below shows me header_id - 199926 and 199929 that he should assume back. I want to fetch documents only with 'MAIN1' and 'MAIN2' both present.

    Header_id Line_id Ordered_item            

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887293 MAIN2

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199929 723887299 MAIN1

    Please suggest.

    Thank you and best regards,

    Prasad.

    Hello

    Try like this...

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1") and in header_id (select test2015 WHERE ORDERED_ITEM in ('MAIN2') header_id)

  • Failed to parse the SQL query: ORA-00918: column ambiguously defined

    I have an application that generates a report that says what indicator is connected to what size. For this, a package is created called and under this function is there that generates and returns a dynamic SQL strings that indicates which indicators depends on what size. In my report under the defination of region identification section, the type is mentioned the SQL (sql query return function body) and in the section source it returning



    Thanks in advance. Any suggestions on this will be useful for me to traceout the issue.

    Concerning
    ADI

    Published by: Adi on January 4, 2010 01:04
  • Help with pl/sql necessary

    I have a procedure like this with 2 settings and I need to change accrodning to the requirement.

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    _ THE PROCEDURE
    create or replace procedure proc is (p_low, p_high number) is
    file_handle utl_file.file_type;
    file_name VARCHAR2 (200);
    MyDir varchar2 (100);

    cursor c_rec
    is
    Select col1 |' | ' || col2 |' | ' || COL3 rec
    tab
    where low_high_value between p_low and p_high;

    Start

    MyDir: = "C:\Users\Desktop";

    if(p_low||) e -'|| p_high = 1-9') then
    file_name: = "FILE_1.txt";
    elsif(p_low||) e -'|| p_high = 10-19') then
    file_name: = "FILE_2.txt";
    on the other
    file_name: = "FILE_3.txt";
    end if;

    file_handle: = utl_file.fopen(mydir,file_name,'w');


    for l_rec in c_rec
    loop

    UTL_FILE.put_line (file_handle, l_rec. REC);

    end loop;

    UTL_FILE.fclose (file_handle);

    end;

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    From now when the user enters values for parameters 1 and 9, the output is written to a text file named FILE_1.txt in the folder fichier_2 for the input of 10 and 19 values and FILE_3 for all others.

    The new requirement is to write the output of p_low and p_high of 13 in a different file (probably in FILE_13.txt I want to add to the code). I can do simply, but when the user enters in the range of 10 and 19, which should exclude the 13 and write to FILE_2.txt.

    How can this be achieved other than the ref cursor?

    Published by: Nel Marcus Sep 19, 2011 13:23

    The case statement said if not p_low 13 and p_high is not 13 and the value of low_high_value in the "current" folder is 1 otherwise 0 13 back and back.

    So, if you spend 10 and 19 (or nothing at all, except 13 and 13) to your procedure then p_low! = 13 has the value true, p_high! = 13 will be true for all rows in the result set. For all rows in the result set where low_high_value is not 13, then the low_high_value = 13 will be false, so the case expression returns the value in the other part (1). Where low_high_value is 13, then low_high_value = 13 will be also true for the case expression returns the value of the part then (i.e. 0) so all lines except 13 will be returned.

    When you pass 13 and 13, then p_low! = 13 will be set to false if the value of the other part (1) will be returned.

    The predicate together checking if the expression box returns 1.

    John

  • I have the table of 3 columns A, B, C. I want to store the sum of columns A B in the C column without using the DML statements. Can anyone help please how to do. ?

    I have the table of 3 columns A, B, C. I want to store the sum of columns A B in the C column without using the DML statements. Can anyone help please how to do. ?

    11.1 and especially you have virtual column

    SQL> create table t
      2  (
      3     a number
      4   , b number
      5   , c generated always as (a+b) virtual
      6  );
    
    Table created.
    
    SQL> insert into t (a, b) values (1, 2);
    
    1 row created.
    
    SQL> select * from t;
    
             A          B          C
    ---------- ---------- ----------
             1          2          3
    

    Before that, a front insert - trigger

    SQL> create table t
      2  (
      3     a number
      4   , b number
      5   , c number
      6  );
    
    Table created.
    
    SQL> create or replace trigger t_default before insert on t for each row
      2  begin
      3    :new.c := :new.a+:new.b;
      4  end;
      5  /
    
    Trigger created.
    
    SQL> insert into t (a, b) values (1, 2);
    
    1 row created.
    
    SQL> select * from t;
    
             A          B          C
    ---------- ---------- ----------
             1          2          3
    
  • If I go to your stand at the NEC photographic show (21-24 March) can I expect to get personal help with using of items 13 - esp. vertical correction and the addition of sky to architects plans?

    If I go to your stand at the NEC photographic show (21-24 March) can I expect to get personal help with using of items 13 - esp. vertical correction and the addition of sky to architects plans?

    No, but Adobe offers a discount on the price of admission to the entire show. Adobe seminar room will be back this year yet, but are of demonstrations and public seminars. A number of speakers UK will be available, but the emphasis is on creative cloud, Lightroom and Photoshop CC.

    http://blogs.Adobe.com/richardcurtis/?p=4100

  • mixture of column type in the select statement

    Oracle 11g r2.

    tab_test (name varchar2, blob... photo)

    (1) select general use *.

    Stored procedure: open ref_cursor to some * of table_test...

    Coast of java: call.registerOutParameter (1, OracleTypes.CURSOR);  Call.Execute ();

    It works very well.

    (2) enter the name of the column in the select such as

    Stored procedure: open ref_cursor to select name, photo table_test...

    Coast of java: call.registerOutParameter (1, OracleTypes.CURSOR);  Call.Execute ();

    ERROR: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent data types: expected - got BLOB

    My question, if I specify the column names in the select statement, including the varchar2 and blob, how can I registerOutParameter (1, OracleTypes.CURSOR);?

    Thank you

    Scott

    PS The number of returned columns, in real case schenario, is a little, so I prefer to specify column names.

    Wrong forum. This forum is for questions of sql and pl/sql.

    Enter the question ANSWER and the repost in the JDBC forum

    https://community.oracle.com/community/developer/english/java/database_connectivity/java_database_connectivity _(jdbc)

    When repost you provide the necessary information to help you:

    1. information on the full version for Oracle DB

    2 version of the JDK

    3. the name and version of the JDBC jar file

    4. real code that demonstrates the problem

    You should also review the info re REF CURSOR in the JDBC Developer's Guide

    http://docs.Oracle.com/CD/E16655_01/Java.121/e17657/oraint.htm#i1058743

  • Use the CASE statement in a query of LOV

    Hello

    I wrote a select statement to be used in my list of values query, and it works fine when I run with SQL Developer. But when I put it in the LOV I get the LOV query is invalid error message. Here's the query I use:
    select 
        case when nt.COMMON_NAME is not null then nt.COMMON_NAME || ' (' || nt.TAXON_NAME || ')'
            else nt.TAXON_NAME
        end display_species
       ,case when nt.COMMON_NAME is not null then nt.COMMON_NAME || ' (' || nt.TAXON_NAME || ')'
            else nt.TAXON_NAME
        end return_species
    FROM NBN_TAXON nt
    WHERE lower(nt.INPUT_CATEGORY) = decode(lower(:P312_TAXON_GROUP_ADD), 'fish', 'fishes', lower(:P312_TAXON_GROUP_ADD))     
    order by 1;
    If the CASE statement is the source of the problem?

    Hello

    Try to remove the colon semi at the end.

    See you soon

    Ben

  • Help! My Mac Pro is displayed as being in the United States

    Can Hi anyone help with this strange problem?

    I live in the United Kingdom and attempted to place a bet earlier, but I got the message that I can't because I am located in the States.

    (Last time I looked at Kent was in the United Kingdom)

    Recently, the only thing that happened was I lost my Iphone. I got (thanks Apple) and enabled "Find my phone" while she tracks down. I noticed that reports I received were at time of the United States (PDT or something) so I changed the time zone in the United Kingdom.

    But why would it make my Mac will appear as if it were in the United States?

    Any suggestions will be greatly appreciated as I want to continue my habit (Paris)

    Thanks for reading this

    You have a Wi - Fi router?

    You use googleDNS or OpenDNS?

  • Help: How to use the Case statement in the ODI11g Interface?

    Hello
    My basic source I get 'Year' values and I want that these values result code in the interface and after translation want to push on the target system.
    Example:
    Database source, I get value for
    Year
    2010
    2011
    2012

    When I get the year 2010 I want to change the value in "FY10".
    When I get year 2011 I want to change the value in "FY11.
    and even for the year 2012 to "FY12.
    I've tried to make the Case statement, but had no success.
    I don't want to create the lookup table in the source system.
    Any help in this matter.

    Thank you

    Concerning
    Sher

    Published by: Sher Ullah Baig on August 26, 2012 17:52

    CASE
    WHEN source_column = '2010' THEN 'FY10.
    WHEN source_column = '2011' and THEN 'FY11.
    WHEN source_column = '2012' and THEN 'FY12.
    END

  • Please help me with this SQL query

    I'm practicing of SQL queries and met one involving the extraction of data from 3 different tables.

    The three paintings are as below

    < pre >
    Country
    Location_id country
    LOC1 Spain
    loc2 England
    LOC3 Spain
    loc4 USA
    loc5 Italy
    loc6 USA
    loc7 USA
    < / pre >
    < pre >


    User
    user_id location_id
    loc1 U1
    loc1 U2
    loc2 U3
    loc2 U4
    loc1 U5
    U6 loc3
    < / pre >
    < pre >


    Publish
    user_id post_id
    P1 u1
    P2 u1
    U2 P3
    P4 u3
    P5 u1
    P6 u2
    < / pre >

    I am trying to write a SQL query - for each country of the users, showing the average number of positions

    I understand the logic behind all this that we must first consolidate all locations, and then the users in one country and then find the way to their positions.
    But, I'm having a difficulty to this format SQL. Could someone help me please with this request.

    Thank you.

    Select
    Country.Country,
    Count (*) Totalpostspercountry,
    Count (distinct post.user_id) Totaldistincuserspercountry,
    count (*) / count (distinct post.user_id) Avgpostsperuserbycountry
    Of
    countries, have, post
    where country.location_id = muser.location_id
    and muser.user_id = post.user_id
    Country.country group

    The output is like this for your sample data - hope that's what you're looking for :)

    COUNTRY, TOTALPOSTSPERCOUNTRY, TOTALDISTINCUSERSPERCOUNTRY, AVGPOSTSPERUSERBYCOUNTRY
    In England, 1, 1, 1.
    Spain, 5, 2, 2.5.

  • Help required in SQL query

    I have a table and passes as below:

    START DATE TOTAL NUMBER OF DAYS
    11/10/2011 15:00 1
    15/10/2011-05:00 1
    2011-12-22 10:00 1
    22/12/2011 11:00 2
    30/12/2011-10:00 1
    01/01/2012 01:00 1
    01/01/2012 10:00 1
    01/01/2012 16:00 2
    02/01/2012 14:00 1
    03/01/2012-15:00 1
    04/01/2012-15:00 2
    04/01/2012 18:00 1



    I need to ask where the values in the table above
    must return values as below:

    I tried many ways, but not able to find the solution.

    Kindly help me in this regard.


    START DATE TOTAL NUMBER OF DAYS
    11/10/2011 15:00 1
    15/10/2011-05:00 1
    22/12/2011 11:00 2
    30/12/2011-10:00 1
    01/01/2012 16:00 2
    02/01/2012 14:00 1
    03/01/2012-15:00 1
    04/01/2012-15:00 2


    the ultimate goal is to summarize the TOTAL number of DAYS column all the numbers in the second table... All jobs must be treated in the same query. I can't use the coding of java to process what my requirement should be included in the query that already exists.

    Published by: 915175 on February 16, 2012 23:02
    select trunc(start_date), max(start_date), max(total_days) from tablename
    group by trunc(start_date);
    

    Please check below is on my local site with you Test data:

    SQL>
    SQL> with tablename as
      2  (
      3  select to_date('10/11/2011 15:00' , 'mm/dd/yyyy hh24:mi') start_date,  1 total_days from dual union all
      4  select to_date('10/15/2011 5:00' , 'mm/dd/yyyy hh24:mi') start_date,  1 total_days from dual union all
      5  select to_date('12/22/2011 10:00' , 'mm/dd/yyyy hh24:mi') start_date,  1 total_days from dual union all
      6  select to_date('12/22/2011 11:00' , 'mm/dd/yyyy hh24:mi') start_date,  2 total_days from dual union all
      7  select to_date('12/30/2011 10:00' , 'mm/dd/yyyy hh24:mi') start_date,  1 total_days from dual union all
      8  select to_date('1/1/2012 1:00' , 'mm/dd/yyyy hh24:mi') start_date,  1 total_days from dual union all
      9  select to_date('1/1/2012 10:00' , 'mm/dd/yyyy hh24:mi') start_date,  1 total_days from dual union all
     10  select to_date('1/1/2012 16:00' , 'mm/dd/yyyy hh24:mi') start_date,  2 total_days from dual union all
     11  select to_date('1/2/2012 14:00' , 'mm/dd/yyyy hh24:mi') start_date,  1 total_days from dual union all
     12  select to_date('1/3/2012 15:00' , 'mm/dd/yyyy hh24:mi') start_date,  1 total_days from dual union all
     13  select to_date('1/4/2012 15:00' , 'mm/dd/yyyy hh24:mi') start_date,  2 total_days from dual union all
     14  select to_date('1/4/2012 18:00' , 'mm/dd/yyyy hh24:mi') start_date,  1 total_days from dual
     15  )
     16  select  TO_CHAR(max(start_date),'MM/DD/YYYY HH24:MI') start_date , max(total_days) total_days from tablename
     17  group by trunc(start_date);
    
    START_DATE       TOTAL_DAYS
    ---------------- ----------
    10/15/2011 05:00          1
    01/03/2012 15:00          1
    12/30/2011 10:00          1
    01/01/2012 16:00          2
    10/11/2011 15:00          1
    12/22/2011 11:00          2
    01/04/2012 18:00          2
    01/02/2012 14:00          1
    
    8 rows selected.
    
    SQL>
    

    Kind regards
    Lifexisxnotxsoxbeautiful...

    Update: added test done to my database...

    Edited by: lifexisxnotxsoxbeautiful Vithalani, on February 16, 2012 23:18

Maybe you are looking for