Group by Clause displays all values search

Hello friends

I have a simple table with columns named Date, reason, product and County and the sample data are shown below.

==========================
Date reason product count
==========================
08/06/2012 raison1 home 1
08/07/2012 raison2 motor 1
08/08/2012 raison1 home 1
08/09/2012 Reason3 Home 2
08/10/2012 raison1 home 1
08/06/2012 Reason5 home 1
===========================

Altogether, I have 5 values of research through Reason5 raison1 reason, but few of them understand the table above.
I would like to diplay result per day and I will quote an example of August 6, I want to display the result below, i.e. show all reason 5 search and assign zero count if there are no records for this day there.

=====================================
DATE REASON HOME_COUNT MOTOR_COUNT
=====================================
08/06/2012 raison1 1-0
08/06/2012 0 1 raison2
08/06/2012 Reason3 0 0
08/06/2012 Reason4 0 0
08/06/2012 Reason5 1 0
=====================================


If we write group by clause, missing for reasons such as Reason3 Reason4 will not appear in the result set.
And I tried to write several UNION ALL queries to get the above result that works very well, but if it 100 search values, I don't want to write 100 Union queries.
Please let me know if you have analytical functions to display the results of the end?

Thank you
Murali.

Published by: Nathalie b on August 19, 2012 20:17

If you followed relational design, you should lookup table. If you do not, you need to create a. In addition, date is reserved word, and the County is a key word, to not use as column names. Then, use the outer join:

SQL> create table tbl as
  2  select to_date('06/08/2012','dd/mm/yyyy') dt,'Reason1' reason,'Home' product,1 qty from dual union all
  3  select to_date('07/08/2012','dd/mm/yyyy'),'Reason2','Motor',1 from dual union all
  4  select to_date('08/08/2012','dd/mm/yyyy'),'Reason1','Home',1 from dual union all
  5  select to_date('09/08/2012','dd/mm/yyyy'),'Reason3','Home',2 from dual union all
  6  select to_date('10/08/2012','dd/mm/yyyy'),'Reason1','Home',1 from dual union all
  7  select to_date('06/08/2012','dd/mm/yyyy'),'Reason5','Home',1 from dual
  8  /

Table created.

SQL> create table reason_list as
  2  select  'Reason' || level reason from dual connect by level <= 5
  3  /

Table created.

SQL> select  d.dt,
  2          r.reason,
  3          nvl(
  4              sum(
  5                  case product
  6                    when 'Home' then qty
  7                  end
  8                 ),
  9              0
 10             ) home_qty,
 11          nvl(
 12              sum(
 13                  case product
 14                    when 'Motor' then qty
 15                  end
 16                 ),
 17              0
 18             ) motor_qty
 19    from      (
 20               select  min_dt + level - 1 dt
 21                 from  (
 22                        select  min(dt) min_dt,
 23                                max(dt) max_dt
 24                          from  tbl
 25                       )
 26                 connect by level <= max_dt - min_dt + 1
 27              ) d
 28          cross join
 29              reason_list r
 30          left join
 31              tbl t
 32            on (
 33                    t.dt = d.dt
 34                and
 35                    t.reason = r.reason
 36               )
 37    group by d.dt,
 38             r.reason
 39    order by d.dt,
 40             r.reason
 41  /

DT        REASON                                           HOME_QTY  MOTOR_QTY
--------- ---------------------------------------------- ---------- ----------
06-AUG-12 Reason1                                                 1          0
06-AUG-12 Reason2                                                 0          0
06-AUG-12 Reason3                                                 0          0
06-AUG-12 Reason4                                                 0          0
06-AUG-12 Reason5                                                 1          0
07-AUG-12 Reason1                                                 0          0
07-AUG-12 Reason2                                                 0          1
07-AUG-12 Reason3                                                 0          0
07-AUG-12 Reason4                                                 0          0
07-AUG-12 Reason5                                                 0          0
08-AUG-12 Reason1                                                 1          0

DT        REASON                                           HOME_QTY  MOTOR_QTY
--------- ---------------------------------------------- ---------- ----------
08-AUG-12 Reason2                                                 0          0
08-AUG-12 Reason3                                                 0          0
08-AUG-12 Reason4                                                 0          0
08-AUG-12 Reason5                                                 0          0
09-AUG-12 Reason1                                                 0          0
09-AUG-12 Reason2                                                 0          0
09-AUG-12 Reason3                                                 2          0
09-AUG-12 Reason4                                                 0          0
09-AUG-12 Reason5                                                 0          0
10-AUG-12 Reason1                                                 1          0
10-AUG-12 Reason2                                                 0          0

