Query to find the sum of different groups of same column

Hi ,
I have a table as follows:
customers
custid credit amt month
001 C 2000 Jan-2012
001 D 5000 Feb-2012
001 C 3000 Mar-2012
001 C 3000 Apr-2012
001 D 7000 May-2012
I Have to write a single query to calculate the sum of credit and sum of debit value separately.
Thanks & Regards,
SB2011

Hi,
SB2011 wrote:
Hi ,
I have a table as follows:
customers
custid credit amt month
001 C 2000 Jan-2012
001 D 5000 Feb-2012
001 C 3000 Mar-2012
001 C 3000 Apr-2012
001 D 7000 May-2012
I Have to write a single query to calculate the sum of credit and sum of debit value separately.Getting the sum sounds like a job for the SUM function.
Getting separate sums for credit and debit sounds like a job for GROUP BY.
Thanks & Regards,
SB2011Here's one way:
SELECT    credit
,       SUM (amt)     AS total
FROM       customers
GROUP BY  credit
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ {message:id=9360002}

Similar Messages

  • Query to Find the Student ids by group

    Hi,
    I have a table which has 2 columns student_id and Course_id.
    Sample Data
    Student_id Course_id
    1 -->     1
    1 -->     2
    1 -->     3
    1 -->     4
    2 -->     1
    2 -->     3
    3 -->     1
    3 -->     2
    3 -->     4
    I want to find those students who have taken all the courses 1,3,4.

    952541 wrote:
    I want to find those students who have taken all the courses 1,3,4.Depends on:
    a) Can student take same course more than once?
    a) Only courses 1,3,4 and no other courses?
    1. Student can't take same course more than once and student took courses 1,3,4 and may be some other courses:
    select  stu_id
      from  t
      where stu_curs in (1,3,4)
      group by stu_id
      having count(stu_curs) = 3
    /2. Student can take same course more than once and student took courses 1,3,4 and may be some other courses:
    select  stu_id
      from  t
      where stu_curs in (1,3,4)
      group by stu_id
      having count(distinct stu_curs) = 3
    /3. Student can't take same course more than once and student took courses 1,3,4 and no other courses:
    select  stu_id
      from  t
      group by stu_id
      having sum(case when stu_curs in (1,3,4) then stu_curs else 10 end) = 8
    /2. Student can take same course more than once and student took courses 1,3,4 and no other courses:
    select  stu_id
      from  t
      group by stu_id
      having sum(distinct case when stu_curs in (1,3,4) then stu_curs else 10 end) = 8
    /SY.

  • Sum two different types in same column

    Hi I have the below as sql output.
    TYPE SUM(UPDATE_COUNT)
    Legis 93
    Acds 43
    Updates 41
    Multibases 345
    but i want updates to be displayed as Sum of updates and Multibases. It should be like the below.
    Updates 386
    the query i used for the first output is
    SELECT type, SUM(Update_Count) FROM Scope1 where type in ('Updates','Multibases','DAIS','Acds','Legis','LegAll') and (RECVD_DATE >='04/02/2012' and RECVD_DATE <='11/30/2012') group by type
    please help me how do i get the second output.
    Thanks

    I suspect you just need to provide an appropriate format mask, i.e.
    SQL> select to_char(7, '999999999.999' ) from dual;
    TO_CHAR(7,'999
             7.000
    SQL> ed
    Wrote file afiedt.buf
      1* select to_char(17.2, '999999999.999' ) from dual
    SQL> /
    TO_CHAR(17.2,'
            17.200Justin

  • Query to find the request group associated with a responsibility

    Hi All,
    I have to attach a report to around 30 responsibilities. For this I will have to find the request group for each responsibility and then add the Concurrent Program for the report in the corresponding request group.
    Can anyone help me with a query to find the request group associated with a responsibility.
    Regards,
    SK

    Just to add to the query above, if you want to see request group along with request_group_id you do join as follows
    SELECT frv.responsibility_name
    ,frg.request_group_name
    FROM fnd_request_groups frg,
    fnd_request_group_units frgu,
    fnd_responsibility_vl frv
    WHERE 1 = 1
    AND frgu.request_group_id = frg.request_group_id
    AND frv.request_group_id = frg.request_group_id
    ORDER BY responsibility_name
    Thanks
    Shailendra

  • How to find the sum of a column

    I need to find the sum of a column and use it in a different column. The following is the example.
    Column names: Feedback(Good, Avg, Poor), Count(no of good, no of avg, no of poor) and %age(Feedback/sum(feedback))
    I want to find the sum in Java class and also calculate the last column in java class.
    Please tell me some way to do it.

    oh.. ok ..thanks for letting me know.. i will formulate the question in a proper way:
    This is what my UI should look like:
    Rating Count Percent
    Excellent 2 20
    Good 6 60
    Poor 1 10
    Bad 1 10
    Now i have the following columns in the data base:
    Meaning and feedback_rating.
    So the following SQL Query:
    SELECT hrl.meaning rating,
    sum(decode(bcpi.feedback_rating, null, 0, 1)) counted
    from cmp_cwb_person_info bcpi ,
    hr_lookups hrl
    group by hrl.meaning
    will give me the result as
    rating counted
    Excellent 2
    Good 6
    Poor 1
    Bad 1
    Now I want a third column as percentage : Earlier we were doing this calculation in the sql query itself, so the query was like
    SELECT hrl.meaning rating,
    sum(decode(bcpi.feedback_rating, null, 0, 1)) counted,
    sum(decode(bcpi.feedback_rating, null, 0, 1))/(max (select count (*) from cmp_cwb_person_info bcpi ,
    hr_lookups hrl )) percent
    from cmp_cwb_person_info bcpi ,
    hr_lookups hrl
    group by hrl.meaning
    Hence the third column (percent) was calculated in the sql query itself.
    But now i feel that the performance of the query could be improved if we get the first two columns from the database and the calculate the third column programatically in the java code.
    So this is what I want to know. How can i do that?

  • Query to find the memory of database in oracle,sql

    Hi All,
    Please let me know the query to find the memory of database in oracle,sql.
    Thanks,
    sajith

    How do I find the overall database size?
    The biggest portion of a database's size comes from the datafiles. To find out how many megabytes are allocated to ALL datafiles:
    select sum(bytes)/1024/1024 "Meg" from dba_data_files;
    To get the size of all TEMP files:
    select nvl(sum(bytes),0)/1024/1024 "Meg" from dba_temp_files;
    To get the size of the on-line redo-logs:
    select sum(bytes)/1024/1024 "Meg" from sys.v_$log;
    Putting it all together into a single query:
    select a.data_size+b.temp_size+c.redo_size "total_size"
    from ( select sum(bytes) data_size
    from dba_data_files ) a,
    ( select nvl(sum(bytes),0) temp_size
    from dba_temp_files ) b,
    ( select sum(bytes) redo_size
    from sys.v_$log ) c;
    Another query ("Free space" reports data files free space):
    col "Database Size" format a20
    col "Free space" format a20
    select round(sum(used.bytes) / 1024 / 1024 ) || ' MB' "Database Size"
    , round(free.p / 1024 / 1024) || ' MB' "Free space"
    from (select bytes from v$datafile
    union all
    select bytes from v$tempfile
    union all
    select bytes from v$log) used
    , (select sum(bytes) as p from dba_free_space) free
    group by free.p
    This is what I use :P From http://www.orafaq.com/wiki/Oracle_database_FAQ#How_do_I_find_the_overall_database_size.3F

  • Query to find the size of a schema

    Hi,
    Can you give me the Oracle query to find the size of a schema ?
    Thanks,
    Smitha

    Hi,
    Following query tell you the size of schema.
    SELECT owner,
    Sum(bytes)/1024/1024 AS total_size_mb
    FROM dba_segments
    WHERE owner = Upper('&1')
    GROUP BY owner
    Regards,
    Awais Riaz

  • Sql query to find the balances for a customer account wise.

    Hi,
    Could someone help me with the sql query to find the balances for each customer account wise. This is need to generate the report.
    presently we are using this query, but the output doesnot return the expected result.
    SELECT sum(nvl(ps.acctd_amount_due_remaining,0)) "Balance"
    FROM      ra_cust_trx_line_gl_dist_all gld,
              gl_code_combinations c,
              ar_payment_schedules_all ps,
              RA_CUSTOMER_TRX_ALL rat,
              ra_customers rc
    WHERE      c.CHART_OF_ACCOUNTS_ID = 101
    and gld.code_combination_id = c.code_combination_id
         and rat.CUSTOMER_TRX_ID=gld.CUSTOMER_TRX_ID
         and rat.CUSTOMER_TRX_ID=ps.CUSTOMER_TRX_ID
    and ps.customer_id=rc.customer_id
         and ps.status='OP'
         and ps.gl_date <= :PDATE
         and ps.org_id=:PORGID
         and ps.class in ('GUAR','INV','DM','DEP')
    and c.SEGMENT4=:Account_id
    and ps.customer_id=:Customer_id
    Thanks in advance.
    Kalyan.

    Can someone help us with this.

  • Query to find the the free space in raw disks

    I am new to ASM.
    What is the query to find the free space in the raw disks in ASM diskgroup.? Can i get space info about individual raw disks in the disk group?
    Is there an ip address for the ASM disk group? If so how can i find it?

    below query will give you total and free space for each raw disk from a disk group.
    select dg.name, d.name, d.total_mb, d.free_mb from v$asm_disk d, v$asm_diskgroup dg where dg.group_number=d.group_number order by 1;
    There no IP address attached to ASM disk group. Why do you want associate IP address with a Disk group?

  • Query to find the long running concurrent requests morethan 2 hours

    Can any one please provide the " Query to find the long running concurrent requests which are running more than 2 hours"

    You need to find the different between the current time/date and FND_CONCURRENT_REQUESTS.ACTUAL_START_DATE for the running requests.
    More details can be found in the following notes:
    Note: 187504.1 - bde_request.sql - Process and Session info for one Concurrent Request (11.5)
    https://metalink2.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=NOT&p_id=187504.1
    Note: 134035.1 - ANALYZEREQ.SQL - Detailed Analysis of One Concurrent Request (Release 11 and up)
    https://metalink2.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=NOT&p_id=134035.1
    Note: 751438.1 - How To Check List , Start And End Dates Of A Parent Concurrent Request And All Childs
    https://metalink2.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=NOT&p_id=751438.1

  • Query to find the class name that has the maximum number of students ?

    I need the query to find the [class Name] which has the most number of students. please look below at the tables
    Students Table                                                                     
    StudentId     StudentName  ClassID                                    
    1                  xxx                   1                                           
    2                  yyy                   1                                           
    3                  zzz                   1  
    4                  fff                    2
    5                   ttt                  2
     Classes Table
     ClassID          ClassNane
     1                    CSHARP
     2                    JSHARP
    The result should be : CSHARP
    since there are 3 students in CSHARP and 2 students in JSHARP class
    Appreciate your help
    Thanks

    Try:
    DECLARE @Classes TABLE (
    ClassID INT identity(1, 1) PRIMARY KEY
    ,ClassName VARCHAR(50)
    INSERT INTO @Classes (ClassName)
    VALUES ('CSharp')
    ,('JSharp')
    DECLARE @Students TABLE (
    StudentID INT identity(1, 1) PRIMARY KEY
    ,StudentName VARCHAR(10)
    ,ClassID INT
    INSERT INTO @Students (
    StudentName
    ,ClassID
    VALUES (
    'xxx'
    ,1
    'yyy'
    ,1
    'zzz'
    ,1
    'fff'
    ,2
    'ttt'
    ,2
    SELECT TOP (1)
    WITH TIES C.ClassName
    FROM @Classes C
    INNER JOIN @Students S ON C.ClassID = S.ClassID
    GROUP BY C.ClassID
    ,C.ClassName
    ORDER BY COUNT(S.StudentID) DESC
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • How to find the sum based on two variables

    Hello!
    I have just started using Numbers, I have a spreadsheet which lists a number of transactions.
    On my chart I have Columns "Date", "Memo", "Type", and "Amount".
    The Type column is a drop down list. What I want to do and find the sum for transactions that fall between a date range (i.e. months 1-3) and that match a certain "Type". I can figure out how to use the sumif function to find the sum for a date range, however I cant figure out how then to only sum only those that match a type.
    Any help?

    Brendon,
    This question arises from time to time in different situations. The answer is to first create an auxiliary column which calculates whether or not your multiple conditions have been met. In this case you have three - more than or equal a start date, less than or equal an end date, and a "Type".
    I'm assuming that your dates and "type" are flexible, so an extra table to enter your search data is shown below, but these three cells could be incorporated in your main table. The formula for the auxiliary column is:
    =IF(AND(Date>=Search Range :: A$2,Date<=Search Range :: B$2,Type=Search Range :: C$2),"SUM","")
    All rows with the word "SUM" satisfy the three conditions. The footer row contains the sum of the values meeting the desired conditions using:
    =SUMIF(Aux, “SUM”, Amount)
    Obviously this has to be reworked to your situation, but the auxiliary column is the key.
    pw
    Message was edited by: pw1840

  • Query to find the list of users having access to a particular scenario

    Hi,
    I am learning Hyperion Planning 9.2 x version. I wanted to know the query to find the list of users having access to Plan Iteration - 1 scenarion.
    As I am new to Hyperion Essbase and Hyperion Planning, I am assuming these ideas work out to get the desired result.
    1) As Hyperion Planning uses Relational DB to store the User Security information, we can query the list of users who is having access to Plan Iteration - 1 Scenario.
    I am not sure if this solution works. Please correct me If I am wrong.
    2) We can also query from the essbase editor to find out who all having access to this scenario.
    If the above is correct, can you please provide me the query.
    I am really need of this and I will be happy if any one provide the solution.
    Thanks & Regards,
    Upendra. Bestha

    Hi,
    If you are looking for some SQL to retrieve the access rights by member then you can use something like (SQL Server code though can easily be modified for Oracle)
    SELECT usr.object_name as Username,mem.object_name as Member,
    'Access Rights' = CASE acc.access_mode
    WHEN -1 THEN 'None'
    WHEN 1 THEN 'Read'
    WHEN 2 THEN 'Write'
    WHEN 3 THEN 'Write'
    ELSE 'Unknown' END,
    'Relation' = CASE acc.flags
    WHEN 0 THEN 'Member'
    WHEN 5 THEN 'Children'
    WHEN 6 THEN 'Children (inclusive)'
    WHEN 8 THEN 'Descendants'
    WHEN 9 THEN 'Descendants (inclusive)'
    ELSE 'Unknown' END
    FROM
    hsp_access_control acc, hsp_object mem, hsp_object usr
    WHERE acc.object_id = mem.object_id
    AND acc.user_id = usr.object_id
    AND mem.object_name = 'Plan Iteration - 1'
    Cheers
    John
    http://john-goodwin.blogspot.com/

  • How to run the Invoices in different groups within the batch in AP

    Need to know that how to run the Invoices in different groups within the batch. This will be of very helpful when we deal with lot of lines under one batch. (e.g) like somewhere we issued a Corporate Card to all the employees Via Bank Of America. Every Month they will send the complete details of all the employees who ever swiped the corporate provided BOA. Accordding to Natco all those lines should be loaded as One Invoice so that a Single Payment can be provided to BOA and it will also makes their life easier. This standard program sometimes it works normal sometimes it will run like a TORTOISE. So thats why in the manual they suggested to use GROUP ID to split the Invoice Load.
    So plz tell me how we can run it
    plz give me the solution
    thanks

    can you give me some material or document on that so that i can read actuaaly i need to make doc on it..

  • Query to find the no of columns in a table

    Is there a query to find the total number of columns in a table?

    Dou you know read?
    -- the number of rows in a table named yourtable:
    select count(1) from yourtable;
    -- the number of columns in a table
    -- If is a user's table:
    select count(1)
    from user_tab_columns
    where table_name='YOURTABLE'
    --If it is´t
    select count(1)
    from DBA_tab_columns
    where owner = 'USEROWNER'
    and table_name='YOURTABLE'

Maybe you are looking for