Help with query - Conversion of the value of the line to the value of the column for the same column

CREATE TABLE TESTY
(
  BATCH_ID  VARCHAR2(100 BYTE),
  LINE_NUM  NUMBER,
  STRING    VARCHAR2(100 BYTE),
  AMOUNT    NUMBER
)
/
Insert into testy
   (BATCH_ID, LINE_NUM, STRING, AMOUNT)
 Values
   ('DBA', 1, '101-201', 100)
/

Insert into testy
   (BATCH_ID, LINE_NUM, STRING, AMOUNT)
 Values
   ('DBA', 1, '101-202', 200)
/

Insert into testy
   (BATCH_ID, LINE_NUM, STRING, AMOUNT)
 Values
   ('DEVELOPER', 2, '101-201', 110)

/
Insert into testy
   (BATCH_ID, LINE_NUM, STRING, AMOUNT)
 Values
   ('DEVELOPER', 2, '101-202', 210)
/

Insert into testy
   (BATCH_ID, LINE_NUM, STRING, AMOUNT)
 Values
   ('DEVELOPER', 2, '101-203', 310)
/
COMMIT
/

BATCH_ID     LINE_NUM     STRING     AMOUNT
DBA          1          101-201     100
DBA          1          101-202     200
DEVELOPER     2          101-201     110
DEVELOPER     2          101-202     210
DEVELOPER     2          101-203     310

Required Output 

BATCH_ID     LINE_NUM  AMOUNT1 AMOUNT2
DBA          1         100     200
DEVELOPER    2         110     210
Only need to this result for string IN (101-201', 101-202').

Can anyone help. ?

Thank you

Hello

In Oracle 8.1 or more

SELECT       batch_id, line_num
,       SUM (CASE WHEN string = '101-201' THEN amount END)     AS amount1
,       SUM (CASE WHEN string = '101-202' THEN amount END)     AS amount2
FROM       testy
GROUP BY  batch_id, line_num
ORDER BY  batch_id, line_num
;

From Oracle 11.1, you can also use SELECT... PIVOT.
For more information, see the FAQ forum {message identifier: = 9360005}

Tags: Database