DT        REASON                                           HOME_QTY  MOTOR_QTY
--------- ---------------------------------------------- ---------- ----------
10-AUG-12 Reason3                                                 0          0
10-AUG-12 Reason4                                                 0          0
10-AUG-12 Reason5                                                 0          0

25 rows selected.

SQL> 

SY.

Tags: Database

Similar Questions

  • Setting: display all values currently available - disabled

    Hello

    I want to show the value of parameter of an another table/folder.

    Such as:

    I created a spreadsheet of the EMP and DEPT/Table of records:

    SELECT E.Emp_Name, D.Dept_Name

    FROM Emp E, Dept. D

    WHERE E.Dept_ID = D.Dept_ID

    Question:

    1. How can I view Dept_ID (s) available in the EMP Table as an input parameter?
    2. If I can show Dept_Name to select between but Dept_ID and take as an input parameter?

    When I try to create the D.Dept_ID parameter 'Display all values currently available' option shows disabled and cannot be selected.

    Help, please.

    Thanks in advance.

    Manny

    I found the answer: How to insert the list of values (LOV) in Discoverer Desktop

    Thank you

  • Display all values that bad

    I have a calendar with this SQL query to get all action items. The last condition is on the last line that pulls up all projects related to this activity of monitoring based on a value in the page. The value is stored in the field 'P133_PROJECT_CAT '.

    Select EBA_PROJ_STATUS_AIS.ID as ID,

    EBA_PROJ_STATUS_AIS. Project as project,

    EBA_PROJ_STATUS_AIS.MILESTONE_ID as MILESTONE_ID,

    EBA_PROJ_STATUS_AIS. ACTION as ACTION,

    EBA_PROJ_STATUS_AIS. End_date as end_date,

    EBA_PROJ_STATUS_AIS. ACTION_STATUS as ACTION_STATUS,

    EBA_PROJ_STATUS_AIS. CREATED as CREATION,

    EBA_PROJ_STATUS_AIS. CREATED_BY as CREATED_BY,

    EBA_PROJ_STATUS. PROJECT within the PROJECT,

    EBA_PROJ_STATUS_AIS_TYPES. AI_TYPE as AI_TYPE,

    EBA_PROJ_STATUS_MS.MILESTONE_NAME as MILESTONE_NAME,

    EBA_PROJ_STATUS.CAT_ID as CAT_ID

    of EBA_PROJ_STATUS_MS EBA_PROJ_STATUS_MS,.

    EBA_PROJ_STATUS_AIS_TYPES EBA_PROJ_STATUS_AIS_TYPES,

    EBA_PROJ_STATUS EBA_PROJ_STATUS,

    EBA_PROJ_STATUS_AIS EBA_PROJ_STATUS_AIS

    where EBA_PROJ_STATUS_AIS. PROJECT = EBA_PROJ_STATUS.ID

    and EBA_PROJ_STATUS_AIS. TYPE_ID = EBA_PROJ_STATUS_AIS_TYPES.ID

    and EBA_PROJ_STATUS_AIS.MILESTONE_ID = EBA_PROJ_STATUS_MS.ID

    and EBA_PROJ_STATUS.CAT_ID =: P133_PROJECT_CAT

    I have the setup of "P133_PROJECT_CAT" of field as a field on the page where, when a value is selected, it works very well and shows all the items/projects action for this page. However, if I select the value '- all categories -' it must pass a 0 that must be null and see all categories.

    Question: what needs this final line (and EBA_PROJ_STATUS.CAT_ID =: P133_PROJECT_CAT) look like so that "0 = all values"?

    and (EBA_PROJ_STATUS.CAT_ID =: P133_PROJECT_CAT or : P133_PROJECT_CAT = 0)

  • Control the Append KM, Group by clause

    Hi all

    I am using the max function in interface (KM: control IKM append), in the map apart from the source columns in the data store that I use few variables ODI (say LD_ID = 200). Sort of variable LD_ID is being picked up by the KM in the group clause, why Labour fails with the error
    GROUP BY position 200 isn't in the target list. I understand the error but do not know why this variable in the grop is recovered by the clause. The substituition method that I see in the KM is odiRef.getGrpBy)

    Can someone help me with this please?

    Thank you
    Ashok

    Hey Ashok,

    When using (a) or several global functions, like SUM, ODI add one GROUP BY clause with all other columns in it.
    You can try to move the mapping of the LD_ID on the target and see if it works (it depends on the architecture you).

    Kind regards
    JeromeFr

  • If the default as invited "All values" in a Variable of presentation

    Hello Experts,
    is it possible to set the default value for filtering on a Variable presentation for all the choices? Because when you select all the choices, I get no results.

    Thank you
    Concerning

    Detection of all choice of dashboard invites you into the answers
    http://108obiee.blogspot.com/2009/04/detecting-all-choices-from-dashboard.html

    See the link above, it also describes how to make a filter to display all values when you use the variable to the presentation and all the choices.

    Concerning
    Goran
    http://108obiee.blogspot.com

  • count all values with a special WHERE clause in a select group?

    Hello

    I have the following table 1:

    code, month, value
    * 1,1,40 *.
    * 1,2,50 *.
    * 1,3,0 *.
    * 1,4,0 *.
    * 1,5,20 *.
    * 1,6,30 *.
    * 1,7,30 *.
    * 1,8,30 *.
    * 1,9,20 *.
    * 1,10,20 *.
    * 1,11,0 *.
    * 1,12,0 *.

    * 2,1,10 *.
    * 2,2,10 *.
    * 2,3,20 *.
    * 2,4,20 *.
    * 2,5,20 *.
    * 2,6,30 *.
    * 2,7,40 *.
    * 2,8,50 *.
    * 2,9,20 *.
    * 2,10,20 *.
    * 2,11,20 *.
    * 2,12,20 *.


    This is a table with 3 columns, first column is a code, second is the number of months, third, one is a value.
    Now, I want to select records for each code. For example, all records for the code = 1.
    I want to count how many values = 0 for this code = 1. After this count, I want to update the value with the number 0.

    For my example:
    Code 1, there are 4 fields with the value 0. So, I want to update all values from code 1 to 4.
    The second code = 2 there is no value = 0. So, I want to update the values of code2 to 0.

    This should be the result:
    code, month, value
    * 1,1,4 *.
    * 1,2,4 *.
    * 1,3,4 *.
    * 1,4,4 *.
    * 1,5,4 *.
    * 1,6,4 *.
    * 1,7,4 *.
    * 1,8,4 *.
    * 1,9,4 *.
    * 1,10,4 *.
    * 1,11,4 *.
    * 1,12,4 *.

    * 2,1,0 *.
    * 2,2,0 *.
    * 2,3,0 *.
    * 2,4,0 *.
    * 2,5,0 *.
    * 2,6,0 *.
    * 2,7,0 *.
    * 2,8,0 *.
    * 2,9,0 *.
    * 2,10,0 *.
    * 2,11,0 *.
    * 2,12,0 *.


    My question is:
    Is it possible in oracle to count in a select (or in an insert/update statement) all values = 0 for a group (in this example, named code) and make an update in the same statement for this group?

    I hope someone can give me a hint, if possible?

    Thank you very much.
    Best regards


    Tim

    something like that? NOT TESTED *.

    Setting a DAY Table1 a SET A.Value = (SELECT COUNT (B.ROWID) FROM Table1 B WHERE A.Code = B.Code AND B.Value = 0);

  • Reg - search form for a VO in group by clause

    Hello
    I have a bar chart that displays data based on the query below.

    SELECT THE STATE. BATCH AS STATUSID, STATUS. STATUS, COUNTY (SR. SERVICEREQUEST_ID) AS SRCOUNT
    OF SR, SERVICEREQUESTSTATUS STATE SERVICE_REQUEST
    WHERE SR. BATCH = STATUS. BATCH
    GROUP STATUS. BATCH, STATUS. STATUS, SR. BATCH

    It displays the number of SRs against a particular status.
    Now, I need to add a search form to this graph with the customer and the date range.
    So you need to add the following line to the place where clause.
    SR. "CUSTOMER_ID =: customerId AND SR. "BETWEEN REQUESTED_ON: fromDate and: to this day.
    But the SR columns. CUSTOMER_ID, SR. REQUESTED_ON also needs to be added to the select clause to create criteria for the search panel view.
    The two columns should also be added to the group by clause if you want to add in the select clause.
    This would not produce the expected results.
    How to create a search with the criteria only form in the where clause. Help, please.


    With respect,
    Guna

    [Url http://docs.oracle.com/cd/E16162_01/apirefs.1112/e17483/oracle/jbo/server/ViewObjectImpl.html] ViewObjectImpl has methods to do this programmatically (setQuery, defineNamedWhereClauseParam, setNamedWhereClauseParam) that you can use to manipulate the query variables bind expected and the values of the lie.

    John

  • Windows Explorer - list: how to set the default value to display all the contents of the folder 'List' not 'Tiles '.

    Using the Windows Explorer of Windows 7, how I have by default set to display all the contents of the folder 'List' not 'Tiles '.  I want set a default value for the new folders and edit globally all folders that I have.

    Thank you, Steve

    Hi Steve Menker,.

    Visit the links that measures to work with files and folders in Windows Explorer below:

    1. working with files and folders:http://windows.microsoft.com/en-US/windows7/Working-with-files-and-folders

    2. change the folder options:http://windows.microsoft.com/en-US/windows7/Change-folder-options

    3. organize, sort, or group your files:http://windows.microsoft.com/en-US/windows7/Arrange-sort-or-group-your-files

    With regard to:

    Samhrutha G S - Microsoft technical support.

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

  • Chart display problems - cut off the end or shows not all values

    I have a file I/O reads database table where I can choose a date range to view history, and I can't seem to format correctly.

    If I set the width of the graph large enough to show all values, it cuts the right part.

    Width 1200, the value date of end 10/01/2014, shows only until September 24:

    fileio.jpg

    Width set at 1700, will now interrupt the rightmost in the values:

    fileio1.jpg

    Obviously, I want the best of both worlds - when the date range is selected, it shows all the values and fits in the chart area.

    Any ideas?

    Create a diagram with scrolling.

    Under the item add something like:

  • Concatenation of data with the GROUP BY clause

    Hi again!

    Following my previous thread...
    Re: Need help with RANK() on data ZERO

    I tried to apply the GROUP BY clause instead of preforming my query with RANK() to manage records NULL... I have a scenario where I also need to concatenate data from several lines.

    CREATE TABLE T_EMP (NUMBER OF EMP_NO, NAME VARCHAR2 (20));
    INSERT INTO T_EMP VALUES (1001, 'MARK');
    INSERT INTO T_EMP VALUES (1002, 'DAVID');
    INSERT INTO T_EMP VALUES (1003, "SHAUN");
    INSERT INTO T_EMP VALUES (1004, "JILL");

    CREATE TABLE T_EMP_DEPT (NUMBER OF EMP_NO, DEPT_NO NUMBER);
    INSERT INTO T_EMP_DEPT VALUES (1001, 10);
    INSERT INTO T_EMP_DEPT VALUES (1001, 20);
    INSERT INTO T_EMP_DEPT VALUES (1002, 10);
    INSERT INTO T_EMP_DEPT VALUES (1002, 20);
    INSERT INTO T_EMP_DEPT VALUES (1002, 30);
    INSERT INTO T_EMP_DEPT VALUES (1003, 20);
    INSERT INTO T_EMP_DEPT VALUES (1003, 30);
    INSERT INTO T_EMP_DEPT VALUES (1004, 10);

    CREATE TABLE T_EMP_VISITS (NUMBER OF EMP_NO, DEPT_NO NUMBER, VISITED DATE);
    INSERT INTO T_EMP_VISITS VALUES (1001, 10, 1 JAN 2009');
    INSERT INTO T_EMP_VISITS VALUES (1002, 10, 1 JAN 2009');
    INSERT INTO T_EMP_VISITS VALUES (1002, 30, 11 APR 2009');
    INSERT INTO T_EMP_VISITS VALUES (1003, 20, 3 MAY 2009');
    INSERT INTO T_EMP_VISITS VALUES (1003, 30: 14 FEB 2009');
    COMMIT;

    I have a T_EMP master table that stores the name and number of the emp. Each emp is required to visit some departments. This mapping is stored in the T_EMP_DEPT table. An employee can visit one or more departments. T_EMP_VISITS table stores the dates where the employee visited the services required. I need to view the report which should show when an employee all completed visits, which is the maximum date when it finished to visit all departments. If he did not visit any of the report should display date max, otherwise NULL. I was able to do using GROUP BY such proposed by Salim, but how do I show a list separated by commas of the services required for an employee in the same query.

    SELECT
    EMP_NO,
    NAME,
    MAX (DEPT_NO) KEEP (DENSE_RANK LAST ORDER BY VISITED) MAX_DEPT_NO,.
    MAX (VISITED) KEEP (DENSE_RANK LAST ORDER PER VISIT) VISITS_COMP
    DE)
    SELECT
    T_EMP. EMP_NO,
    NAME,
    T_EMP_DEPT. DEPT_NO,
    VISITED
    OF T_EMP
    LEFT OUTER JOIN T_EMP_DEPT
    ON T_EMP. EMP_NO = T_EMP_DEPT. EMP_NO
    LEFT OUTER JOIN T_EMP_VISITS
    ON T_EMP_DEPT. EMP_NO = T_EMP_VISITS. EMP_NO
    AND T_EMP_DEPT. DEPT_NO = T_EMP_VISITS. DEPT_NO)
    GROUP EMP_NO, NAME;

    Output
    EMP_NO NAME MAX_DEPT_NO VISITS_COMP
    1001 MARK 20
    1002 DAVID 20
    1003 SHAUN 20 3 MAY 09
    JILL 1004

    Power required
    EMP_NO NAME REQ_DEPTS MAX_DEPT_NO VISITS_COMP
    1001 MARC 20 10.20
    1002 DAVID 10,20,30 20
    1003 SHAUN 20,30 20 3 MAY 09
    JILL 10 1004

    Can we do this in a single query?

    Hello

    user512647 wrote:
    ... Sanjay
    The query you provided that stragg() use seems to work but my requirement is not in the result set. I don't know how to use stragg with
    MAX (DEPT_NO) KEEP (DENSE_RANK LAST ORDER BY VISITED) MAX_DEPT_NO,.
    MAX (VISITED) KEEP (DENSE_RANK LAST ORDER PER VISIT) VISITS_COMP
    I need more, these two columns these gives me the date when they have completed all visits. If they missed any Department then the result must be NULL in the VISITS_COMP field.

    Just add them to the SELECT clause:

    SELECT    t_emp.emp_no,
           name,
           STRAGG (t_emp_dept.dept_no)     AS deptno,
           MAX (t_emp_dept.dept_no) KEEP (DENSE_RANK LAST ORDER BY visited)
                                      AS max_dept_no,
           MAX (visited)                      AS visits_comp
    FROM             t_emp
    LEFT OUTER JOIN      t_emp_dept     ON   t_emp.emp_no     = t_emp_dept.emp_no
    LEFT OUTER JOIN      t_emp_visits     ON   t_emp_dept.emp_no     = t_emp_visits.emp_no
                                 AND  t_emp_dept.dept_no = t_emp_visits.dept_no
    GROUP BY  t_emp.emp_no
    ,            name
    ;
    

    The column called visit_comp is simply the last visited, regardless of how the employee visited departments.
    If you want to have the NULL value if the employee has not yet visited all 3 departments:

    ...       CASE
              WHEN  COUNT (DISTINCT t_emp_dept.dept_no) = 3
              THEN  MAX (visited)
           END                    AS visits_comp
    

    The 'magic number' 3 is the total number of departments.
    If you want to understand the correct value of that at the time of the execution of the query, replace the code literal 3 hard with a scalar subquery.

    Note that 'KEEP MAX (x) (DENSE_RANK OVER LAST SERVICE BY x)' (where the exact same column is used as an argument and that the ORDER BY column) is just "MAX (x)".

  • Doubts formidable wrt "select", "join" and "group by" clause to calm a while...

    Dear all,

    Kindly advise on below 2 doubts which have been daunting for quiet some time. The bottom has forced our requests to take alternative routes to receive solutions.

    Doubt 1) is it possible for us to use 'select (*)' with a group and a join clause, if we have at least 2/3 tables with minimum 15-20 columns... given the constraint of having to add all the names column in the group by clause.

    Question 2) is it possible to use "select" (*) with the exception of a single column of the table?
    That is to say, I hate the output of a statement select (*) to have the entire field except a field in a table

    Ask your advisor for the same thing. Excuse me because I have no examples to illustrate this point in time. A successful idea will do.

    Thank you and best regards,

    Séverine Suresh

    Hey, Sebastian,

    Séverine Suresh - 3035408 wrote:

    Dear all,

    Kindly advise on below 2 doubts which have been daunting for quiet some time. The bottom has forced our requests to take alternative routes to receive solutions.

    Doubt 1) is it possible for us to use 'select (*)' with a group and a join clause, if we have at least 2/3 tables with minimum 15-20 columns... given the constraint of having to add all the names column in the group by clause.

    Sure.  If you had problems, you did it wrong.  Your postal code, examples of data (CREATE TABLE and INSERT statements) and the results desired from these data.

    Check out the Forum FAQ: Re: 2. How can I ask a question on the forums?

    Normally, you don't want to ' SELECT *...» "in a join.  Are most joins are equijoins, ' SELECT *...» "produced 2 copies of / columns used for Assembly.  In addition, many tables have columns (for example, modified_date) that are not necessary in most applications.

    All columns must be included explicitly in a GROUP BY clause. You cannot use * it.

    You can not have parentheses around the * in ' SELECT *...» ».  You might think 'SELECT COUNT (*) '

    Question 2) is it possible to use "select" (*) with the exception of a single column of the table?
    That is to say, I hate the output of a statement select (*) to have the entire field except a field in a table

    ...

    No.; If you don't want a particular column in the result set, you cannot use "SELECT *...» ».

    Your front end can have so as not to display a column in the result set.  For example, in SQL * more:

    COLUMN modified_date NOPRINT

    means that you won't see any column called modified_date.

  • -SQL - GROUP BY clause: fields of nonaggregate mandate

    Hello

    I study data (especially data recovery) and found something interesting.

    When you use an aggregate function in the SELECT clause, it is mandatory to have all fields that are not aggregated in the SELECT clause to be there in the GROUP BY clause.
    For example,.

    SELECT dept_no, Salary
    The EMPLOYEE
    GROUP BY dept_no;

    The SQL above works fine.
    But what happens if the user forgets the dept_no in the GROUP BY clause or the clause GROUP BY itself is missing?
    Certainly, it is a mistake.

    Why this error is not handled by the database. I mean, the database must be smart/pretty smart to add the GROUP BY clause by itself. So let's assume that, if I miss the GROUP BY clause or miss a field no aggregated from the SELECT clause when I get at least an aggregate function on a field with at least a no aggregated field in the SELECT clause, the database should check the GROUP BY clause at compile time and add mandate missed the fields in the GROUP BY clause.

    Example,

    SQL1:_
    SELECT dept_no, Salary
    The EMPLOYEE
    GROUP BY dept_no;

    SQL2:_
    SELECT dept_no, Salary
    The EMPLOYEE;

    Here, the SQL1 and SQL2, both should give me same output without error.

    I can't understand why this is handled?

    Hello

    998478 wrote:
    ... If we mix the aggregated and non-aggregated values, then there must be a GROUP BY clause that contains all non-aggregated values. Why this is handled by the database/compiler itself?

    It IS managed by the compiler itself. The compiler manages to trigger an error. The compiler has no way of knowing if you want to remove something from the SELECT clause, or add something to the GROUP BY clause, or not to use the aggregate functions or use several aggregate functions, or a combination of the above. If the compiler re-writes your code and none of these things done automatically, it would be wrong more often that he was right, and you would (rightly) complain about his behavior.

    For example, it is clearly wrong:

    SELECT    deptno
    ,       job
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ;
    

    What is the right way to fix it?

    1. remove something from the SELECT clause

    SELECT    deptno
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ;
    

    2. add something to the GROUP BY clause

    SELECT    deptno
    ,       job
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ,         job
    ;
    

    3. do not use aggregate functions

    SELECT    deptno
    ,       job
    ,       sal
    FROM       scott.emp
    ;
    

    4. use several aggregate functions

    SELECT    deptno
    ,       MIN (job)
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ;
    

    What are all the options, either. For example, the correct solution would be to use analytical functions instead of aggregate functions.
    How can anyone tell which of them is right? They all have the right answer for some problem.

    Moreover, by saying that everying in the SELECT clause must be an aggregate or in the GROUP BY clause is a bit oversimplified.
    Fuller, here's the ABC of GROUP BY:
    When you use a GROUP BY clause or in an aggregate function, then all in the SELECT clause must be:
    (A) a ggregate function,
    (B) one of the expressions "group By."
    (C) adding to C, or
    (D) something that Depends on the foregoing. (For example, if you "GROUP BY TRUNC (dt)", you can SELECT "TO_CHAR (TRUNC (dt), 'Mon - DD')").

    Published by: Frank Kulash on April 13, 2013 13:44
    Additional code examples.

  • Procedure is not reading/returns all values

    The SP below reads in business_date as an input parameter.
    The procedure should calculate this value MAX in a range
    (ln_business_date) and this value must be used throughout the rest of the
    operating mode.
      SELECT MAX(business_date
          INTO ln_business_date
          FROM proof;
    The procedure returns the name of 'missing_tbl_name' of the INTO clause as the OUT parameter
    However, the performance of the SP_MONTHLY_ASSET procedure below I don't get not all values. The procedure
    Compiles without error but I think that my procedure is bad work and not read or return values
    correctly.

    Any help is greatly appreciated.

    CREATE OR REPLACE PROCEDURE SP_MONTHLY_ASSET(
        business_dt_num IN NUMBER DEFAULT NULL, 
        missing_tbl_name OUT NOCOPY VARCHAR2
    )
    IS
       ln_business_dt_num NUMBER;
       bad_date_value EXCEPTION;
    BEGIN
       IF business_dt_num < 0 
       THEN
          RAISE bad_date_value;
       ELSE
        SELECT MAX(business_dt_num)
          INTO ln_business_dt_num 
          FROM sasor.dp_ca_proof@psasr
    
          SELECT data_table_name
          INTO missing_tbl_name 
          FROM ( 
             SELECT whatever 
             FROM tablename
             WHERE some_condition MINUS 
             ( 
             SELECT whatever
             FROM secondtablename
             WHERE business_dt_num = ln_business_dt_num 
              UNION 
             SELECT whatever 
             FROM tablename
             WHERE business_dt_num = to_number( substr('&datenum',1,6) || '01' ) 
             )
               );
       end if;
    EXCEPTION
        WHEN bad_date_value 
       THEN
          raise_application_error ( -10001, 'Incorrect date ...');
    END;

    There is an exception no_data_found because there was no data found. This subquery:

             SELECT UPPER(data_table_name) data_table_name
             FROM filespec
             WHERE data_table_name IN ('SSBBALIN', 'LPLAPRA',
             'DSTASSET', 'EDJBALIN', 'APLASSET', 'HARTFORDDC',
             ' WLFBALIN', ' MSTBALIN', ' STFAPRA', ' RJABALIN',
             ' JYMAPRA', ' RBCBALIN', ' UBSBALIN',
             ' MERRILL_UMA_MAN', ' MERRILL_MAA_MAN') MINUS (
             SELECT UPPER(fus_data_table_name)
             FROM dp
             WHERE business_date = ln_business_date
             AND fus_data_table_name IN (' SSBBALIN',' LPLAPRA',
             ' MGNAPRA', ' DSTASSET', ' EDJBALIN', ' APLASSET',
             ' HARTFORDDC',  ' MSTBALIN', ' STFAPRA',
             ' RJABALIN', ' JYMAPRA', ' RBCBALIN', ' UBSBALIN',
             ' MERRILL_UMA_MAN', ' MERRILL_MAA_MAN')
             GROUP BY UPPER(fus_data_table_name) UNION
             SELECT UPPER(fus_data_table_name)
             FROM dp
             WHERE business_date = to_number( substr('201111',1,6) || '01' )
             AND fus_data_table_name = 'HARTFORDDC'
             GROUP BY UPPER(fus_data_table_name) )
    

    Nothing found. If you want to return just ' lack of support: "If the subquery is empty when what you need is:

    BEGIN
      SELECT 'Missing Load : ' || data_table_name
           INTO missing_tbl_name
          FROM (
             SELECT UPPER(data_table_name) data_table_name
             FROM filespec
             WHERE data_table_name IN ('SSBBALIN', 'LPLAPRA',
             'DSTASSET', 'EDJBALIN', 'APLASSET', 'HARTFORDDC',
             ' WLFBALIN', ' MSTBALIN', ' STFAPRA', ' RJABALIN',
             ' JYMAPRA', ' RBCBALIN', ' UBSBALIN',
             ' MERRILL_UMA_MAN', ' MERRILL_MAA_MAN') MINUS (
             SELECT UPPER(fus_data_table_name)
             FROM dp
             WHERE business_date = ln_business_date
             AND fus_data_table_name IN (' SSBBALIN',' LPLAPRA',
             ' MGNAPRA', ' DSTASSET', ' EDJBALIN', ' APLASSET',
             ' HARTFORDDC',  ' MSTBALIN', ' STFAPRA',
             ' RJABALIN', ' JYMAPRA', ' RBCBALIN', ' UBSBALIN',
             ' MERRILL_UMA_MAN', ' MERRILL_MAA_MAN')
             GROUP BY UPPER(fus_data_table_name) UNION
             SELECT UPPER(fus_data_table_name)
             FROM dp
             WHERE business_date = to_number( substr('201111',1,6) || '01' )
             AND fus_data_table_name = 'HARTFORDDC'
             GROUP BY UPPER(fus_data_table_name) ));
    EXCEPTION
      WHEN no_data_found THEN
        missing_tbl_name := 'Missing Load : ';
    END;
    
  • Not a GROUP BY clause error

    Hi all

    Can someone tell me what is wrong with the query below? I get an exception is NOT a GROUP OF CLAUSE:
    SELECT
            LAST_DAY((xxx.timevalue)) as  SUMMARYDATE
          , xxx.acct AS ACCTNUM
          , xxx.referenceid AS SEARCHTYPE
           , CASE WHEN NVL(xxx.PRIMARYSRCH_IND,'N') = 'Y' THEN COUNT(xxx.searchid) ELSE 0 END  Cnt_PrimarySearches
                   , CASE WHEN NVL(xxx.primarysrch_ind,'N') = 'N' THEN COUNT(xxx.Searchid) ELSE 0 END  Cnt_SecondarySearches
         
          FROM XXX_Tablename xxx
          WHERE
           xxx.referenceid NOT IN ('Alert','Search','Collection')
          GROUP BY
      LAST_DAY((xxx.timevalue))  
      , xxx.acctnum
      , xxx.referenceid 

    Hello

    Try this

    SELECT
            LAST_DAY((xxx.timevalue)) as  SUMMARYDATE
          , xxx.acct AS ACCTNUM
          , xxx.referenceid AS SEARCHTYPE
           ,COUNT( CASE WHEN NVL(xxx.PRIMARYSRCH_IND,'N') = 'Y' THEN (xxx.searchid) ELSE 0 END  Cnt_PrimarySearches)
                   , COUNT(CASE WHEN NVL(xxx.primarysrch_ind,'N') = 'N' THEN (xxx.Searchid) ELSE 0 END) Cnt_SecondarySearches
    
          FROM XXX_Tablename xxx
          WHERE
           xxx.referenceid NOT IN ('Alert','Search','Collection')
          GROUP BY
      LAST_DAY((xxx.timevalue))
      , xxx.acctnum
      , xxx.referenceid
    

    Not tested

  • Group by to ignore the values null

    Hi all
    Oracle version 10 g 2
    Consider the scenario

    Table: point
    Columns: Itm_id, Itm_type, appl_y_n (VALUES VALID 'Y', ' n, NULL)

    I HAVE FOLLOWING ITEM TYPES.

    TYP1
    TYP2
    TYP3

    In this appl_y_n is applicable only to the item type typ1 and it can contain y, n, or null.
    Types of remaining elements of this field may contain any value, including null, but I want to ignore these values.
    ITM_ID     ITM_TYPE     APPL_Y_N
    1     TYP1     NULL
    2     TYP1     Y
    3     TYP1     N
    4     TYP2     NULL
    5     TYP2      NULL
    6     TYP3     NULL
    7     TYP3     NULL
    now I need a group of clause which must ignore values null to appl_y_n only for TYP1

    on top of the sample output must be
    ITM_TYPE     APPL_Y_N     CNT
    TYP1     YES     1
    TYP1     NO     1
    TYP2     NULL     2
    TYP3     NULL     2
    Query, that I use for this:

    Select ITEM_TYPE, COUNT (ITM_ID), decode (itm_type, 'TYP1', DECODE(APPL_Y_N,'Y','Yes','N','No')) APPL_Y_N
    of the order of the day
    Item_type group, decode (itm_type, 'TYP1', DECODE(APPL_Y_N,'Y','Yes','N','No'))

    But he's considering typ1 as null values.

    Please give me a solution that ignores null values

    Thanks in advance
    NM

    Hello

    Check the below. It will be useful.

    SQL >
    SQL > WITH t AS)
    2 SELECT 2 ITM_ID, 'TYP1' ITM_TYPE, 'Y' APPL_Y_N FROM dual UNION ALL
    3. SELECT 3 ITM_ID, 'TYP1' ITM_TYPE, "n" APPL_Y_N FROM dual UNION ALL
    4. SELECT 1 ITM_ID, 'TYP1' ITM_TYPE, APPL_Y_N NULL FROM dual UNION ALL
    5. SELECT 4 ITM_ID, "TYP2' ITM_TYPE, APPL_Y_N NULL FROM dual UNION ALL
    6. SELECT 5 ITM_ID, "TYP2' ITM_TYPE, APPL_Y_N NULL FROM dual UNION ALL
    7. SELECT 6 ITM_ID, "TYP3' ITM_TYPE, APPL_Y_N NULL FROM dual UNION ALL
    8. SELECT 7 ITM_ID, "TYP3' ITM_TYPE, APPL_Y_N double NULL)
    9 SELECT itm_type,
    10 DECODE (itm_type, 'TYP1', DECODE (appl_y_n, 'Y', 'Yes', "n","no")) appl_y_n,.
    11 COUNT (itm_id)
    12 FROM t
    13 WHERE NOT (itm_type = 'TYP1' AND APPL_Y_N IS NULL)
    14 group of itm_type,
    15 decode (itm_type, 'TYP1', DECODE (APPL_Y_N, 'Y', 'Yes', "n","no"))
    16 ORDER BY itm_type
    17.

    ITM_TYPE APPL_Y_N COUNT (ITM_ID)
    -------- -------- -------------
    TYP1 NO. 1
    TYP1 Yes 1
    TYP2 2
    TYP3 2

    SQL >

    Rgds
    Ameya

Maybe you are looking for