Reg: Query writing - please help

Hello

can you please help me get the results below?

select * from emp_time

EMP_ID  SHIFT_START_TIME                      SHIFT_END_TIME                        YARD_DEPARTURE                                                EFFECTIVE_DATE
220541  03-12-08                              03-12-08                              y                                                              03-12-08
220541  04-12-08                              04-12-08                              y                                                              04-12-08
220541  18-02-09                              18-02-09                              y                                                              18-02-09
220541  20-02-09                              20-02-09                              y                                                              20-02-09
220541  24-02-09                              24-02-09                              y                                                              24-02-09
220541  25-02-09                              25-02-09                              y                                                              25-02-09
220541  26-02-09                              26-02-09                              y                                                              26-02-09
220541  27-02-09                              27-02-09                              y                                                              27-02-09
220541  28-02-09                              28-02-09                              y                                                              28-02-09
220541  28-03-09                              28-03-09                              y                                                              28-03-09

select * from emp_time
where effective_date between to_date('02-20-09','mm-dd-yy') and to_date('02-24-09','mm-dd-yy');

EMP_ID  SHIFT_START_TIME                      SHIFT_END_TIME                        YARD_DEPARTURE                                                EFFECTIVE_DATE
220541  20-02-09                              20-02-09                              y                                                              20-02-09
220541  24-02-09                              24-02-09                              y                                                              24-02-09

Expected output:

EMP_ID  SHIFT_START_TIME                      SHIFT_END_TIME                        YARD_DEPARTURE                                                EFFECTIVE_DATE
220541  20-02-09                              20-02-09                              y                                                              20-02-09
220541  20-02-09                              20-02-09                              y                                                              21-02-09
220541  20-02-09                              20-02-09                              y                                                              22-02-09
220541  20-02-09                              20-02-09                              y                                                              23-02-09
220541  24-02-09                              24-02-09                              y                                                              24-02-09


Oracle Version: 11.2.0

Kind regards

Jame

Let me make a try also, because your statement only a min and max on XX, efective_date whatever the emp_id ;-)

I pretend not, this one is better (too many results that must be filtered with a separate), but it should be emp id independent

with emp_exist as (select emp_id

shift_start_time

shift_end_time

yard_departure

effective_date

of emp_time)

