Howto create 'select statement' that returns first row? (simple table)

quick question that drives me crazy:
Say I have the following table:
ID....car_model....point_A....total
1........333.............NY..........54
2........333.............NJ..........42
3........333.............NH...........63
4........555.............NJ...........34
5........555.............PA...........55
I would like to create a select statement that return car_model which the starting point is NJ - in this example it's only 555.
thanks for any tips

fair enough.
the problem is this: given the table below, I need to create a report that reflects car rentals from specific location. you can rent a car from different locations; a car has a starting point (like a flight itinerary) so consider this:
Mark rent a car with the following itinerary:
NY--> NJ
NJ--> PA
PA-->FL
FL-->LA
the end user would like to see all car that were rented between X and Y and start point was NJ, so in the example above, the starting point is NY so it doesn't match the end users' criteria.
The table is organized in the following manner: ID....car_model....point_A....total
* I don't know whey the someone choose point_A as a column description as it just suppose to be 'location'
so, back to my first example:
ID....car_model....point_A....total
1........333.............NY..........54
2........333.............NJ..........42
3........333.............NH...........63
4........555.............NJ...........34
5........555.............PA...........55
if I do this:
Select car_model from myTable where point_A='NJ'
the return result will be 333 and 555 but that is in correct because 333 starting point is NY.
I hope I provided enough information, thanks

