How to write SQL query and apply aggregate functions on it

Hello experts,
Iu2019ve a task to write SQL query on tree tables and do inner join on them. Iu2019ve accomplish this task by using T-CODE SQVI. However now I need to write a query and apply SQL functions on it i.e. Add, Count, Max and Min etc. Please can someone tell me how I can write SQL query with aggregate functions in SAP?
Thanks a lot in advance

HI Mr. Cool
you can see the below code for using aggregate functions.
where ARTIST and SEATSOCCU are the field names for which you want to perform these functions.
DATA: TOTAL_ENTRIES TYPE I,
      TOTAL_ATT TYPE I,
      MAX_ATT TYPE I,
      AVG_ATT TYPE I.
SELECT COUNT( DISTINCT ARTIST )
       SUM( SEATSOCCU )
       MAX( SEATSOCCU )
       AVG( SEATSOCCU ) FROM YCONCERT INTO (TOTAL_ENTRIES, TOTAL_ATT,
MAX_ATT, AVG_ATT).
Thanks
Lalit Gupta

Similar Messages

  • How to write sql query for counting pairs from below table??

    Below is my SQL table structure.
    user_id | Name | join_side | left_leg | right_leg | Parent_id
    100001 Tinku Left 100002 100003 0
    100002 Harish Left 100004 100005 100001
    100003 Gorav Right 100006 100007 100001
    100004 Prince Left 100008 NULL 100002
    100005 Ajay Right NULL NULL 100002
    100006 Simran Left NULL NULL 100003
    100007 Raman Right NULL NULL 100003
    100008 Vijay Left NULL NULL 100004
    It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..
    I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains  both left_leg and right_leg id, then it is called pair.)
    I know there are three pairs under id "100001" :-
    1.  100002 and 100003
    2.  100004 and 100005
    3.  100006 and 100007
        100008 will not be counted as pair because it does not have right leg..
     But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...
    Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005

    Sounds like this to me
    DECLARE @ID int
    SET @ID = 100001--your passed value
    SELECT left_leg,right_leg
    FROM table
    WHERE (user_id = @ID
    OR parent_id = @ID)
    AND left_leg IS NOT NULL
    AND right_leg IS NOT NULL
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to write sql query for below mentioned eaxmple.

    Hi,
    I have requirement.
    There are number of rows and I need the result through query as follows.Please help me to proved sql query for below mentioned requirement.
    example: table TEST.
    COLA COLB COLC COLD COLE COLF MANAGER 5 NULL NULL 3 NULL
    SR.MANAGER 6 3 NULL NULL NULL
    VP 5 5 4 5 5
    I have to write the sql query if COLA IS MANAGER THEN CONSIDER MANGER RECORD,AND IF ANY COLUMN FILED IS NULL FOR MANGER THEN CONSIDER COLA IS SR.MANGER, AND IF ANY COLUMN FILED IS NULL FOR SR,MANGER THEN CONSIDER VP records.
    I need output as below.
    COLB COLC COLD COLE COLF
    5(manager) 3(sr.manger) 4(vp) 3(manger) 3(vp)
    Please provide the for above mentioned output.
    Thanks

    Duplicate thread. please view the answer posted in your first thread.
    how to write sql query.
    And, please don't post any duplicate thread.
    Regards.
    Satyaki De.

  • How to write sql query with many parameter in ireport

    hai,
    i'm a new user in ireport.how to write sql query with many parameters in ireport's report query?i already know to create a parameter like(select * from payment where entity=$P{entity}.
    but i don't know to create query if more than 1 parameter.i also have parameter such as
    $P{entity},$P{id},$P{ic}.please help me for this.
    thanks

    You are in the wrong place. The ireport support forum may be found here
    http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=215&func=showcat&catid=9

  • How will write SQL query to fetch data from  each Sub-partition..

    Hi All,
    Anyone does have any idea about How to write SQL query to fetch data from Sub-partition.
    Actually i have one table having composite paritition(Range+list)
    Now if i want to fetch data from main partition(Range) the query will be
    SELECT * FROM emp PARTITION(q1_2005);
    Now i want to fetch data at sub-partition level(List) .But i am not able to get any SQL query for that.
    Pls help me to sort out.
    Thanks in Advance.
    Anwar

    SELECT * FROM emp SUBPARTITION(sp1);

  • How to write  sql query?

    create table test (
    company varchar2(2),
    order_number number(4),
    item_id number(4),
    quantity number(8),
    item_desc varchar2(16)
    insert into test values ('A',1001,1,10,'apple');
    insert into test values ('A',1001,2,20,'banana');
    insert into test values ('B',1002,2,50,'banana');
    insert into test values ('B',1002,1,30,'orange');
    commit;
    Table Test DATA
    company order_number item_id quantity item_desc
    1001 1 10 apple
    A 10
    1002 2 50 banana
    1002 1 30 orange
    B 80
    90
    how to make above result in using sql query ?
    thanks !

    And just so you can see....
    We can also get the totals and even break down the cursors to show individual orders within each of the companys...
    SQL> select * from test;
    CO ORDER_NUMBER    ITEM_ID   QUANTITY ITEM_DESC
    A          1001          1         10 apple
    B          1002          2         50 banana
    B          1002          1         30 orange
    B          1003          2         40 pear
    B          1003          1         70 grapefruit
    SQL>
    SQL> select cursor(
      2  select cursor(select cursor(select item_id, quantity, item_desc
      3                              from   test t4
      4                              where  t4.order_number = t2.order_number
      5                              and    t4.company = t2.company
      6                              order by item_id) order_details,
      7                       order_number,
      8                       (select sum(quantity)
      9                        from   test t5
    10                        where  t5.company = t2.company
    11                        and    t5.order_number = t2.order_number) order_quantity_total
    12                from   (select distinct company, order_number from test) t2
    13                where  t2.company = t.company
    14                order by order_number) company_orders
    15        ,RPAD(company,10,' ') company
    16        ,(select sum(quantity)
    17          from   test t3
    18          where  t3.company = t.company) company_quantity
    19  from (select distinct company from test) t
    20  ) company_orders,
    21  (select sum(quantity) from test) as total_quantity
    22  from dual;
    COMPANY_ORDERS       TOTAL_QUANTITY
    CURSOR STATEMENT : 1            200
    CURSOR STATEMENT : 1
    COMPANY_ORDERS       COMPANY    COMPANY_QUANTITY
    CURSOR STATEMENT : 1 A                        10
    CURSOR STATEMENT : 1
    ORDER_DETAILS        ORDER_NUMBER ORDER_QUANTITY_TOTAL
    CURSOR STATEMENT : 1         1001                   10
    CURSOR STATEMENT : 1
       ITEM_ID   QUANTITY ITEM_DESC
             1         10 apple
    CURSOR STATEMENT : 1 B                       190
    CURSOR STATEMENT : 1
    ORDER_DETAILS        ORDER_NUMBER ORDER_QUANTITY_TOTAL
    CURSOR STATEMENT : 1         1002                   80
    CURSOR STATEMENT : 1
       ITEM_ID   QUANTITY ITEM_DESC
             1         30 orange
             2         50 banana
    CURSOR STATEMENT : 1         1003                  110
    CURSOR STATEMENT : 1
       ITEM_ID   QUANTITY ITEM_DESC
             1         70 grapefruit
             2         40 pear
    SQL>

  • How to write sql query that display comma suppurated result using Group by

    Hi,
    I am having data like bellow ,
    Above result got from joining two tables VMTAGroupClient,VMTAipNames .
    Query i have written to display above result is,
    select vgc.VMTAGroupId,vn.VMTAName from VMTAGroupClient vgc inner join VMTAipNames vn
    on vgc.VMTAID=vn.VMTANameID group by vgc.VMTAGroupId,vn.VMTAName 
    using the VMTAGroupId column how to write query to display result result like,
    VMTAGroupID    VMTAs
       1                       VMTA1,VMTA3
       2                       VMTA2,VMTA4,VMTA5
    Regards,
    Anwar Shaik

    Satheesh,
    Here in my case data need to read from two tables VMTAGroupClient, VMTAipNames.
    VMTAGroupId is in one table and VMTAName column in some other table.Iin both the tables VMTAID is common.
    Please check the above result displayed data from two tables.
    can we write same query using join?
    Anwar Shaik

  • How to write sql query for below example.

    Hi,
    I have requirement.
    There are number of rows and I need the result through query as follows.Please help me to proved sql query for below mentioned requirement.
    example: table TEST.
    COLA COLB COLC COLD
    MANAGER 5 NULL null
    SR.MANAGE 6 3 NULL
    VP 5 5 4
    I have to write the sql query if COLA IS MANAGER THEN CONSIDER MANGER RECORD,AND IF ANY COLUMN FILED IS NULL FOR MANGER THEN CONSIDER COLA IS SR.MANGER, AND IF ANY COLUMN FILED IS NULL FOR SR,MANGER THEN CONSIDER VP records.
    I need output as below.
    COLB COLC COLD
    5(MANGER) 3(sr.manger) 5(vp)
    Please provide the for above mentioned output.
    Thanks

    <<if COLA IS MANAGER THEN CONSIDER MANGER RECORD>>
    What does this sentence means? COLA does not cnatin a single record but the number of records with diffrent values.
    Regards
    Arun

  • How to write SQL query to flatten the hierarchy

    Hi,
    I have person table which has recursive hierarchy and I wish to flatten it upto 5 levels.
    I am using Oracle10g and I have written following query to flatten the hierarchy
    SELECT
    ID,
    level lvl,
    REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 1) AS level_1,
    REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 2) AS level_2,
    REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 3) AS level_3,
    REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 4) AS level_4,
    REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 5) AS level_5
    FROM cmt_person
    CONNECT BY manager_id = PRIOR id
    and level<=5
    The person table have more than a million records.
    Here I am getting the correct output but this query is taking a lot of time to run.
    I am looking at a SQL query without use of connect by to get the required output.
    To recreate the issue, you can use this query in HR schema of Oracle and use Employees table.
    Any help would be greatly appreciated.
    Thanks,
    Raghvendra

    I tried to rewrite the query without using regular expression and connect by. Here is the code:
    SELECT
    cmt_person.id ,
    cmt_person.fname as mgr1,
    case when cmt_person2.manager_id=cmt_person.id then (cmt_person2.fname ) end as mgr2,
    case when cmt_person3.manager_id=cmt_person2.id then (cmt_person3.fname ) end as mgr3,
    case when cmt_person4.manager_id=cmt_person3.id then cmt_person4.fname end as mgr4,
    case when cmt_person5.manager_id=cmt_person4.id then cmt_person5.fname end as mgr5
    FROM
    cmt_person,
    cmt_person cmt_person2,
    cmt_person cmt_person3,
    cmt_person cmt_person4,
    cmt_person cmt_person5
    WHERE
    cmt_person2.manager_id(+)=cmt_person.id and
    cmt_person3.manager_id(+)=cmt_person2.id and
    cmt_person4.manager_id(+)=cmt_person3.id and
    cmt_person5.manager_id(+)=cmt_person4.id
    order by 2,3,4
    I got following output:
    emplo000000000200100     Bobby     Khasha     Rahul     Rajesh     
    emplo000000000200099     Bobby     Khasha     Rahul     Ajay     
    emplo000000000200101     Bobby     Khasha     Rahul     Swati     
    emplo000000000200320     Bobby     Khasha     Rahul     Jinesh     
    emplo000000000201231     Bobby     Khasha     Test123          
    emplo000000000201230     Bobby     Khasha     User1_Domain          
    emplo000000000201227     Bobby     Khasha     User1_World          
    emplo000000000200104     Bobby     Khasha     Yitzik     Natalia     
    emplo000000000200103     Bobby     Khasha     Yitzik     Andrew     
    total 9 rows
    But this is partially correct output. I want output like this:
    emplo000000000200097          Bobby                    
    emplo000000000200087          Bobby     Khasha               
    emplo000000000200102          Bobby     Khasha     Yitzik          
    emplo000000000200103          Bobby     Khasha     Yitzik     Andrew     
    emplo000000000200104          Bobby     Khasha     Yitzik      Natalia     
    emplo000000000201227          Bobby     Khasha     User1_World          
    emplo000000000201231          Bobby     Khasha     Test123          
    emplo000000000201230          Bobby     Khasha     User1_Domain          
    emplo000000000200098          Bobby     Khasha     Rahul          
    emplo000000000200099          Bobby     Khasha     Rahul     Ajay     
    emplo000000000200100          Bobby     Khasha     Rahul     Rajesh     
    emplo000000000200320          Bobby     Khasha     Rahul     Jinesh     
    emplo000000000200101          Bobby     Khasha     Rahul     Swati     
    total 13 rows
    Do you know what I should do to get this output.
    Thanks,
    Raghvendra

  • How to write sql query to parse xml?

    Hi, I am new to this parsing xml. I need to extract value from xml using sql query. Please find the sample xml file. Kindly help me.
    <?xml version="1.0" encoding="UTF-8"?>
    <MaterialUpdates>
      <Materials>
         <SalesOrg>
           <ID>KAR12</ID>
         </SalesOrg>
    </Materials>
    </MaterialUpdates>
    I need to extratct ID value : /MaterialUpdates/Materials/SalesOrg/ID

    Thanks once again, it is working as per your suggestion. One last question .. how about in case of there is multiple nodes.
    Please find sample xml below:
    <?xml version="1.0" encoding="UTF-8"?>
    <MaterialUpdates>
      <Materials>
         <SalesOrg>
           <ID>KAR12</ID>
         </SalesOrg>
         <SalesOrg>
           <ID>KAR13</ID>
         </SalesOrg>
         <SalesOrg>
           <ID>KAR14</ID>
         </SalesOrg>
    </Materials>
    </MaterialUpdates>
    The above mentioned sql query works fine when there is one occurance of SalesOrg - ID, but fails when there is multiple occurance.

  • How to write a Query a table and the return result is the column name

    Hi All
    Pls advise how to write a query whereas the return result is the
    column name.
    I know there is describe <table_name>;
    Is there any other ways?
    Pls advise
    Tj
    Edited by: user600866 on Oct 14, 2008 12:13 AM

    Data Dictionary table user_tab_columns has all the column names. You can query that and get what ever you want.
    To get the column list of a table just query
    select *
      from user_tab_columns     
    where table_name = <your_table>Edited by: Karthick_Arp on Oct 14, 2008 12:18 AM

  • How to write this query ?

    how to write this query ?
    list the emp name who is working for the highest avg sal department.
    I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?
    the row_number version is like this
    select emp.* from emp ,
    select deptno, row_number() over (order by avg(sal) desc) r from emp
    group by deptno
    )e
    where e.r = 1
    and emp.deptno = e.deptno

    Hi,
    806540 wrote:
    how to write this query ?
    list the emp name who is working for the highest avg sal department.
    I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?ROW_NUMBER is just plain SQL, and has been since Oracle 8.1.
    ROW_NUMBER (or its close relative, RANK) is the simplest and most efficient way to solve this problem. Why not do this the right way?
    the row_number version is like this
    select emp.* from emp ,
    select deptno, row_number() over (order by avg(sal) desc) r from emp
    group by deptno
    )e
    where e.r = 1
    and emp.deptno = e.deptno
    If there happens to be a tie (that is, two or more departments have the same average sal, and it is the highest), then the query above will only arbitrarily treat one of them (no telling which one) as the highest. Change ROW_NUMBER to RANK to get all departments with a claim to having the highest average sal.
    You could use the ROWNUM pseudo-column instead of ROW_NUMBER, if all you want to do is avoid ROW_NUMBER.
    Without using ROW_NUMBER or RANK, there are lots of ways involving other analytic functions, such as AVG and MAX.
    If you really, really don't want to use analytic functions at all, you can do this:
    SELECT     *
    FROM     scott.emp
    WHERE     deptno     IN  (
                      SELECT       deptno
                      FROM       scott.emp
                      GROUP BY  deptno
                      HAVING       AVG (sal) =  (
                                                       SELECT    MAX (AVG (sal))
                                               FROM          scott.emp
                                               GROUP BY  deptno
    ;

  • Hi in my sql query i applied like condition (like '%TEST') but it is taking long time

    Hi in my sql query i applied like condition (like '%TEST') but it is taking long time. I applied indexes also,but still i'm facing the same problem. In my databse nearly 2,00,000 records their.

    Hi Manikandan
    Is there a difference in performance between running the query in BEx and WebI?
    have you aggregates in place on the BEx side of things?
    When you say its taking too long to open the report, have you a variable screen coming up for the report and is that what is taking the time or is it the report execution.
    With regards
    Gill

  • How to create sql query for item master with operator LIKE with variables?

    hi all,
    How to create sql query for item master with
    operator LIKE(Contains,Start With,End With) with variables using query generator in SAP B1 ?
    Jeyakanthan

    Hi Jeyakanthan,
    here is an example (put the like statement into the where field)
    SELECT T0.CardCode, T0.CardName FROM OITM T0 WHERE T0.CardName Like '%%test%%'
    The %% sign is a wildcard. If you need start with write 'test%%' and otherwise ends with '%%test'. For contains you need '%%test%%'. You also could combinate this statements like 'test%%abc%%'. This means starts with test and contains abc.
    Regards Steffen

  • How does a SQL query work

    Hi all...
    How does a SQL query basically work? Is there any pseudocode for it?
    How ro calculate the cost of query?
    Can we find out what would be the execution time for a query before it is executed?
    Thanks in advance.
    Ameya

    Hi Avinash,
    Look closely mate!!!
    You have done set autotrace on... meaning you are executing the statement while i am doing set autotrace traceonly explain meaning the statement would never get executed actually.. oracle optimizer will just select the desired plan and will show the output..
    You can even ESTIMATE how many rows the qeury will return before even executing the statement using the same above tech.. i will show you how:
    The explain plan gives this -- as long as you are using the CBO (cost based
    optimizer). Doesn't matter if you have 1 or 50 tables. The last part of the
    plan is the estimate output.
    consider:
    SQL> set autotrace traceonly explain
    SQL> select e1.*, e2.* from emp e1, emp e2;
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=15 Card=196 Bytes=12544)
    1 0 MERGE JOIN (CARTESIAN) (Cost=15 Card=196 Bytes=12544)
    2 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=448)
    3 1 SORT (JOIN) (Cost=14 Card=14 Bytes=448)
    4 3 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=448)
    196 = 14 * 14 -- thats right....
    SQL> select emp.ename, mgr.ename from
    2 emp, emp mgr
    3 where emp.mgr = mgr.empno;
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=5 Card=13 Bytes=208)
    1 0 MERGE JOIN (Cost=5 Card=13 Bytes=208)
    2 1 SORT (JOIN) (Cost=3 Card=14 Bytes=112)
    3 2 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=112)
    4 1 SORT (JOIN) (Cost=3 Card=14 Bytes=112)
    5 4 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=112)
    Here is estimated that about 13 rows would be returned...
    SQL> select count(*) from emp;
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1)
    1 0 SORT (AGGREGATE)
    2 1 INDEX (FULL SCAN) OF 'EMP_PK' (UNIQUE) (Cost=1 Card=14)
    one row...
    SQL> select ename, dname
    2 from emp, dept where emp.deptno = dept.deptno;
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=5 Card=14 Bytes=252)
    1 0 NESTED LOOPS (Cost=5 Card=14 Bytes=252)
    2 1 TABLE ACCESS (FULL) OF 'DEPT' (Cost=1 Card=4 Bytes=44)
    3 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=98)
    14 rows...
    1 select ename, dname
    2* from emp, dept where emp.deptno = dept.deptno and emp.ename like '%A%'
    SQL> /
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=18)
    1 0 NESTED LOOPS (Cost=2 Card=1 Bytes=18)
    2 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=1 Bytes=7)
    3 1 TABLE ACCESS (BY INDEX ROWID) OF 'DEPT' (Cost=1 Card=4 Bytes=44)
    4 3 INDEX (RANGE SCAN) OF 'DEPT_IDX' (NON-UNIQUE)
    1 rows.....
    and this was all done without executing the query. See EXPLAIN plan and the
    PLAN_TABLE.

Maybe you are looking for