emp_fake as (select emp_id separate

to_date('02-20-09','mm-dd-yy') shift_start_time

to_date('02-20-09','mm-dd-yy') shift_end_time

, 'y' yard_departure

, to_date (20 February 09 ',' dd-mm-aa "") + effective_date level 1

of emp_exist

connect by level<=>

Select emp_id nvl (t1.emp_id, t2.emp_id)

nvl (t1.shift_start_time, t2.shift_start_time) shift_start_time

nvl (t1.shift_end_time, t2.shift_end_time) shift_end_time

nvl (t1.yard_departure, t2.yard_departure) yard_departure

nvl (t1.effective_date, t2.effective_date) effective_date

of emp_exist t1

join right emp_fake t2

on (t1.emp_id = t2.emp_id

and t1.effective_date = t2.effective_date)

order effective_date;

Tags: Database

Similar Questions

  • Adding values to insert the query [was: Please help with this code]

    I created a comments section where there is that a single comment here field is the code for the form:

    < form id = "frmComment" name = "frmComment" method = "POST" action = "<?" PHP echo $editFormAction;? > ">"

    < h3 >

    < label for = "namegd" > < / label > comment: < / h3 >

    < p >

    < label for = 'comment2' > < / label >

    < textarea = "comment" id = "comment2' passes name ="60"lines ="10"> < / textarea >

    < /p >

    < p >

    < label for = "submit" >

    < / label >

    < input type = "submit" name = "submit" id = "submit" value = "Submit" / >

    < label for = "reset" > < / label >

    < input type = "reset" name = "reset" id = "reset" value = "Clear" / >

    < /p >

    < input type = "hidden" name = "MM_insert" value = "frmComment" / >

    < / make >

    and inclusion in the code applied to the shape is as follows

    $editFormAction = $_SERVER ['PHP_SELF'];

    If (isset {}

    $editFormAction. = « ? ». htmlentities($_SERVER['QUERY_STRING']);

    }


    If ((isset($_POST["MM_insert"])) & & ($_POST ["MM_insert"] == "frmComment")) {}

    $insertSQL = sprintf ("INSERT INTO comments ('comment',) VALUES (%s)", ")

    GetSQLValueString ($_POST ['comment'], "text"));


    @mysql_select_db ($database_my_connection, $my_connection);

    $Result1 = mysql_query ($insertSQL, $my_connection) or die (mysql_error ());

    }

    But I want the form to insert more than two values to the database one commented_by and the other will be post_id where = commented_by ($_SESSION ['Username']) and post_id = $row_Recordset1 ['id'] can someone please let me know what will be the modified code and there is commented_by is a text field and post_id is an int field.
    Please guys help me thanks in advance

    It is easy to add additional values to the Insert query:

    $insertSQL = sprintf("INSERT INTO comments (`comment`, commented_by, post_id) VALUES (%s, %s, %s)",
             GetSQLValueString($_POST['comment'], "text"),
             GetSQLValueString($_SESSION['Username'], "text"),
             GetSQLValueString($row_recordset1['id'], "int"));
    

    You must ensure that code to recordset1 comes before the insertion query. By default, Dreamweaver recordset place code immediately above the declaration DOCTYPE and under other server behavior. Thus, you must move the code. Otherwise, this insert query does not work.

  • I am writing please help :) CFScript

    I wonder if someone could help with a cfscript that I'm writing.

    Have a number for example

    135

    4245

    1

    I want to turn these into a string of 6 zeros at the beginning as follows:

    000135

    004245

    000001

    THX 4 any ideas

    Use numberformat()

    tmpNo = 523;

    NumberFormat(tmpNo,"000000");

  • Query Error - Please help

    The following query returns this error message: "ORA-00918: column ambiguously defined" it points to the last line of the query to the "color" before equal to...
    WITH  ranked_data  As
    (
         SELECT Members_Order.Catalogue_ID, Item.Item_Description, Item.Colour, Item.Hire_Charge, Members_Order.Number_of_Days, (Hire_Charge * Number_of_Days) AS Total, Item.Colour, RANK () OVER (ORDER BY Hire_Charge * Number_of_Days DESC) AS rnk
         FROM Members_Order, Item
         WHERE Members_Order.Item_ID = Item.Item_ID
    )
    SELECT * FROM ranked_data
    WHERE Colour  = (SELECT  Colour FROM ranked_data WHERE rnk = 1);
    Could someone help?

    Published by: user10945931 on April 16, 2009 10:54

    you have a column named color twice in your query. Delete one, or put an alias after one of them

  • I have some problem in my query to sql server. If I press f5 to run it opens to record dialogue area nd do not run (I don't get successful... orders etc). Please help me

    I have some problem in my query to sql server. If I press f5 to run it opens to record dialogue area nd do not run (I don't get successful... orders etc). Please help me

    Hello

    SQL Server is not supported in these forums. I suggest that you ask your question again in one of the forums dedicated to Microsoft for him here:

    http://social.technet.microsoft.com/Forums/en-us/category/sqlserver .

    Thank you.   :)

  • Please help me find the solution for the query

    Hi Experts,

    Please help build a sql query. Thank you

    Examples of data

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

    create the table Material_tb

    (

    Detail varchar2 (20).

    Description varchar2 (200)

    )

    /

    Start

    Insert into material_tb values('Color','Red,Blue,Black,Green,White');

    Insert into material_tb values ('Material','Gold, Silver, Platinum');

    end;

    /

    Select * from material_tb;

    DETAIL DESCRIPTION

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

    Color red, blue, black, green, white

    Material gold, silver, Platinum

    I want that output voltage

    DETAIL DESCRIPTION

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

    Red color

    Blue color

    Black color

    Green color

    White color

    Material gold

    Silver material

    Platinum material

    You can try under sql

    select distinct detail,regexp_substr(description,'[^,]+',1,LEVEL)
     from material_tb
    connect by regexp_substr(description,'[^,]+',1,LEVEL) is not null
    order by 1
    
  • 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)

  • Ask help - please help me get the expected defined in a query result

    Dear all,

    Help with your suggestions and advice. Please see the bottom of the screenshots for the planned result sets.

    create table shift_dt 
    (name varchar2(20),
    shift_start date,
    shift_end date);
    
    insert into shift_dt values ('Brian',to_date('01-01-2015 09:10:00','dd-mm-yyyy hh24:mi:ss'), to_date('01-01-2015 22:10:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('02-01-2015 09:10:00','dd-mm-yyyy hh24:mi:ss'), to_date('04-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('04-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('05-01-2015 15:20:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('06-01-2015 10:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('06-01-2015 23:20:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('07-01-2015 11:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('09-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('09-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('10-01-2015 22:00:00','dd-mm-yyyy hh24:mi:ss'));
    
    select * from shift_dt;
    
    
    

    Select * from shift_dt;

    1.jpg

    Expected result set: -.

    1.jpg

    I compare date of shift_end with the next date of shift_start of the day. If the dates made 12 am (IE 00:00:00), then I ignore and display the next shift end date available as displayed in the screen game result. Please see the screen set outcome turned for more details.

    Please help get the results you want in the SQL query

    Kind regards

    Souls

    Hello

    Here's one way:

    WITH got_new_grp AS

    (

    SELECT name, shift_start, shift_end

    CASE

    WHEN shift_start = TRUNC (shift_start)

    AND shift_start = LAG (shift_end) OVER (ORDER BY shift_start)

    THEN 0

    1. OTHER

    END AS new_grp

    OF shift_dt

    )

    got_grp AS

    (

    SELECT name, shift_start, shift_end

    SUM (new_grp) OVER (ORDER BY shift_start) AS grp

    OF got_new_grp

    )

    SELECT MIN (name) DUNGEON (DENSE_RANK FIRST ORDER BY shift_start)

    As a name

    MIN (shift_start) AS shift_start

    MAX (shift_end) AS shift_end

    OF got_grp

    GROUP BY grp

    ORDER BY shift_start

    ;

    Output (from the full sample data):

    NAME SHIFT_START SHIFT_END

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

    01/01/2015 Brian 09:10 01/01/2015 22:10

    Brian 02/01/2015 09:10 01/05/2015 15:20

    Brian 06/01/2015 10:00 01/06/2015 23:20

    Brian 07/01/2015 11:00 10/01/2015 22:00

    Brian 01/02/2015 15:00 02/02/2015 00:00

    Brian 02/02/2015 11:00 02/02/2015 17:00

    Brian 03/02/2015 00:00 03/02/2015 08:00

    Brian 02/05/2015 15:30 06/02/2015 07:30

    Brian 07/02/2015 23:30 10/02/2015 00:30

    I guess that shift_start is unique.

    It's finally a GROUP BY problem: we want to show the first shift_start and the last shfit_end of a group of 1 or more lines.  The tricky part is to identify the groups.  If I understand the problem, line X is grouped with the previous line X-1 if shift_start on the X line is midnight and is equal to shift_end on line X-1.  The above query uses the LAG analytic to see if each line begins a new group or not and the analytic function SUM to see how many groups have already begun (and therefore, which group each line entry belongs.)

    I still don't understand why you 'Corrected name of Anne in Brian' and not vice versa.  I guess the name to display in each group is the name of the first row in the Group (i.e. the line with the shift_start earlier).

  • I want to loop through the data from two different tables using for loop where the query should be replaced at runtime, please help me

    I have the data into two table with the structure of similar column, I want to loop through the data in these two tables

    based on some condition and runtime that I want to put the query in loop for example, the example is given, please help me

    create table ab (a number, b varchar2 (20));

    Insert into ab

    Select rownum, rownum. "" sample "

    of the double

    connect by level < = 10

    create table bc (a number, b varchar2 (20));

    Insert into BC.

    Select rownum + 1, rownum + 1 | "" sample "

    of the double

    connect by level < = 10

    declare

    l_statement varchar2 (2000);

    Boolean bool;

    Start

    bool: = true;

    If it is true, then

    l_statement: =' select * ab ';

    on the other

    l_statement: =' select * from bc';

    end if

    I'm in execute immediate l_statement - something like that, but I don't know

    loop

    dbms_output.put_line (i.a);

    end loop;

    end;

    Something like that, but this isn't a peace of the code work.

    Try this and adapt according to your needs:

    declare

    l_statement varchar2 (2000);

    c SYS_REFCURSOR;

    l_a number;

    l_b varchar2 (20);

    Boolean bool;

    Start

    bool: = true;

    If it is true, then

    l_statement: = "select a, b, AB;

    on the other

    l_statement: = "select a, b from bc;

    end if;

    --

    Open c for l_statement;

    --

    loop

    extract the c in l_a, l_b;

    When the output c % notfound;

    dbms_output.put_line (l_a |') -' || l_b);

    end loop;

    close c;

    end;

    /

  • Please, help me to query SQL Construct.


    Hi Experts,

    Could you please help me to query SQL Construct.  Please find the details

    HOSTNAME HOSTTYPE DEM R1 R2
    RS123 P ABC 24.5 265,5

    RS123 P CYC 24.5 265,5

    RS123 P ADDS 24.5 265,5

    RS123 P ADE 24.5 265,5

    RS123 P SRC 24.5 265,5

    EXPECTED RESULTS

    HOSTNAME      HOSTTYPE                              MNE                                                                      R1          R2
    RS123 P ABC, CYC, ADD, ADE, CBC 24.5 265,5

    Concerning

    See you soon

    with t as)

    Select "RS123' hostname 'P' hosttype, 'ABC' dem, 24.5 r1, r2 265,5 Union double all the

    Select 'RS123', 'P', "CYC", 24.5, 265,5 double Union all

    Select 'RS123', 'P', 'ADD', 24.5, 265,5 double Union all

    Select "RS123', 'P', 'ADE', 24.5, 265,5 double Union all

    Select 'RS123', 'P', 'SRC', 24.5, 265,5 double

    )

    Select the host name,

    HostType,

    RTrim (XMLAGG (XmlElement(e,MNE,','). (Extract ('//Text ()')), ',') DEM,.

    R1,

    R2

    t

    Group hostname,

    HostType,

    R1,

    R2

    /

    HOSTN H DEM R1 R2
    ----- - -------------------- ---------- ----------
    RS123 P ABC, CBC, ADE, ADD, CYC 24.5 265,5

    SQL >

    SY.

  • Query XML giving NULL result - please help!

    Hello

    I am a newbie in XML and I try to extract the XMLType column information.
    I am using Oracle 11g

    I use the table is

    CREATE TABLE 'PRODUCTS '.
    (
    'ID' VARCHAR2 (10 BYTE),
    "CUSTDOC" 'SYS '. "" XMLTYPE.
    )
    _________________________________________________________________

    I am the slot loading XML file in the CUSTDOC column

    < report xmlns = "http://developer.cognos.com/schemas/report/6.0/."
    expressionLocale = "en - to the" >
    [< modelPath > / content/folder[@name='Packages']/folder[@name='Prod']/package[@name='ORGS']/model[@name='model'] < / modelPath >
    < drillBehavior modelBasedDrillThru = "true" / >
    < query >
    < application name = "Query1" >
    < source >
    < model / >
    < / source >
    < selection >
    < name of dataItem = aggregate 'Incident ID' = 'none' rollupAggregate = 'none' >
    < expression > [ABC]. [XYZ]. [Incident] < / expression >
    < / dataItem >
    < / selection >
    < detailFilters >
    < detailFilter >
    < filterExpression > [ABC]. [XYZ]. [Company] = "SOCIÉTÉTEST" < / filterExpression >
    < / detailFilter >
    < / detailFilters >
    < / query >
    < / queries >
    < layouts >
    < layout >
    < reportPages >
    < name = 'Page1' page >
    < style >
    < defaultStyles >
    < defaultStyle = "pg" refStyle / >
    < / defaultStyles >
    < / style >
    < pageBody >
    < style >
    < defaultStyles >
    < defaultStyle = "pb" refStyle / >
    < / defaultStyles >
    < / style >
    < content >
    < refQuery list = "Query1" horizontalPagination = "true" name = "List1" >
    < style >
    < defaultStyles >
    < defaultStyle = 'ls' refStyle / >
    < / defaultStyles >
    < value CSS = "border-collapse: collapse" / >
    < / style >
    < more >
    < listColumn >
    < listColumnTitle >
    < style >
    < defaultStyles >
    < defaultStyle = 'lt' refStyle / >
    < / defaultStyles >
    < / style >
    < content >
    < textItem >
    < dataSource >
    < dataItemLabel refDataItem = "The Incident ID" / >
    < / dataSource >
    < / textItem >
    < / Summary >
    < / listColumnTitle >
    < listColumnBody >
    < style >
    < defaultStyles >
    < defaultStyle = 'lc' refStyle / >
    < / defaultStyles >
    < / style >
    < content >
    < textItem >
    < dataSource >
    < dataItemValue refDataItem = "The Incident ID" / >
    < / dataSource >
    < / textItem >
    < / Summary >
    < / listColumnBody >
    < / listColumn >
    < / more >
    < / list >
    < / Summary >
    < / pageBody >
    < Entetepage >
    < content >
    < block >
    < style >
    < defaultStyles >
    < defaultStyle = 'ta' refStyle / >
    < / defaultStyles >
    < / style >
    < content >
    < textItem >
    < style >
    < defaultStyles >
    < defaultStyle = "tt" refStyle / >
    < / defaultStyles >
    < / style >
    < dataSource >
    < staticValue / >
    < / dataSource >
    < / textItem >
    < / Summary >
    < / block >
    < / Summary >
    < style >
    < defaultStyles >
    < defaultStyle = "ph" refStyle / >
    < / defaultStyles >
    < value = CSS "padding-bottom: 10px" / >
    < / style >
    < / pageHeader >
    < pageFooter >
    < content >
    < table >
    < tableRows >
    < tableRow >
    < tableCells >
    < tableCell >
    < content >
    < date >
    < style >
    < dataFormat >
    < dateFormat / >
    < / dataFormat >
    < / style >
    < / date >
    < / Summary >
    < style >
    < value="vertical-align:top;text-align:left;width:25%"/ CSS >
    < / style >
    < / tableCell >
    < tableCell >
    < content >
    < pageNumber / >
    < / Summary >
    < style >
    < value="vertical-align:top;text-align:center;width:50%"/ CSS >
    < / style >
    < / tableCell >
    < tableCell >
    < content >
    < time >
    < style >
    < dataFormat >
    < timeFormat / >
    < / dataFormat >
    < / style >
    < / time >
    < / Summary >
    < style >
    < value="vertical-align:top;text-align:right;width:25%"/ CSS >
    < / style >
    < / tableCell >
    < / tableCells >
    < / tableRow >
    < / TableRow >
    < style >
    < defaultStyles >
    < defaultStyle = "CT" refStyle / >
    < / defaultStyles >
    < value="border-collapse:collapse;width:100%"/ CSS >
    < / style >
    < /table >
    < / Summary >
    < style >
    < defaultStyles >
    < defaultStyle = "pf" refStyle / >
    < / defaultStyles >
    < value = CSS "padding-top: 10px" / >
    < / style >
    < / pageFooter >
    < / print this page >
    < / reportPages >
    < / page layout >
    < / page layout >
    < XMLAttributes >
    < name XMLAttribute = "RS_CreateExtendedDataItems" value = "true" output = "no" / >
    < name XMLAttribute = "listSeparator' value = ',' output ="no"/ >
    < / XMLAttributes >
    < / report >

    ____________________________________________________________________________________

    I run the query below

    SELECT SYS. XMLTYPE. GETCLOBVAL (SYS. XMLTYPE. Extract ("CUSTDOC",'/ Report/Queries/Query/Selection/DataItem/expression / Text () ')) 'CUSTDOC' OF 'PRODUCT '.

    and I await the result below

    CUSTDOC
    ---
    [ABC]. [XYZ]. [Incident]

    but I am NOTHING.

    Please help me. Don't know what I'm doing wrong here.

    Kind regards
    Ravi

    Hi Ravi,

    Don't know what I'm doing wrong here.

    Two things:

    -EXTRACT function is deprecated--> use XMLQuery or XMLTable rather
    -You are missing the namespace declaration

    See if it works for you:

    SQL> SELECT XMLCast(
      2           XMLQuery(
      3             'declare default element namespace "http://developer.cognos.com/schemas/report/6.0/"; (: :)
      4              /report/queries/query/selection/dataItem/expression'
      5             passing t.custdoc
      6             returning content
      7           )
      8           as varchar2(30)
      9         ) as expr
     10  FROM product t ;
    
    EXPR
    ------------------------------
    [ABC].[XYZ].[Incident]
     
    
  • SQL QUERY PLEASE HELP

    My requirement is to calculate a few things like total_cash_amount, TOTAL_CO_PATIENT_AMOUNT etc... For this we will use BILLING. PATIENTBILL, BILLING. PATIENTBILLDETAILS, BILLING. Operating tables. Here it is already developed procedure.
    I'm askd to change the same for best performance. This is the part of it that is calculation of total_cash_amount. There will be two categories such as INPATIENT (IP) and OUTPATIENT (OP). Can anyone of you please help me to change the code in a manner better. And can you please suggest that global temporary tables are better or can we use the tables directly.
    INSERT INTO GTT_PATIENTBILL
    SELECT *
      FROM BILLING.PATIENTBILL P
     WHERE P.LOCATIONID = IN_LOCATIONID
       AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND TRUNC(IN_FROMDATE);
    INSERT INTO GTT_PATIENTBILLDETAILS   
    SELECT PB.*
      FROM BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
     WHERE P.LOCATIONID = IN_LOCATIONID
       AND P.BILLNO = PB.BILLNO
       AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND TRUNC(IN_FROMDATE);
    INSERT INTO GTT_TRANSACTION
    SELECT *
      FROM BILLING.TRANSACTION T
     WHERE T.LOCATIONID = IN_LOCATIONID
       AND TRUNC(T.CREATEDDATE) BETWEEN TRUNC(IN_YEARDATE) AND
           TRUNC(IN_FROMDATE);
    
    DELETE FROM TEMP_1;
    INSERT INTO TEMP_1
    
      SELECT (CASE
               WHEN PARTICULARS IS NULL AND FLG = 1 THEN
                'TOTAL COLLECTIONS'
               WHEN PARTICULARS IS NULL AND FLG = 2 THEN
                'TOTAL DEDUCTIONS'
               ELSE
                PARTICULARS
             END) PARTICULARS,
             SUM(A) "FOR THE DAY",
             SUM(B) "MONTH TO DATE",
             SUM(C) "YEAR TO DATE"
        FROM (SELECT PARTICULARS,
                     SUM(TOTAL_CASH_AMOUNT_DAY) A,
                     SUM(TOTAL_CASH_AMOUNT_MONTH) B,
                     SUM(TOTAL_CASH_AMOUNT_YEAR) C,
                     1 FLG
                FROM (SELECT 'OP CASH' PARTICULARS,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_DAY,
                             0 TOTAL_CASH_AMOUNT_MONTH,
                             0 TOTAL_CASH_AMOUNT_YEAR
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 2
                         AND TRUNC(P.BILLDATE) =TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'OP CASH' PARTICULARS,
                             0,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_MONTH,
                             0
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 2
                         AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_MONTHDATE) AND
                             TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'OP CASH' PARTICULARS,
                             0,
                             0,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_YEAR
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 2
                         AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND
                            TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'IP CASH' PARTICULARS,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_DAY,
                             0 TOTAL_CASH_AMOUNT_MONTH,
                             0 TOTAL_CASH_AMOUNT_YEAR
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 3
                         AND TRUNC(P.BILLDATE) = TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'IP CASH' PARTICULARS,
                             0,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_MONTH,
                             0
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 3
                         AND TRUNC(P.BILLDATE)  BETWEEN TRUNC(IN_MONTHDATE) AND
                             TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'IP CASH' PARTICULARS,
                             0,
                             0,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_YEAR
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 3
                         AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND
                             TRUNC(IN_FROMDATE)) T1
               GROUP BY PARTICULARS
               
    Thanks in advance...

    You can do this in the same query. As long as the underlying table is the same thing that you don't have to have multiple queries to the same table. You just improvise on your selection fields. Use a CASE statement instead of DECIDING that I used and include the other INDICATIONS which I didn't understand.

  • Please help on a query

    Hello

    We have two tables test_a and test_b and would like to get the result of this query:
    COL1 VAL_A VAL_B
    ------------------------------ ------------------------------ -------------
    code_1 aa_1 aa_1
    code_1 aa_2 aa_2
    code_1 aa_3
    code_2 bb_1
    code_2 bb_2


    Could someone please help on this, thanks a lot!


    create table test_a)
    col1 varchar2 (30),
    col2 varchar2 (30));

    insert into values test_a ('code_1', 'aa_1');
    insert into values test_a ('code_1', 'aa_2');
    insert into values test_a ('code_1', 'aa_3');
    insert into test_a values ('code_2', 'bb_1');
    commit;

    create table test_b)
    col1 varchar2 (30),
    col2 varchar2 (30));

    insert into test_b values ('code_1', 'aa_1');
    insert into test_b values ('code_1', 'aa_2');
    insert into test_b values ('code_2', 'bb_2');
    commit;

    Select * from test_a;

    COL1 COL2
    ------------------------------ ------------------------------
    code_1 aa_1
    code_1 aa_2
    code_1 aa_3
    code_2 bb_1

    Select * from test_b;

    COL1 COL2
    ------------------------------ ------------------------------
    code_1 aa_1
    code_1 aa_2
    code_2 bb_2

    Published by: user489948 on January 5, 2010 10:13

    Published by: user489948 on January 5, 2010 10:15
  • need help with query-please help

    Hi all
    I want to convert the query to a view online below. I write examples of data that I used
    create table pde(empno number(10),act_hour varchar(10 byte));
     
    Table created
     
    insert into pde values(211,'12:20');
    insert into pde values(211,'06:00');
    insert into pde values(213,'07:00');
    insert into pde values(213,'07:20');
     
    WITH  got_total_hours     AS
    (
         SELECT SUM (   TO_NUMBER (SUBSTR (act_hour, 1, 2))
                    + ( TO_NUMBER (SUBSTR (act_hour, 4, 2))
                      / 60
                      )
                    ) AS total_hours  
         from      pde
     
    )
    SELECT        TO_CHAR (FLOOR (total_hours))
           || ':'
           || TO_CHAR ( MOD (total_hours, 1) * 60
                  , 'fm00'
                   )     AS hh_mm,empno,act_hour
    FROM     got_total_hours ,pde
     
    this is the output i am getting  
     
    HH_MM  EMPNO  ACT_HOUR
    32:40     211     12:20
    32:40     211     06:00
    32:40     213     07:00
    32:40     213     07:20
    
    i tried changing the above query to an inline view given below
    SELECT        TO_CHAR (FLOOR (total_hours))
           || ':'
           || TO_CHAR ( MOD (total_hours, 1) * 60
                  , 'fm00'
                   )     AS hh_mm,act_hour from(SELECT SUM (   TO_NUMBER (SUBSTR (act_hour, 1, 2))
                    + ( TO_NUMBER (SUBSTR (act_hour, 4, 2))
                      / 60
                      )
                    ) AS total_hours  
         from      pde)
    
    ORA-00904:''ACT_HOUR":invalid identifier
    Please help me
    thanking in advance
    Kind regards
    oracleuser

    Published by: makdutakdu on January 5, 2010 11:41

    Just push "with the clause" within the main request and give an alias of the table for him... something like

     SELECT TO_CHAR (FLOOR (total_hours))
      || ':'
      || TO_CHAR ( MOD (total_hours, 1) * 60 , 'fm00' ) AS hh_mm,
      empno                                                     ,
      act_hour
       FROM
      (SELECT SUM ( TO_NUMBER (SUBSTR (act_hour, 1, 2)) + ( TO_NUMBER (SUBSTR (act_hour, 4, 2)) / 60 ) ) AS total_hours
         FROM pde
      ) got_total_hours ,
      pde
    

    Ravi Kumar

  • Please help me write this SQL query...

    Hi everyone,
    
    Please help me in this query.
    A patient can multiple types of Adresses (types P,M,D).If they have all the 3 types i need to select type: p and
    if they have (M and D) i need to select type M,and if they have only type D i have to select that.
    For each address i need to validate whether that particular address is valid or not (by start date and end date and valid flag)
    
    Patient table
    =============
    
    Patient_id          First_name    last_name
    
    1                   sanjay        kumar
    2                   ajay          singh
    3                   Mike          John
    
    
    Adress table
    ============
    
    address_id       patient_id       adresss       city       type       startdate        enddate      valid_flg
    
    1                   1             6222         dsadsa           P          01/01/2007       01/01/2010
    2                   1             63333        dsad             M          01/02/2006       01/01/2007      N
    3                   1             64564         fdf              M          01/01/2008       07/01/2009      
    4                   1             654757       fsdfsa          D          01/02/2008       09/10/2009  
    5                   2             fsdfsd       fsdfsd            M          01/03/2007       09/10/2009   
    6                   2             jhkjk        dsad              D          01/01/2007       10/10/2010   
    7                   3             asfd         sfds               D          01/02/2008       10/10/2009      
    
    
    output
    =====
    
    1        sanjay       kumar            6222       dsadsa      P          01/01/2007        01/01/2010     
    2        ajay         singh            fsdfsd     fsdfsd       M          01/03/2007        09/10/2009 
    3        mike         john              asfd       sfds        D          01/02/2008        10/10/2009
    Thanks in advance
    Phani

    Hello, Fabienne,.

    This race for you (twisted code of Sarma):

    SELECT patient_id, first_name, last_name, address, city, type, startdate, enddate
     FROM (
      SELECT a.patient_id patient_id, first_name, last_name, address, city, type, startdate, enddate,
                 ROW_NUMBER() OVER (PARTITION BY p.patient_id ORDER BY CASE type WHEN 'P' THEN 1
                                                                          WHEN  'M' THEN 2
                                                                          WHEN  'D' THEN 3
                                                                        END) rn
        FROM  patient p
        JOIN  address a ON (p.patient_id = a.patient_id )
       WHERE NVL(valid_flg, 'X') != 'N'
         AND SYSDATE BETWEEN startdate AND  NVL(enddate, SYSDATE)
         )
    WHERE rn = 1; 
    

    Edit, currently in the trial:

    With Patient AS (
    SELECT 1  Patient_id , 'sanjay' First_name, 'kumar'  last_name FROM DUAL UNION ALL
    SELECT 2, 'ajay', 'singh' FROM DUAL UNION ALL
    SELECT 3, 'Mike', 'John' FROM DUAL),
    Address AS (
    SELECT 1   address_id, 1  patient_id, '6222'    address, 'dsadsa'   city, 'P'  type, to_date('01/01/2007', 'DD/MM/YYYY')  startdate, to_date('01/01/2010', 'DD/MM/YYYY')  enddate, NULL  valid_flg FROM DUAL UNION ALL
    SELECT 2,1,'63333','dsad','M', to_date('01/02/2006', 'DD/MM/YYYY'), to_date('01/01/2007', 'DD/MM/YYYY'),  ' N'  FROM DUAL UNION ALL
    SELECT 3,1,'64564','fdf','M', to_date('01/01/2008', 'DD/MM/YYYY'), to_date('07/01/2009', 'DD/MM/YYYY'), NULL  FROM DUAL UNION ALL
    SELECT 4,1,'654757','fsdfsa','D', to_date('01/02/2008', 'DD/MM/YYYY'), to_date('09/10/2009', 'DD/MM/YYYY'),  NULL  FROM DUAL UNION ALL
    SELECT 5,2,'fsdfsd ','fsdfsd','M', to_date('01/03/2007', 'DD/MM/YYYY'), to_date('09/10/2009', 'DD/MM/YYYY'), NULL  FROM DUAL UNION ALL
    SELECT 6,2,' jhkjk','dsad','D', to_date('01/01/2007', 'DD/MM/YYYY'), to_date('10/10/2010', 'DD/MM/YYYY'),  NULL  FROM DUAL UNION ALL
    SELECT 7,3,'asfd',' sfds',' D', to_date('01/02/2008', 'DD/MM/YYYY'), to_date('10/10/2009', 'DD/MM/YYYY'),  NULL  FROM DUAL)
    -- end test data
     SELECT patient_id, first_name, last_name, address, city, type, startdate, enddate
     FROM (
      SELECT a.patient_id patient_id, first_name, last_name, address, city, type, startdate, enddate,
                 ROW_NUMBER() OVER (PARTITION BY p.patient_id ORDER BY CASE type WHEN 'P' THEN 1
                                                                          WHEN  'M' THEN 2
                                                                          WHEN  'D' THEN 3
                                                                        END) rn
        FROM  patient p
        JOIN  address a ON (p.patient_id = a.patient_id )
       WHERE NVL(valid_flg, 'X') != 'N'
         AND SYSDATE BETWEEN startdate AND  NVL(enddate, SYSDATE)
         )
    WHERE rn = 1; 
    
    PATIENT_ID FIRST_ LAST_ ADDRESS CITY   TY STARTDATE ENDDATE
    ---------- ------ ----- ------- ------ -- --------- ---------
             1 sanjay kumar 6222    dsadsa P  01-JAN-07 01-JAN-10
             2 ajay   singh fsdfsd  fsdfsd M  01-MAR-07 09-OCT-09
             3 Mike   John  asfd     sfds  D 01-FEB-08 10-OCT-09
    

Maybe you are looking for

  • ASP tutorial

    Hello Is there a tutorial on how to write/build an ASP project, from start to finish, with Measurement Studio? Curt

  • OEM Windows XP Professional installation, bought on Ebay shows not authentic

    Hi, I recently bought a so-called installation OEM Windows XP Professional genuine United Nations Open unused on Ebay to $107.50. But when it was installed it always tells me it's not a real, so what I do now and it will cost me more money? * origina

  • Anyone who is familiar with "Satellite Direct TV"?

    Wondering if anyone knows "Satellite Direct TV" and if it is safe to download on his computer?

  • GlassFish does not have the result of the action

    My glassfish does not have the result of the action, maybe the adfc-config not the welcome-file project, because from the moment where I redirect on a bean for the workflow, it starts to run the result, perhaps the answer to this problem either, like

  • Impossible to reconnect to a virtual machine running

    HelloI removed decadent a virtual machine running in the inventory.The machine virtual is critical and cannot be stopped.When I try to browse the data store the option "add to the inventory" is grayed out.How can I reconnect it to the inventory?Thank