Similar Questions

  • Need help with query to sort the Digital Alpha channels.

    Hello
    I have data like A2-1 A2-2 A2-3 A2-4 A4-S 10 - A2 A2-22 A5 - 15 16 A5 A5-1 has A5-1 b
    I want the data in a sequence sorted as A2-1 A2-2 A2 - 3 4 - A2 A2 10 A2-22 S A4 A5-1 has A5-1B A5-15 A5-16

    How to get there. Help, please.

    Perhaps that would help you

    with data as
        (
           select 'A2-1' LOCATION_DESC  from dual union all
           select 'A2-2'  as col1 from dual union all
           select 'A2-3'  as col1 from dual union all
           select 'A2-4'  as col1 from dual union all
           select 'A4-S'  as col1 from dual union all
           select 'A2-10' as col1 from dual union all
           select 'A2-22' as col1 from dual union all
          select 'A5-15' as col1 from dual union all
          select 'A5-16' as col1 from dual union all
          select 'A5-1A' as col1 from dual union all
          select 'A5-1B' as col1 from dual
      )
    SELECT * FROM data
    order by SUBSTR(LOCATION_DESC,2,INSTR(LOCATION_DESC,'-')-2);
    

    * 009 *.

  • Help with query Oracle?

    Help with query Oracle?

    Take a look at the query

    Select channel_desc, sum (unit_cost), AVG (Unit_price) of channels C, cost S

    where C.channel_id = S.channel_id

    Group 1,2,3



    the first time the error is "channel_desc not a group by expression"-desc is a varchar type there are 6 channels

    II remove the group by clause and then pops up an error "" not only - group expression to group "".

    All the ideas!

    Select channel_desc, sum (unit_cost), AVG (Unit_price) of channels C, cost S
    where C.channel_id = S.channel_id
    Channel_desc group

  • Need help with query SQL Inline views + Group

    Hello gurus,

    I would really appreciate your time and effort on this application. I have the following data set.

    Reference_No---Check_Number---Check_Date---description---Invoice_Number---Invoice_Type---Paid_Amount---Vendor_Number
    1234567 11223 - 05/07/2008 -paid for cleaning- 44345563-I-* 20.00 *---19
    1234567 11223 - 05/07/2008 - 44345563 -a--10,00---19 ofbad quality adjustment
    7654321 11223 - 05/07/2008 - setting the last billing cycle - 23543556 - A - 50.00 - 19
    4653456 11223 - 05/07/2008 - paid for cleaning - 35654765 - I - 30, 00-19

    Please ignore '-' added for clarity

    I'm writing a paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, aggregate query Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Type, Invoice_Number, Vendor_Number. When there are no more records I want to display the respective Description.

    The query should return the following data set

    Reference_No---Check_Number---Check_Date---description---Invoice_Number---Invoice_Type---Paid_Amount---Vendor_Number
    1234567 11223 - 05/07/2008 -paid for cleaning- 44345563-I-* 10.00 *---19
    7654321 11223 - 05/07/2008 - setting the last billing cycle - 23543556 - A - 50.00 - 19
    4653456 11223 - 05/07/2008 - paid for cleaning - 35654765 - I - 30, 00-19
    Here's my query. I'm a little lost.

    Select b., A.sequence_id, A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
    de)
    Select sequence_id, check_number, check_date, invoice_number, sum (paid_amount) sum, vendor_number
    of the INVOICE
    Sequence_id group check_date, check_number, invoice_number, vendor_number
    ) A, B OF INVOICE
    where A.sequence_id = B.sequence_id


    Thank you
    Nick

    It seems that this is a duplicate thread - correct me if I am wrong in this case->

    Need help with query SQL Inline views + Group

    Kind regards.

    LOULOU.

  • Need help with query outputing group names

    I'm trying to find a way for groups of output headers, then all the records in each group etc header. It would be easy, except there is a key with what I want to do.

    Normally, if I have this data set (that I've ' borrowed' a site that showed the closest to what I was looking for):

    Example table:

    TABLE [number]

    (Name, NUMBER)

    Dave Bower 843-444-4444

    Dave Bower 843-555-5555

    Matthew Small 843-111-1111

    Matthew Small _843-222-2222

    Matthew Small 843-333-3333

    I could use the following code:

    < cfoutput query = "somequery" group = "name" >

    #name # < br >

    < cfoutput >

    #phonenumber # < br >

    < / cfoutput >

    < hr >

    < / cfoutput >

    And get this:

    Dave Bower

    843-444-4444.

    843-555-5555.

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

    Matthew Small

    843-111-1111.

    843-222-2222.

    843-333-3333.

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

    BUT my actual tables are not set up like that. Rather than recording of every name of every record, I would have an ID that is the foreign key to another table.

    Current table set up is as follows:

    TABLE [people]

    (ID, NAME)

    1 Dave Bower

    2 small Matthew

    TABLE [Phones]

    (PEOPLE_ID NUMBER)

    1 843-444-4444

    1 843-555-5555

    2 843-111-1111

    2 843-222-2222

    2 843-333-3333

    If this output actually would this give me with my current setup and request above code:

    1

    843-444-4444.

    843-555-5555.

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

    2

    843-111-1111.

    843-222-2222.

    843-333-3333.

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

    How can I keep my current setup but create a query that produces the same result from the top? (The table names of people like the group headers, but data from the phones table under that output)

    You must gather the two tables, and then group the output.

    Something along the lines of the (may vary slightly depending on your DB and the exact table structure)

    SELECT ppl.name, ph.number

    PEOPLE ppl

    INNER JOIN phones ph ON ppl.id = ph.people_id

    ORDER BY ppl.name

    See you soon

    Kai

  • When you add pictures to the Vista image folder it creates a folder HIDDEN of the same name with two copies of the same image inside

    I use Vista 32 bit. I started to see this problem when I recently copy photos from an SD card.

    When you add pictures to the Vista image folder it creates a folder HIDDEN of the same name with two copies of the same image inside

    I saw hidden files enabled in folder options.

    Any help is appreciated.

    Hey guys... I found the answer... Problem is with the software vaio content analyzer that comes with the vaio...

    It just search for Vaio Content Analyzer (parameters) and deselect

    Video analysis
    image analysis
    analysis of the music

    I went looking for this crazy thing... used almost all types of online scan, and finally to find that it is caused by an internal software only...
    anyway thanks to all... Hope it helps someone in the future :)
  • share 'Start' content and presentation of the tile with multiple users on the same Tablet Windows RT

    Hello

    Can someone tell me if it is possible to share/copy the content start and the tile layout with multiple users on the same shelf?

    Hello

    Welcome to the Microsoft community.

    Unfortunately, you will not be able to share or copy the content start and available to tile with multiple users. However, you can rearrange the tiles and select the content to be displayed manually on each of the user accounts.

    See these articles for more details:

    Customize the splash screen

    http://Windows.Microsoft.com/en-in/Windows-8/start-screen#1TC=T1

    Reorganization of the tiles start

    http://Windows.Microsoft.com/en-in/Windows-8/rearrange-tiles-start

    I hope this helps. Let us know if you need more assistance.

  • Combine data sources with different granularity in the same fact table?

    I have two operating tables 'Incident (157 columns)' and 'unit (70 Colums) '. For all the "incidents" happening there could be one or more records in the table of the 'unit '.

    As part of my design of data mart, I have merged the tables in one "makes the incident (227 columns)" and insert records from two tables with a join condition between them [incident. IN_NUM = Unit.IN_NUM].

    Is this correct, is my question? or am I mix data sources with different granularity in the same fact table. Appreciate your help.

    Best regards
    Bees

    Bees,
    Are the measures of the 'Incident', repeated during an incident given, in more than one record in the table of the unit? If so, then the sum (indicent.measure) will give an incorrect result?

    What is there to merge physically tables set outside OBIEE? With OBIEE you might have a table of 'facts' logic to present the user with report, which from tables separated units and Incidents and would stop the occurrence of incorrect aggregations. A common piece of modeling in the same way would be arrested in OBIEE headers and lines of command, quite common to have a logical fact 'orders' which contained the two header orders and order line, this translates into the Incidents-> relationship of units.

    To do what I mentioned, is relatively simple, you need a "Dim - Incident" at two levels, unit, mapp and Incident unique identifiers as keys to level and then use these levels to define the content of the levels correctly in your 2 tables logic sources logic "done", IE the LTS Incidents at incident level LTS units as level of units.

    Hope this helps, let us know if you get stuck.
    See you soon
    Alastair

  • You can play with two players on the same conlsoe on modern war

    XXXXXXXXXXXXXX

    Hi Capcurt,

    Welcome to the Microsoft Answers site!

    You try to play the game live with 2 players on the same console?

    Unfortunately, you can not go online with two people on the same console. Modern Warfare 2 allows a user to sign in when you play in multiplayer.

    Modern Warfare 2 is a completely new game mode that supports 2-player cooperative play in single line of the solo campaign in history. To do this, you will need two machines.

    For better support, you can contact Xbox Forums.

    http://www.xbox360forum.com/Forum/

    Hope this information is useful.

    Amrita M

    Microsoft Answers Support Engineer
    Visit our Microsoft answers feedback Forum and let us know what you think.

  • Why I get two files with different names but the same image when I save in 2015 CC Photoshop?

    Why I get two files with different names but the same image when I save in 2015 CC Photoshop?

    Try resetting the PS preferences - Preferences in Photoshop

    ~ Rohit

  • I have a desktop version independent of PS6 - if I download some desktop PS CC application will be this result in all conflicts with both applications on the same desktop computer?

    I have a desktop version independent of PS6 - if I download some desktop PS CC application will be this result in all conflicts with both applications on the same desktop computer?

    Hello

    Yes both licenses will be in conflict and it will activate CS6 with creative cloud.

    You can only use a license on a computer and you can also download the previous version of the creative cloud.

    Concerning

    Gilles marneffe

  • Help with time operations invloving the SQL query

    I created 2 tables in my SQL. One is the user_info_table table that stores the connection time and time zone of login for each user. The other is the post_table that stores user postid, who makes the post time post and time zone for each message.
    CREATE TABLE user_info
    (
    user_id VARCHAR(20), 
    login_date DATE,
    login_time_zone VARCHAR(20),
    PRIMARY KEY (user_id)
    );
    CREATE TABLE post_table
    (
    post_id VARCHAR(20),  
    user_id VARCHAR(20), 
    datepost DATE,  
    time_zone VARCHAR(20),
    PRIMARY KEY (post_id),
    FOREIGN KEY (user_id) REFERENCES user_info(user_id) ON DELETE CASCADE
    ) ;
    Some examples of data for my paintings is as below.
    INSERT INTO user_info VALUES( 'u1', to_date('9/17/2009 20:00','MM/DD/YYYY mi:ss'), -2 );
    INSERT INTO user_info VALUES( 'u2', to_date('9/17/2009 19:55','MM/DD/YYYY mi:ss'), -4 );
    
    INSERT INTO post_table VALUES( 'p1', 'u1', to_date('9/17/2009 20:50','MM/DD/YYYY mi:ss'), 6 );
    INSERT INTO post_table VALUES( 'p2', 'u2', to_date('9/17/2009 20:30','MM/DD/YYYY mi:ss'), -5 );
    INSERT INTO post_table VALUES( 'p3', 'u2', to_date('9/18/2009 6:00','MM/DD/YYYY mi:ss'), 2 );
    INSERT INTO post_table VALUES( 'p4', 'u1', to_date('9/17/2009 21:00','MM/DD/YYYY mi:ss'), -3 );
    I need to write a SQL query which - is the user whose time difference between the time of the connection and the last time when he or she wrote a post is the smallest. I need to consider the time zones here as well.

    I am not sure if time_zone must be of type VARCHAR or TIMESTAMP then have created as a VARCHAR in my paintings.

    Someone help me please to form this query.

    PS: How can I tags < code > user in this forum to write sql statements.

    Published by: user11994430 on October 9, 2009 17:59

    You can do this by adding a clause of "HAVING" as shown below. However, with your test defined data it returns ZERO rows because for all users of the MAX (DATEPOST) IS LESS than MAX (LOGIN_DATE)

    SELECT     USER_ID
    FROM
    (
         SELECT     USER_ID
         ,     DENSE_RANK() OVER (ORDER BY POST_LOGIN_DIFF) RNK
         FROM
         (
              SELECT     USER_INFO.USER_ID
              ,     MAX(DATEPOST) - MAX(LOGIN_DATE) AS POST_LOGIN_DIFF
              FROM     USER_INFO
              JOIN     POST_TABLE     ON POST_TABLE.USER_ID = USER_INFO.USER_ID
              GROUP BY USER_INFO.USER_ID
              HAVING      MAX(DATEPOST) > MAX(LOGIN_DATE)
         )
    )
    WHERE RNK = 1
    
  • Mulltiple criteria on the same column - please help with query

    Could someone help with this query?

    I have three tables-

    Table: SuperOxMap
    -------------
    ox_id
    ox_pd

    Table: SuperCPDtls
    ------------
    ox_pd
    ox_dtid

    Table: SuperPkDtls
    ------------
    ox_dtid
    ox_pkVal

    Example of SuperPkDtls-
    -------------
    ox_dtid ox_pkVal
    1 T1
    2 T2
    3 T3

    As seen above, SuperOxMap.ox_pd = SuperCPDtls.ox_pd and SuperCPDtls.ox_dtid = SuperPkDtls.ox_dtid.

    I find list of the SuperOxMap.ox_id and the SuperPkDtls.ox_pkVal where pkVal for this ox_id contains T1 and T2, but excludes the T3.

    If I put in my join query IN ("T1", "T2") and! = 'T3', which obviously do not work. I don't know how / if I can use the inner query so that he can give me all the ox_ids where associated pkVal contains T1 and T2 but would not T3 contain.

    You will appreciate all help!
  • Help with a sql using the ASCII value

    Hello

    I have a table with the following data
    DOC_NO     CLIENT_REV
    ------     ----------
    240          A
    240          B
    240          B
    240          C
    240          C
    240          C
    240          0
    240          1
    240          1
    240          1
    240          2
    240          2
    240          2
    241          A
    241          A
    241          B
    241          C
    241          C
    241          D
    The requirement is retur the following values:
    240          2
    241          D
    I tried with the ASCII function, but the necessary result is somewhat complicated. That is to say for a group of doc_no I need as many client_rev if there are numbers, if not then I need the largest letter.

    How can I achieve this requirement?

    Kind regards.

    Hello

    What do you mean by "the decision must be the minor doc_date?
    It would be useful that you posted your desired results.
    I assume you mean you want only the line with the doc_date lowest (for each doc_no).
    in other words, the desired results are:

        DOC_NO L DOC_DESC           DOC_DATE
    ---------- - ------------------ ---------
           240 2 Description of 240 01-JAN-09
           241 D Description of 241 01-FEB-09
    

    Here's a way to get these results:

      SELECT x.doc_no, x.last_client_rev, y.doc_desc, y.doc_date
      FROM     (     -- Begin sub-query based on table_a
         SELECT   doc_no,
                     MAX (client_rev) KEEP
                       (DENSE_RANK LAST ORDER BY TRANSLATE(client_rev
                                                           ,'ABCD012' -- Desired sort order
                                                           ,'012ABCD'  -- Actual sort order
                                                          )) AS last_client_rev
         FROM       table_a
            GROUP BY  doc_no
         ) x,
         (     -- Begin sub-query based on table_b
         SELECT       doc_no,
                MAX (doc_desc) KEEP (DENSE_RANK FIRST ORDER BY doc_date)
                             AS doc_desc,
                MIN (doc_date)     AS doc_date
         FROM       table_b
         GROUP BY  doc_no
         ) y
      WHERE x.doc_no = y.doc_no;
    

    As you restrict the table_a lines are used, so now you restrict table_b lines are used.

    First, let's review what you do with table_a.
    Table_a has multiple lines with the same doc_no.
    You only want to consider one of these lines: one that comes last, when to sort your particular way.
    If your main query does not use the table_a:
    It uses a subquery table_a-based which is a GROUP BY doc_no, to ensure that only the row by doc_no is used.

    Now, what is the problem with table_b?
    Table_b has as many rows with the same doc_no.
    You only want to consider one of these lines: the one with the lowest doc_date.
    So your main query should not use table_b:
    She must use a subquery table_b-based which is a GROUP BY doc_no, to ensure that only the row by doc_no is used.

  • Need help with query to get days of work there are using the calendar BOM

    RDBMS: 10.2.0.4.0
    Oracle Applications: 11.5.10.2

    I try to use the BOM existing in the EBS to calculate working days calendar, there is a report that I am train. The BOM calendar presents the working days and non-working with the weekend days and holidays listed as non-working days. The following query gives correct results, but I'm looking for two different ways to do the same without "the union. I have nothing against the "unions", but I feel that I'm missing a more elegant to get there way. The query will in Discoverer Plus 10.1.2.3, so using a statement 'By' is not supported.
    sample data
    
    calendar_code                calendar_date                 seq_num                     
    SAC-WRKDAY                   12/3/2010                     1817
    SAC-WRKDAY                   12/4/2010                                                              
    SAC-WRKDAY                   12/5/2010                                                              
    SAC-WRKDAY                   12/6/2010                     1818                                 
    SAC-WRKDAY                   12/7/2010                     1819                                 
    SAC-WRKDAY                   12/8/2010                     1820     
    SAC-WRKDAY                   12/9/2010                     1821
    SAC-WRKDAY                   12/10/2010                    1822
    SAC-WRKDAY                   12/11/2010                   
    SAC-WRKDAY                   12/12/2010
    SAC-WRKDAY                   12/13/2010                    1823             
    select calendar_code
         , calendar_date
         , seq_num
         , sum(decode(bcd.seq_num,null,0,1))over(partition by bcd.calendar_code order by bcd.calendar_date desc) workdays_ago
    from   bom.bom_calendar_dates bcd
    where  calendar_code = 'SAC-WRKDAY'     
    and    trunc(bcd.calendar_date) < trunc(sysdate)
    
    union
    
    select calendar_code
         , calendar_date
         , seq_num
         , -sum(decode(bcd.seq_num,null,0,1))over(partition by bcd.calendar_code order by bcd.calendar_date ) workdays_ago
    from   bom.bom_calendar_dates bcd
    where  calendar_code = 'SAC-WRKDAY'
    and    trunc(bcd.calendar_date) > trunc(sysdate)
    sample output
    
    calendar_code                calendar_date                 seq_num                      workdays_ago
    SAC-WRKDAY                   12/3/2010                     1817                                  3
    SAC-WRKDAY                   12/4/2010                                                           2
    SAC-WRKDAY                   12/5/2010                                                           2
    SAC-WRKDAY                   12/6/2010                     1818                                  2
    SAC-WRKDAY                   12/7/2010                     1819                                  1
    SAC-WRKDAY                   12/9/2010                     1821                                 -1
    SAC-WRKDAY                   12/10/2010                    1822                                 -2
    SAC-WRKDAY                   12/11/2010                                                         -2
    SAC-WRKDAY                   12/12/2010                                                         -2
    SAC-WRKDAY                   12/13/2010                    1823                                 -3

    Hello

    Of course, you should be able to combine these queries, something like this:

    select calendar_code
         , calendar_date
         , seq_num
         , sum ( CASE
               WHEN  bcd.seq_num IS NULL
               THEN  0
               WHEN  bcd.calendar_date < TRUNC (SYSDATE)
               THEN  1
               ELSE  -1
              END
            ) over ( partition by  bcd.calendar_code
                  ,            SIGN (SYSDATE - bcd.calendar_date)
                  order by        ABS  (SYSDATE - bcd.calendar_date)
                ) workdays_ago
    from   bom.bom_calendar_dates bcd
    where  calendar_code = 'SAC-WRKDAY'
    and    trunc(bcd.calendar_date) < trunc(sysdate)
    

    If you do not want to display some CREATE TABLE and INSERT statements for the sample data, and then I could test it.

    t_norwillo wrote:
    ... I have nothing against the "unions", but I feel that I'm missing a more elegant to get there way.

    Good thinking!
    When the two branches of the UNION query the same table, there is usually a more effective way: something that only requires a pass through the table.

Maybe you are looking for