Similar Messages

  • How to create a function that returns multiple rows in table

    Dear all,
    I want to create a funtion that returns multiple rows from the table (ex: gl_balances). I done following:
    -- Create type (successfull)
    Create or replace type tp_gl_balance as Object
    PERIOD_NAME VARCHAR2(15),
    CURRENCY_CODE VARCHAR2(15),
    PERIOD_TYPE VARCHAR2(15),
    PERIOD_YEAR NUMBER(15),
    BEGIN_BALANCE_DR NUMBER,
    BEGIN_BALANCE_CR NUMBER
    -- successfull
    create type tp_tbl_gl_balance as table of tp_gl_balance;
    but i create a function for return some rows from gl_balances, i can't compile it
    create or replace function f_gl_balance(p_period varchar2) return tp_tbl_gl_balance pipelined
    as
    begin
    return
    (select gb.period_name, gb.currency_code, gb.period_type, gb.period_year, gb.begin_balance_dr, gb.begin_balance_cr
    from gl_balances gb
    where gb.period_name = p_period);
    end;
    I also try
    create or replace function f_gl_balance(p_period varchar2) return tp_tbl_gl_balance pipelined
    as
    begin
    select gb.period_name, gb.currency_code, gb.period_type, gb.period_year, gb.begin_balance_dr, gb.begin_balance_cr
    from gl_balances gb
    where gb.period_name = p_period;
    return;
    end;
    Please help me solve this function.
    thanks and best reguard

    hi,
    Use TABLE FUNCTIONS,
    [http://www.oracle-base.com/articles/9i/PipelinedTableFunctions9i.php]
    Regards,
    Danish

  • 64-bit driver only returns first row of table

    I have a C++ application using ADO (the MSADO COM components) and the Oracle OLEDB provider for database access. The application works fine on a 32-bit computer using the 32-bit Oracle client and driver. However, when I run a 64-bit build of the application, running on a 64-bit computer (Windows Server 2008 x64), using the 64-bit Oracle client and driver, a SELECT operation returns only the first row of the table.
    Note that this is only happening with the ActiveX ADO components. ADO.NET is not having a problem.
    In both cases I am connecting to the same database, which is Oracle 10.2 (32-bit) running on a different server. I have tested with Oracle client 10.2.0.4 and 11.1.0.7 and got the same result in both cases.
    I have reproduced the issue with a simple table (one column, NVARCHAR2(255)) and simple code.
    In the code, I execute "select count(*) from tablename" and get the correct record count (more than one record). But when I then open the recordset ("select columnname from tablename"), ADO reports EndOfFile after I have read the first row and called MoveNext on the recordset.
    My Oracle knowledge is limited so I don't know if there are driver properties I should be checking.
    Anyone have ideas?
    Thanks in advance.

    For 10.2 it's fixed in 10204 Patch 21 and higher.
    For 11.1 it's fixed in 11107 Patch 12 and higher.
    Cheers,
    Greg

  • Return first row entered based on date column

    I'm trying to select the first entered row in a table, as judged by the datetime column. If more than one row has the same date and time, then only one row should be returned (any row having that datetime is fine). Some processing will occur on that row and then it will be deleted. The select statement is used thereafter to select the next (first) entered row in the table, etc. This way, the rows are processed first-in first-out (FIFO) style. Here's my example table:
    create table my_table
    datetime date,
    firstname varchar2(50)
    insert into my_table(datetime, firstname) values(to_date('2012-04-02 11:00:00', 'YYYY-MM-DD HH24:MI:SS'),'ken');
    insert into my_table(datetime, firstname) values(to_date('2012-04-02 11:00:00', 'YYYY-MM-DD HH24:MI:SS'),'john');
    insert into my_table(datetime, firstname) values(to_date('2012-04-02 11:00:00', 'YYYY-MM-DD HH24:MI:SS'),'sue');
    commit;
    Here's my example select statement, which returns simply one row of the above, since all are the same date and time:
    SELECT *
    FROM my_table
    WHERE datetime = ( select min(datetime) from my_table )
    AND rownum = 1;
    My question is, if I use the following
    SELECT *
    FROM my_table
    WHERE datetime = ( select min(datetime) from my_table );
    It returns all 3 rows:
    DATETIME FIRSTNAME
    02-APR-12 11:00:00 ken
    02-APR-12 11:00:00 john
    02-APR-12 11:00:00 sue
    So, wouldn't setting rownum = 2 return john, and rownum = 3 return sue? For example,
    SELECT *
    FROM my_table
    WHERE datetime = ( select min(datetime) from my_table )
    AND rownum = 2;
    return no rows. I just want to make sure I'm understanding how the select statement above works. It seems to work fine for returning one row having the minimum date and time. If this is always the case, then everything is fine. But I wouldn't have expected it not to return one of the other rows when rownum is 2 or 3, which makes me question why? Maybe I can learn something here. Any comments much appreciated.
    Edited by: tem on Apr 2, 2012 2:06 PM

    Hi,
    tem wrote:
    ... So, wouldn't setting rownum = 2 return john, and rownum = 3 return sue? For example,, ROWNUM
    SELECT *
    FROM my_table
    WHERE datetime = ( select min(datetime) from my_table )
    AND rownum = 2;
    return no rows. I just want to make sure I'm understanding how the select statement above works. It seems to work fine for returning one row having the minimum date and time. If this is always the case, then everything is fine. But I wouldn't have expected it not to return one of the other rows when rownum is 2 or 3, which makes me question why? Maybe I can learn something here. Any comments much appreciated.ROWNUM is assigned as rows are fetched and considered for inclusion in the result set. If the row is not chosen for any reason, the same ROWNUM will be reused with the next row fetched. ROWNUM=2 will not be assigned until a row with ROWNUM=1 has been included in hte result set.
    So, in your example:
    SELECT  *
    FROM    my_table
    WHERE   datetime = ( select min(datetime) from my_table )
    AND     rownum = 2;Say the first row that happens to be fetched has firstname='ken'. It is assigned ROWNUM=1, and fails the WHERE clause condition "WHERE rownum = 2".
    Say the next row fetched has firstname='john'. ROWNUM=1 hasn't been used yet, so this row is also assigned ROWNUM=1, and it fails the WHERE clause for the same reason. Likewise with the next row; it also is assigned ROWNUM=1, and it also fails.
    When using ROWNUM in a WHERE clause, you almost always want to say "ROWNUM = 1" or "ROWNUM <= n".
    You could also use the analytic ROW_NUMBER function:
    WITH     got_r_num     AS
         SELECT     datetime, firstname
         ,     ROW_NUMBER () OVER (ORDER BY  datetime)     AS r_num
         FROM     my_table
    SELECT     datetime, firstname
    FROM     got_r_num
    WHERE     r_num     = 1
    ;Here, all values of r_num are available, so it would make sense to say things like "WHERE r_num = 2" or "WHERE r_num >= 2".
    Edited by: Frank Kulash on Apr 2, 2012 5:31 PM
    Added to explanation.

  • HOW TO EXECUTE A STORE PROCEDURE THAT RETURN MULTIPLE ROWS FROM A QUERY

    I NEED TO CREATE AND USE A STORE PROCEDURE THAT IS GOING TO DO A SELECT STATEMENT AN THE RESULT OF THE SELECT STATEMENT IS RETURN IN SOME WAY TO THE REQUESTER.
    THIS CALL IS MADE BY AN EXTERNAL LANGUAGE, NOT PL/SQL OR FORMS APPLICATION. USING FOR EXAMPLE ODBC AND VISUAL BASIC. WHAT I NEED TO DO IS ADD A DATA ACCESS LAYER TO MY APPLICATION AND I ALREADY HAVE IT DONE FOR MS SQL SERVER, BUT I NEED THE SAME FUNCTIONALITY ACCESSING AN ORACLE DATABASE.
    FLOW:
    1. VB CREATE A ODBC CONNECTION TO A ORACLE DATABASE
    2. VB EXECUTE A STORE PROCEDURE
    3. THE STORE PROCEDURE RETURNS TO THE VB APPLICATION THE RESULT OF THE QUERY THAT IS INSIDE OF THE STORE PROCEDURE.(I.E. THE STORE PROCEDURE IS A BASIC SELECT, NOTHING COMPLEX)
    4. VB DISPLAY THE RESULT IN A GRID
    FOR SURE I CAN DO THE SELECT DIRECTLY TO ORACLE, BUT FOR PERFORMANCE REASONS AND SCALABILITY, I'LL LIKE IT TO DO IT USING A STORE PROCUDURES
    IS THIS POSIBLE?, HOW?
    THANKS

    Certainly, it's possible. First, define a stored procedure that includes an OUT parameter which is a REF CURSOR. Then, call the stored procedure via ODBC omitting the OUT parameter from the list of parameters. IT will automatically be returned as a result set. Syntax for both is below...
    CREATE PROCEDURE foo (inParam in varchar2, resultSet OUT REF CURSOR )
    In ODBC:
    {call foo( 'someData' )}
    Justin

  • First row of table is displayed as selected always when application loads.

    Hi..
    I have a table with few rows in it.When the application loads the first row of table is displayed as selected always.I don't want any of the rows to be displayed as selected.
    How can I prevent this ?
    My jdev version 11.1.1.5.0
    Edited by: Lovin_JV_941794 on Mar 12, 2013 2:13 AM

    Hi,
    can you remove the attribute
    selectedRowKeys="#{bindings.XXXX.collectionModel.selectedRow}"This would ensure that there are no selected rows persisted. you can also paste the jspx code also for better answer.
    ~Abhijit

  • I want a select statement to return two values, sum of one column and customer number

    I have two columns one called invoice_number and the other invoice_amount. I want a select statement to return two columns.... invoice_number and then the sum of the invoice_amount(s) for each unique invoice number.
    SELECT sum(invoice_amount) AS Totalinvoice_amount FROM InvoiceTB where invoice_number = 'INV102'
    This is where I've started, which returns:
    Totalinvoice_amount
    500.00
    Any help is appreciated.
    Please mark my post as helpful or the answer or better yet.... both! :) Thanks!

    Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
    use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 
    This is minimal polite behavior on SQL forums. 
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Need help with a SQL qurey that returns multiple rows for one record?

    I have the following query where I use a CASE WHEN clause to determine the date of a shift that begins with "FRLO" on day1 - day14 of the pay period. It works great if a schedule record contains one day that begins "FRLO", but if more than one day is "FRLO" then it only returns the first day it finds and not the others. Is there some way to get the query to return a ron for every day 1 - 14 that begins "FRLO"? System if Oracle 11G
    Order of the results is not important as this is part of a larger query that orders the results.
    Thanks in advance for any help,
    George
    SELECT s.empid,
    CASE
    WHEN UPPER (SUBSTR (s.Day1, 0, 4)) = 'FRLO'
    THEN
    pp.startpp
    WHEN UPPER (SUBSTR (s.Day2, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 1
    WHEN UPPER (SUBSTR (s.Day3, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 2
    WHEN UPPER (SUBSTR (s.Day4, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 3
    WHEN UPPER (SUBSTR (s.Day5, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 4
    WHEN UPPER (SUBSTR (s.Day6, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 5
    WHEN UPPER (SUBSTR (s.Day7, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 6
    WHEN UPPER (SUBSTR (s.Day8, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 7
    WHEN UPPER (SUBSTR (s.Day9, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 8
    WHEN UPPER (SUBSTR (s.Day10, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 9
    WHEN UPPER (SUBSTR (s.Day11, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 10
    WHEN UPPER (SUBSTR (s.Day12, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 11
    WHEN UPPER (SUBSTR (s.Day13, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 12
    WHEN UPPER (SUBSTR (s.Day14, 0, 4)) = 'FRLO'
    THEN
    pp.startpp + 13
    END
    startdate,
    NULL starttime,
    NULL endtime,
    8 hours,
    0 minutes
    FROM schedules s
    JOIN
    payperiods pp
    ON pp.periodid = s.periodid
    WHERE UPPER (SUBSTR (s.Day1, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day2, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day3, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day4, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day5, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day6, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day7, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day8, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day9, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day10, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day11, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day12, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day13, 0, 4)) = 'FRLO'
    OR UPPER (SUBSTR (s.Day14, 0, 4)) = 'FRLO';
    CURRENT OUTPUT
    EMPID STARTDATE STARTTIME ENDTIME HOURS MINUTES
    753738, 3/25/2013 , , ,8 ,0
    753740, 3/25/2013 , , ,8 ,0
    753748, 3/25/2013 , , ,8 ,0
    DESIRED OUTPUT
    EMPID STARTDATE STARTTIME ENDTIME HOURS MINUTES
    753738, 3/25/2013 , , ,8 ,0
    753740, 3/25/2013 , , ,8 ,0
    753748, 3/25/2013 , , ,8 ,0
    753738, 3/26/2013 , , ,8 ,0
    753740, 3/26/2013 , , ,8 ,0
    753740, 3/28/2013 , , ,8 ,0
    753748, 1/1/2013 , , ,8 ,0
    753738, 4/3/2013 , , ,8 ,0
    753748, 4/3/2013 , , ,8 ,0
    CREATE TABLE SCHEDULES
    SCHEDULEID NUMBER(12) NOT NULL,
    EMPID NUMBER(12) NOT NULL,
    PERIODID VARCHAR2(6 BYTE) NOT NULL,
    AREAID NUMBER(12) NOT NULL,
    DAY1 VARCHAR2(50 BYTE),
    DAY2 VARCHAR2(50 BYTE),
    DAY3 VARCHAR2(50 BYTE),
    DAY4 VARCHAR2(50 BYTE),
    DAY5 VARCHAR2(50 BYTE),
    DAY6 VARCHAR2(50 BYTE),
    DAY7 VARCHAR2(50 BYTE),
    DAY8 VARCHAR2(50 BYTE),
    DAY9 VARCHAR2(50 BYTE),
    DAY10 VARCHAR2(50 BYTE),
    DAY11 VARCHAR2(50 BYTE),
    DAY12 VARCHAR2(50 BYTE),
    DAY13 VARCHAR2(50 BYTE),
    DAY14 VARCHAR2(50 BYTE),
    NOPTIND1 INTEGER DEFAULT 0,
    NOPTIND2 INTEGER DEFAULT 0,
    NOPTIND3 INTEGER DEFAULT 0,
    NOPTIND4 INTEGER DEFAULT 0,
    NOPTIND5 INTEGER DEFAULT 0,
    NOPTIND6 INTEGER DEFAULT 0,
    NOPTIND7 INTEGER DEFAULT 0,
    NOPTIND8 INTEGER DEFAULT 0,
    NOPTIND9 INTEGER DEFAULT 0,
    NOPTIND10 INTEGER DEFAULT 0,
    NOPTIND11 INTEGER DEFAULT 0,
    NOPTIND12 INTEGER DEFAULT 0,
    NOPTIND13 INTEGER DEFAULT 0,
    NOPTIND14 INTEGER DEFAULT 0
    CREATE TABLE PAYPERIODS
    PERIODID VARCHAR2(6 BYTE) NOT NULL,
    STARTPP DATE,
    ENDPP DATE
    Insert into SCHEDULES
    (SCHEDULEID, EMPID, PERIODID, AREAID, DAY1,
    DAY2, DAY3, DAY4, DAY5, DAY6,
    DAY7, DAY8, DAY9, DAY10, DAY11,
    DAY12, DAY13, DAY14, NOPTIND1, NOPTIND2,
    NOPTIND3, NOPTIND4, NOPTIND5, NOPTIND6, NOPTIND7,
    NOPTIND8, NOPTIND9, NOPTIND10, NOPTIND11, NOPTIND12,
    NOPTIND13, NOPTIND14)
    Values
    (3693744, 753738, '082013', 2167, 'X',
    'FRLO<1530>', 'FRLO<1530>', '1530', '1530', '1530',
    'X', 'X', '1530', '1530', 'FRLO',
    '1530', '1530', 'X', 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0);
    Insert into SCHEDULES
    (SCHEDULEID, EMPID, PERIODID, AREAID, DAY1,
    DAY2, DAY3, DAY4, DAY5, DAY6,
    DAY7, DAY8, DAY9, DAY10, DAY11,
    DAY12, DAY13, DAY14, NOPTIND1, NOPTIND2,
    NOPTIND3, NOPTIND4, NOPTIND5, NOPTIND6, NOPTIND7,
    NOPTIND8, NOPTIND9, NOPTIND10, NOPTIND11, NOPTIND12,
    NOPTIND13, NOPTIND14)
    Values
    (3693745, 753740, '082013', 2167, 'X',
    'FRLO<1530>', 'FRLO<1530>', '1530', 'FRLO', '1530',
    'X', 'X', '1530', '1530', '1530',
    '1530', '1530', 'X', 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0);
    Insert into SCHEDULES
    (SCHEDULEID, EMPID, PERIODID, AREAID, DAY1,
    DAY2, DAY3, DAY4, DAY5, DAY6,
    DAY7, DAY8, DAY9, DAY10, DAY11,
    DAY12, DAY13, DAY14, NOPTIND1, NOPTIND2,
    NOPTIND3, NOPTIND4, NOPTIND5, NOPTIND6, NOPTIND7,
    NOPTIND8, NOPTIND9, NOPTIND10, NOPTIND11, NOPTIND12,
    NOPTIND13, NOPTIND14)
    Values
    (3693746, 753748, '082013', 2167, 'X',
    'FRLO<1530>', '1530', '1530', '1530', '1530',
    'X', 'X', 'FRLO<1530>', '1530', 'FRLO',
    '1530', '1530', 'X', 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0);
    COMMIT;
    Insert into PAYPERIODS
    (PERIODID, STARTPP)
    Values
    ('082013', TO_DATE('03/24/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    COMMIT;

    Do you have the opportunity to change the data model to have one day per row ? It would make this easier to get this result without the need for a 14-way CASE or UNION.
    If not...
    The case statement will return as soon as it matches one of the conditions. Since you want a match when any column in the row starts with FRLO you can use a UNION ALL treating each column as a separate result. There may be more efficient ways to do this, but here is one way:
    Select S.Empid,       Pp.Startpp Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day1, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+1 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day2, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+2 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day3, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+3 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day4, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+4 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day5, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+5 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day6, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+6 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day7, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+7 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day8, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+8 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day9, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+9 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day10, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+10 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day11, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+11 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day12, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+12 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day13, 0, 4)) = 'FRLO'
    Union All 
    Select S.Empid,       Pp.Startpp+13 Startdate,       Null Starttime,       Null Endtime,       8 Hours,       0 Minutes
      From Schedules S       Join  Payperiods Pp On Pp.Periodid = S.Periodid
      Where Upper (Substr (S.Day14, 0, 4)) = 'FRLO'

  • Select function that returns multiple columns.

    Currently I have a function that will return 1 column and I can use that in a select statement. What I need is to return multiple columns from a table. In the function I have I return the street address from a table that holds information about dealers, I need to be able to also return the columns that contain the city, state, and zip code so it can be used in an sql select statement. How would I do this?
    My function:
    FUNCTION GET_ADDRESS(dealer_id IN number)
    RETURN tbl_dealer_info.c_street%TYPE AS
    v_rc tbl_dealer_info.c_street%TYPE;
    CURSOR get_dealer_cur IS
    SELECT c_street
    FROM tbl_dealer_info
    WHERE n_dealer_id = dealer_id;
    BEGIN
    v_rc := NULL;
    OPEN get_dealer_cur;
    FETCH get_dealer_cur INTO v_rc;
    IF get_dealer_cur%NOTFOUND THEN
    NULL;
    END IF;
    CLOSE get_dealer_cur;
    RETURN v_rc;
    EXCEPTION
    WHEN OTHERS THEN
    RETURN NULL;
    END GET_ADDRESS;
    My select statement:
    select GET_ADDRESS(1205) Street, DI.n_inactive_flag, DI.n_onhold_flag
    from tbl_dealer_info DI
    where DI.n_dealer_id = 1205;
    I would like to be able to select the street, city, state, and zip all in this select statement from the GET_ADDRESS function.
    Thanks,
    Lori Neirynck

    "The reality is you've probably already put your blinders on and you won't learn the best approach to solving your problem because you insist on asking a courtroom style question (just "yes" or "no" please)."
    Actually, I asked this question because I was looking for the best approach to my problem. I have an SQL statement that correctly selects everything it needs to from all 15 tables. The thing of it is it is very long and difficult to read. I wanted to try using functions so that I could change 12 AND/OR statements into 3 IF statements so everything is easier to read. I have received a couple of different ways to do this from other forums that assumed I knew what I was doing. I have gotten one to work, the other I'm still working on. I'll post the one that worked for others that want to know.
    SQL> insert into dealer_info values (1,'Me','13 success street', 'Wonder Town','Wonderland','1313');
    SQL> commit;
    SQL> create type t_address as object (
    2 street varchar2(100),
    3 city varchar2(30),
    4 state varchar2(30),
    5 zip varchar2(10));
    6 /
    Type created.
    SQL> create or replace function get_address (p_id number) return t_address
    2 is
    3 ret t_address := t_address(null,null,null,null);
    4 rec dealer_info%rowtype;
    5 begin
    6 select * into rec from dealer_info where id=p_id;
    7 ret.street := rec.street;
    8 ret.city := rec.city;
    9 ret.state := rec.state;
    10 ret.zip := rec.zip;
    11 return ret;
    12 end;
    13 /
    Function created.
    SQL> col name format a10
    SQL> col address format a70
    SQL> select name, get_address(id) address from dealer_info;
    NAME ADDRESS(STREET, CITY, STATE, ZIP)
    Me T_ADDRESS('13 success street', 'Wonder Town', 'Wonderland', '1313')
    1 row selected.
    -Lori

  • Need a function that return multiple rows

    Hi,
    I need a function that should return output of this query
    SELECT b.branding_code, c.name_desc
    FROM
    development.brandings b, godot.company c
    WHERE b.company_id = c.company_id;
    This above function return 30 rows and I am not giving any input
    Function using cursor,pipeline
    Please help
    Edited by: 1008783 on Jun 6, 2013 6:40 AM

    Hi,
    1008783 wrote:
    Hi
    Table A:
    Development schema
    branding_code,company_id
    Table B:
    Gogot schema
    name_desc,...Those aren't CREATE TABLE and INSERT statements. Post something that the people who want to help you can run on their own systems to re-create the problem and test their ideas.
    o/p:
    branding_code,name_desc
    I should return these column as o/p, no i/pA query will do that. There's no need for a function.
    There are good reasons for wanting a functin. If you have one, say what it is. Explain how you plan to use the function.
    Have you tried writing a function? Post your code. What specific problems did you have?

  • Select statement not returning correct data

    I'm working on an application and I'm having a weird problem. The application connects to an Oracle database, pulls out some data, and does something with it. In the process of testing I've already read and dealt with some rows. On those rows the UPDATE_FLAG field was changed to a N so they wouldn't be selected again.
    I do want to select those rows again, so with SQLPlus I changed the UPDATE_FLAG back to Y. Now here's the strange part. The same select statement run from SQLPlus is returning thoe row I modified, but my application does not.
    Here's the select statement:
    SELECT * FROM MY_TABLE WHERE UPDATE_FLAG = 'Y' and USER_ID like 'B%';
    When I run this I get a row back called Brian.
    In my application I do this:
    try
                   Class.forName("oracle.jdbc.driver.OracleDriver");               
                   Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@server:port:sid", "username", "password");
                   Statement st = conn.createStatement();
                   ResultSet rec = st.executeQuery("SELECT * FROM MY_TABLE WHERE UPDATE_FLAG = 'Y' and USER_ID like 'B%'");
                   System.out.println("SELECT * FROM MY_TABLE WHERE UPDATE_FLAG = 'Y' and USER_ID like 'B%'");
                   while(rec.next())
                        System.out.println(rec.getString("USER_ID"));
                   st.close();
              } catch (Exception e)
                   System.out.println("Error -- " + e.toString());
                   e.printStackTrace();
                }When this runs it doesn't return Brian and there are no exceptions caught. It's like it does not find it.
    Assuming I explained this well enough, anyone have any ideas what the problem could be?
    Thanks,
    James

    I do want to select those rows again, so with SQLPlus I changed the UPDATE_FLAG back to Y. Last time I ran SQLPlus I explicitly had to do a commit after modifications, because it was creating a transaction around the sqlplus session. Of course running a query in that session would work on the modified data rather than the actual data.

  • 'select count(*) from x' returns 5460 rows and 'Select * from x' returns 0 rows

    As you can see in the next lines something is wrong in my Oracle (8.0.6 on Win NT 4.0 sp 6a) :
    Oracle8 Enterprise Edition Release 8.0.6.0.0 - Production
    With the Partitioning option
    PL/SQL Release 8.0.6.0.0 - Production
    SQLWKS> SELECT * FROM V_TERRA_TE;
    PERIOD_DATE PERIOD_TIME TERRARCV TERRASND TERCV TESND
    0 rows selected.
    SQLWKS> SELECT COUNT(*) FROM V_TERRA_TE;
    COUNT(*)
    5460
    1 row selected.
    V_TERRA_TE is a complex join of 5 Tables :
    CREATE OR REPLACE VIEW "ACTUATE".V_TERRA_TE AS Select to_date(to_char(p1.period_date,'YYYYMMDD'),'YYYYMMDD') "PERIOD_DATE",
    to_date(to_char(p1.period_time,'HH24:MI'),'HH24:MI') "PERIOD_TIME",
    to_number(p1.caudalrcv + ((p3.caudalrcv + p4.caudalrcv)*(confterrate.conexadslterra/confterrate.conexadsltotal))) "TERRARCV",
    to_number(p1.caudalsnd + ((p3.caudalsnd + p4.caudalsnd)*(confterrate.conexadslterra/confterrate.conexadsltotal))) "TERRASND",
    to_number((p2.caudalrcv * confterrate.pctinfonegocio) + ((p3.caudalrcv + p4.caudalrcv)*(confterrate.conexadslte/confterrate.conexadsltotal)))"TERCV",
    to_number((p2.caudalsnd * confterrate.pctinfonegocio) + ((p3.caudalsnd + p4.caudalsnd)*(confterrate.conexadslte/confterrate.conexadsltotal)))"TESND"
    from p1,p2,p3,p4,confterrate
    where (p1.period_date=p2.period_date)and
    (p1.period_date=p3.period_date)and
    (p1.period_date=p4.period_date)and
    (p1.period_time=p3.period_time)and
    (p1.period_time=p4.period_time)and
    (p1.period_time=p2.period_time)and
    to_char(p1.period_date,'MMYYYY')=to_char(confterrate.period_datetime,'MMYYYY');
    I think that some not reported error happens in the select * with some temporary space or similar but only the message '0 rows selected' is displayed (instead the real error)
    Could somebody help me ?
    Thanks in advance
    Francisco

    Forcing the Join/sort to be made on Disk (not on memory) the problem not happens. This demostrate that ORACLE has a VERY IMPORTANT BUG : It returns 0 rows wich is false.
    To force it to work on disk i use this parameters :
    alter session set sort_area_size=0
    alter session set hash_join_enabled=false
    Note : probably is not the best combination or use of parameters, but using it the query works as espected.

  • Select for update returns no rows even though there is no locking thread

    I'm using Ibatis library over oracle sql for my query. The select for update statement returns no rows. This happens intermittently. When this was happening last time, I executed the select statement on sqldeveloper (but without the 'for update') and got rows. This situation is not easily reproducible so I've not yet been able to ascertain whether rows are returned on sqldeveloper with the 'for update' clause. But I know for sure that there was no other thread locking the rows. How could this be happening?

    The select for update statement returns no rowsWhy do you think that a select for update will always return rows?
    the for update clause if there not to garantee the presence of rows but to lock the row when it is present
    sql> select * from t;
             A          B C
             1          1 step1
             2          2 step2
             3          3 step3Then session 1 issues the following select
    SELECT     *
          FROM t
         WHERE a = 1
    FOR UPDATE NOWAIT;If session 2 issues the same select before session 1 commits or rolls back
    SELECT     *
          FROM t
         WHERE a = 1
    FOR UPDATE NOWAIT;It will get the following error
    ERROR at line 1:
    ORA-00054: resource busy and acquire with NOWAIT specifiedBut if session 2 issue such a kind of select
    sql> SELECT     *
      2        FROM t
      3       WHERE a = 99
      4  FOR UPDATE NOWAIT;
    no rows selectedYou see then that a select for update can return no rows
    Best Regards
    Mohamed Houri

  • SELECT statement not returning NULL records

    I have the following SELECT statement:
    SELECT *
    FROM RPT_DS1_CNT_CAT
    WHERE DUPS_SAME <> 'N/A';
    I also tried:
    SELECT *
    FROM RPT_DS1_CNT_CAT
    WHERE DUPS_SAME != 'N/A';
    Same results - There ARE Null DUPS_SAME values
    which are not being selected. I get NO results.
    When I execute this, I do get NULL values.
    SELECT *
    FROM RPT_DS1_CNT_CAT
    WHERE DUPS_SAME IS NULL;
    How can I ensure that I get all non - 'N/A' records
    if it won't pick up the nulls? Is there a function
    I can use or a different wording? I do want to use
    != or <> because there may be other values besides
    'N/A' such as 'YES' or 'NO' and they may be null or
    filled with spaces.

    The expression
    NVL(DUPS_SAME,'') is meaningless. You're saying "If DUPS_SAME is NULL, return the empty string, which is NULL, otherwise return DUPS_SAME." If you're going to use NVL, the string that gets returned if the column is NULL should be non-NULL, i.e.
    NVL(DUPS_SAME,'DUPS_SAME was NULL') Your query should, as others have pointed out, either be
    SELECT *
      FROM rpt_ds1_cnt_cat
    WHERE dups_same <> 'N/A' OR dups_same IS NULLor
    SELECT *
      FROM rpt_ds1_cnt_cat
    WHERE NVL(dups_same, 'DUPS_SAME is NULL') <> 'N/A' Either of these return the same results as your query but they're going to be far clearer for whoever needs to maintain the code. There may be performance differences as well if DUPS_SAME is indexed depending on the data distribution...
    Justin

  • Hi, how to write select  statement to get first 10 maximum salaries

    hi,
    i need to select first 10 maximum salaries from the emp table which is having say 100 records

    SELECT *
    FROM (SELECT * FROM my_table ORDER BY salary
    ry DESC)
    WHERE ROWNUM < 10;your query will return 9 rows:
    SQL> select * from (select * from emp order by sal desc)
      2  where rownum < 10;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7839 KING       PRESIDENT            17-NOV-81       5000                    10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
          7566 JONES      MANAGER         7839 02-APR-81       2975                    20
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
    9 rows selected.
    Now if 2 employee has same salary, both should come in the 10 maximum salary list.
    try this:
    [pre]
    SQL> select empno, salary
      2  from (select empno, sal salary , rank() over (order by sal desc) rank from emp)
      3  where rank <= 10 ;
         EMPNO     SALARY
          7839       5000
          7788       3000
          7902       3000
          7566       2975
          7698       2850
          7782       2450
          7499       1600
          7844       1500
          7934       1300
          7521       1250
          7654       1250
    11 rows selected.look at the above query, I queried for first 10, but it returned 11 rows because empno 7521 and 7654 both should rank 10.

Maybe you are looking for