Select multi column group by one column

Dear SQL Experts,
I have one question related to pl/sql.
I need to do below query. Please advice proper way.
select code, firstname, lastname
from customer
group by code
But group by expecting firstname and lastname.
Please advice
Thank you,
Erdenebayar

Erdenebayar wrote:
I have one question related to pl/sql.You have posted SQL code. Not PL/SQL code.
I need to do below query. Please advice proper way.
select code, firstname, lastname
from customer
group by code
But group by expecting firstname and lastname.Of course you will also need to group by firstname and lastname for that SQL statement.
With a group by SQL you have basically 2 types of values that can be returned:
- the value to group by
- the aggregated/calculated value for that group
So you could look at the first firstname and the last lastname per code:
select
  code, MIN(firstname), MAX(lastname)
from customer
group by codeYou can look at the number of unique first and last names per code:
select
  code, COUNT(DISTINCT firstname), COUNT(DISTINCT lastname)
from customer
group by codeThink of the group by clause in terms of having dimensions (e.g. code in the above SQL) and measures (the calculations per code in the above SQL).

Similar Messages

  • Group By one column

    I have one table as below
    CREATE TABLE [dbo].[emp](
        [EmpId] INT,
        [EmpName] VARCHAR(20),
        [SAL] INT,
        [DeptId] INT
    ) ON [PRIMARY]
    I want to select all column with group by one column
    SELECT EmpId, EmpName, SAL, DeptId FROM Emp GROUP BY DeptId
    Is it possible ?
    Is there any other way to achieve this?
    Deepak Talele Ph: 91-9158413830 Email: [email protected], [email protected]

    Hi,
    If you want to select all the columns then what is the need of group by?
    Group By normally used when you have math function in your query like, SUM() , COUNT(), MIN(), MAX() etc
    Could you please provide us with sample output that you are trying to achieve.
    Please mark solved if I've answered your question, vote for it as helpful to help other users find a solution quicker
    Praveen Dsa | MCITP - Database Administrator 2008 |
    My Blog | My Page

  • [Forum FAQ] How to configure a Data Driven Subscription which get multi-value parameters from one column of a database table?

    Introduction
    In SQL Server Reporting Services, we can define a mapping between the fields that are returned in the query to specific delivery options and to report parameters in a data-driven subscription.
    For a report with a parameter (such as YEAR) that allow multiple values, when creating a data-driven subscription, how can we pass a record like below to show correct data (data for year 2012, 2013 and 2014).
    EmailAddress                             Parameter                      
    Comment
    [email protected]              2012,2013,2014               NULL
    In this article, I will demonstrate how to configure a Data Driven Subscription which get multi-value parameters from one column of a database table
    Workaround
    Generally, if we pass the “Parameter” column to report directly in the step 5 when creating data-driven subscription.
    The value “2012,2013,2014” will be regarded as a single value, Reporting Services will use “2012,2013,2014” to filter data. However, there are no any records that YEAR filed equal to “2012,2013,2014”, and we will get an error when the subscription executed
    on the log. (C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\LogFiles)
    Microsoft.ReportingServices.Diagnostics.Utilities.InvalidReportParameterException: Default value or value provided for the report parameter 'Name' is not a valid value.
    This means that there is no such a value on parameter’s available value list, this is an invalid parameter value. If we change the parameter records like below.
    EmailAddress                        Parameter             Comment
    [email protected]         2012                     NULL
    [email protected]         2013                     NULL
    [email protected]         2014                     NULL
    In this case, Reporting Services will generate 3 reports for one data-driven subscription. Each report for only one year which cannot fit the requirement obviously.
    Currently, there is no a solution to solve this issue. The workaround for it is that create two report, one is used for view report for end users, another one is used for create data-driven subscription.
    On the report that used create data-driven subscription, uncheck “Allow multiple values” option for the parameter, do not specify and available values and default values for this parameter. Then change the Filter
    From
    Expression:[ParameterName]
    Operator   :In
    Value         :[@ParameterName]
    To
    Expression:[ParameterName]
    Operator   :In
    Value         :Split(Parameters!ParameterName.Value,",")
    In this case, we can specify a value like "2012,2013,2014" from database to the data-driven subscription.
    Applies to
    Microsoft SQL Server 2005
    Microsoft SQL Server 2008
    Microsoft SQL Server 2008 R2
    Microsoft SQL Server 2012
    Please click to vote if the post helps you. This can be beneficial to other community members reading the thread.

    For every Auftrag, there are multiple Position entries.
    Rest of the blocks don't seems to have any relation.
    So you can check this code to see how internal table lt_str is built whose first 3 fields have data contained in Auftrag, and next 3 fields have Position data. The structure is flat, assuming that every Position record is related to preceding Auftrag.
    Try out this snippet.
    DATA lt_data TYPE TABLE OF string.
    DATA lv_data TYPE string.
    CALL METHOD cl_gui_frontend_services=>gui_upload
      EXPORTING
        filename = 'C:\temp\test.txt'
      CHANGING
        data_tab = lt_data
      EXCEPTIONS
        OTHERS   = 19.
    CHECK sy-subrc EQ 0.
    TYPES:
    BEGIN OF ty_str,
      a1 TYPE string,
      a2 TYPE string,
      a3 TYPE string,
      p1 TYPE string,
      p2 TYPE string,
      p3 TYPE string,
    END OF ty_str.
    DATA: lt_str TYPE TABLE OF ty_str,
          ls_str TYPE ty_str,
          lv_block TYPE string,
          lv_flag TYPE boolean.
    LOOP AT lt_data INTO lv_data.
      CASE lv_data.
        WHEN '[Version]' OR '[StdSatz]' OR '[Arbeitstag]' OR '[Pecunia]'
             OR '[Mita]' OR '[Kunde]' OR '[Auftrag]' OR '[Position]'.
          lv_block = lv_data.
          lv_flag = abap_false.
        WHEN OTHERS.
          lv_flag = abap_true.
      ENDCASE.
      CHECK lv_flag EQ abap_true.
      CASE lv_block.
        WHEN '[Auftrag]'.
          SPLIT lv_data AT ';' INTO ls_str-a1 ls_str-a2 ls_str-a3.
        WHEN '[Position]'.
          SPLIT lv_data AT ';' INTO ls_str-p1 ls_str-p2 ls_str-p3.
          APPEND ls_str TO lt_str.
      ENDCASE.
    ENDLOOP.

  • Inserting multiple selection from checkbox in to one column of the database

    Hi,
    how to insert multiple selection from checkbox into one column of the database.(I select array of values from checkbox ,then how to insert tat array of values iinto single column name).
    Anyone can u reply me
    Thanx

    hhhmmm.... is this what you mean?
    lets say you hava a checkbox1 with values value1, value2, value3 and you selected all there of them? then you want then to be stored in the database in one column?
    now the question is:
    Is it going to be one column one row?
    datafield
    value1,value2,value3
    Or one column multiple row?
    datafield
    value1
    value2
    value3
    Which is it?

  • Combine 2 specific column values into one column

    Is there an easy solution to combine column values that is in a group?
    In my report I have 1 column group named Activity_Area which has 5 different values Activity 1, Activity2...Activity5.
    Let's say I want to merge Activity 4 and Activity 5 and call it ActivityX and gives me the output as below
    Activity 1 | Activity 2 | Activity 3 | ActivityX

    Hi Jhonny86,
    According to your description, you create a field in database and it contains five values. Then you want to only display four values in report column group, and the last two values should be replaced by “ActivityX”, right?
    In your scenario, you can create an table like below:
    create table Act(id int,activity varchar(99))
    insert into Act select 1,'Activity 1'
    insert into Act select 2,'Activity 2'
    insert into Act select 3,'Activity 3'
    insert into Act select 4,'Activity 4'
    insert into Act select 5,'Activity 5'
    Then query the table with code like below in query designer, then add the field [Act] into the column group.
    If you have any question, please feel free to ask.
    Best regards,
    Qiuyun Yu
    Qiuyun Yu
    TechNet Community Support

  • Concacenate Many Columns text into one Column

    Hi,
    I have many columns with text in it. I would like to ideally concatenate these column content into just ONE column.
    Please help work out how to do this as I have no idea.
    I am somewhat familiar with CMD so if you can help with this urgently that would be SO awesome.
    Thanks
    :)

    890635 wrote:
    Thanks again...
    So I want to display this data eventually on forms. So I think I do need to create a blank column.
    so let's say I want the concatenated data to end up in "Final_Delivery".
    How to do that?
    Thank youI dont have any idea about Forms...But just a guess that for form as well you must be writing some SQL, and you can use the sql there directly. Otherwise if you want to get it inserted into new column in existing table....First you have to add a new column to your table and then run update Statement to populate that data..similar to this
    SQL> select * from emp;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO       TEST INFO
          7369 SMITH      CLERK           7902 17-DEC-80        800                    20
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
          7566 JONES      MANAGER         7839 02-APR-81       2975                    20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
          7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
          7839 KING       PRESIDENT            17-NOV-81       5000                    10
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
          7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO       TEST INFO
          7900 JAMES      CLERK           7698 03-DEC-81        950                    30
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
    14 rows selected.
    SQL> update emp set info=to_char(empno)||' '||ename||' '||job;
    14 rows updated.
    SQL> select * from emp;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO       TEST INFO
          7369 SMITH      CLERK           7902 17-DEC-80        800                    20            7369 SMITH CLERK
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30            7499 ALLEN SALESMAN
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30            7521 WARD SALESMAN
          7566 JONES      MANAGER         7839 02-APR-81       2975                    20            7566 JONES MANAGER
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30            7654 MARTIN SALESMAN
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30            7698 BLAKE MANAGER
          7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10            7782 CLARK MANAGER
          7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20            7788 SCOTT ANALYST
          7839 KING       PRESIDENT            17-NOV-81       5000                    10            7839 KING PRESIDENT
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30            7844 TURNER SALESMAN
          7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20            7876 ADAMS CLERK
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO       TEST INFO
          7900 JAMES      CLERK           7698 03-DEC-81        950                    30            7900 JAMES CLERK
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    20            7902 FORD ANALYST
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    10            7934 MILLER CLERK
    14 rows selected.
    SQL>Regards,
    Navneet

  • How to group by one column and concatinate records from another col

    hi,
    i want to fill a list-item in forms 6i with one cursor. i need a cursor / sql-function that fetch the data from the following table structur. it looks like this:
    col1 col2
    group1 01
    group1 02
    group2 03
    group3 04
    group2 05
    group1 06
    the resultset should look like this:
    label value
    group1 01.02.06
    group2 03.05
    group3 04
    any ideas ?
    thanks in advance.
    greets Kevin

    Try this
    table X2
    columns a and b
    SELECT a
    , LTRIM(MAX(SYS_CONNECT_BY_PATH(b,'.'))
    KEEP (DENSE_RANK LAST ORDER BY curr),'.') AS concatenated
    FROM ( SELECT a
    , b
    , ROW_NUMBER() OVER (PARTITION BY a ORDER BY b) AS curr
    , ROW_NUMBER() OVER (PARTITION BY a ORDER BY b) -1 AS prev
    FROM x2 )
    GROUP BY a
    CONNECT BY prev = PRIOR curr AND PRIOR a = a
    START WITH curr = 1
    Pls. Confirm if it works..

  • How to select/unselect all checkbox in one column

    Hi Experts,
    I have a report with a column as check box. The query is like:
    select apex_item.checkbox(1, ID) "update", ID, name, job
    from a_table
    So that user can select the rows they want to update.
    Sometimes user wants to select all the rows. It's not easy to select all the rows one by one when there are many rows on the report.
    So how to select/unselect all rows in this case?
    Thanks,
    Daniel

    Hi,
    See if this post help
    Re: Need Check All in Checkbox
    Regards,
    Jari

  • How to get 2 columns data in One Column

    Hi,
    I have requirement like below
    EName       Sal
    Smith         1000
    Scott          2000
    Miller          3000
    I want to display this two column like as follows
    Emp
    Smith
    1000
    Scott
    2000
    Miller
    3000
    Thanks & Regards,
    Hari Babu

    Hi,
    That's called Unpivoting.
    In Oracle 11.1 (and higher) you can use the SELECT  ... UNPIVOT feature. For details and examples, see the SQL Language manual: http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#sthref6810
    In any version of Oracle, you can cross-join your table to any table or result set that has at least 2 rows, and use CASE (or DECODE) to get the results you want.
    Remember that a column can only have 1 datatype: it's impossible to have a column that's a VARCHAR2 on some rows, but a NUMBER on other rows.  You'll have to convert some data so that everything in the 1 output column has the same datatype.  There's no way to express 'Smith' as a NUMBER, but you can convert all the NUMBERs to strings.
    I hope this answers your question.
    If not, post  a little sample data (CREATE TABLE and INSERT statements), and also post the results you want from that data (if not what you posted already).
    Post your best attempt at a query.  Point out where it is getting the wrong results, and explain, using specific examples, how you get those results from the given data in those places.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • Showing multiple column name in one column of a table

    Hi
    I have a table like below
    Emp_No Sal
    P101 10000
    P102 20000
    P103 30000
    P104 40000
    Now I have a requirment to show like below only
    Emp_No
    Sal
    No values.
    Please help

    user12838764 wrote:
    Now I have a requirment to show like below only
    Emp_No
    Sal
    select column_name from All_Tab_Cols where table_name = <table_name>;Vivek L

  • Inserting multiple selection from checkbox into one column of the database

    Hi,
    How to insert multiple selection values from checkbox into one column of the database.
    Anyone can u help me
    Thanx

    hi
    try to use request.getParameterValues("fieldname")

  • Select distinct('more than one column') from ....

    Why does this sql statement not work:
    select distinct(column_a, column_b, column_c), empno
    from emp;
    How can I 'select distinct' using more than one column.

    Actually, DISTINCT applies to all columns in the result.
    Distinct is not a function, you cannot do distinct(column1, column2, ...).
    As a matter of fact it doesn't make sense to expect distinct applies to only one column, because you do not specify any criteria on how to select the values for the second column.
    A final note: you can do distinct(column1), column2, but this will still apply to both columns!
    Here're some examples that will all return both rows from the set, where set is: (1,1), (1,2):
    select distinct val1, val2
    from
    select 1 as val1, 1 as val2 from dual
    union all
    select 1 as val1, 2 as val2 from dual
    select distinct val1||'-'||val2
    from
    select 1 as val1, 1 as val2 from dual
    union all
    select 1 as val1, 2 as val2 from dual
    select distinct(val1), val2 -- this is very misleading. distinct still applies to both columns
    from
    select 1 as val1, 1 as val2 from dual
    union all
    select 1 as val1, 2 as val2 from dual
    Edited by: user4010726 on Dec 1, 2009 11:06 AM
    Edited by: user4010726 on Dec 1, 2009 11:07 AM

  • GROUB BY with more than one column

    I (who is not very good at SQL) was explaining GROUP BY clause in SQL to my beginner colleague . I showed him an example where results are grouped for one column . Something like
    select customer, count(*) as ItemCount
    from Orders
    group by customerCould you guys give us a meanigful, simple and easy to remember example where two columns are being grouped

    Hi Zebra,
    Got this example from Google
    DROP TABLE myenterprise;
    CREATE TABLE myenterprise(
    city VARCHAR2(10),
    storeid VARCHAR2(10),
    month_name VARCHAR2(10),
    total_sales NUMBER);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id1', 'January', 1000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id1', 'March', 7000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id1', 'April', 2000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id2', 'November', 2000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id3', 'January', 5000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('london', 'id4', 'Janaury', 3000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('london', 'id4', 'August', 6000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('london', 'id5', 'September', 500);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('london', 'id5', 'November', 1000);
    -- GROUPing of one column (which you already have)
    SELECT city, SUM(total_sales) AS TOTAL_SALES_PER_CITY
    FROM myenterprise
    GROUP BY city
    -- Two columns making up a group
    select city, month_name, sum(total_sales) as total_sales_per_city
    from myenterprise
    group by city, month_name
    order by total_sales_per_city desc;

  • Concate 3  column data in to one column.

    I would like to concate 3 columns in to one column. all three column has datatype as LONG. Is any one knows how to concate long datatype column into one column?
    Thanks in Advance

    812294 wrote:
    Is there any way to not use CLOB datatype? I am fetching data from three different tables.And what fetching from three different tables has to do with CLOB?
    SQL> set serveroutput on
    SQL> declare
      2      concat_result clob;
      3      tmp           long;
      4  begin
      5      select  text
      6        into  tmp
      7        from  dba_views
      8        where rownum = 1;
      9      concat_result := tmp;
    10      select  data_default
    11        into  tmp
    12        from  dba_tab_columns
    13        where data_default is not null
    14          and rownum = 1;
    15      concat_result := concat_result || tmp;
    16      select  search_condition
    17        into  tmp
    18        from  dba_constraints
    19        where search_condition is not null
    20          and rownum = 1;
    21      concat_result := concat_result || tmp;
    22      dbms_output.put_line(concat_result);
    23  end;
    24  /
    select
    "LIB_IDX","LIB_NAME","VENDOR_NAME","PROTOCOL_NUM","VERSION_NUM","PATH_NAME","MAP
    _FILE","FILE_CFGID","MAP_ELEM","ELEM_CFGID","MAP_SYNC" from v$map_library0 OBJ#
    IS NOT NULL
    PL/SQL procedure successfully completed.
    SQL> SY.

  • How to create Tree format in Table Rn in one Column having data?

    Hi Gurus,
    i got the new requirement it's very interesting pl z help me...
    My Requirement is i have one Table Rn in Table Rn having 5 columns There here one column name is 'Competencies ' That Competencies column having the more rows liki
    FUNC.ADMIN.Achievement Orientation
    FUNC.ADMIN.Administrative Assistance and Support
    FUNC.ADMIN.Administrative(ADMIN)
    This is one column data my Requirement is now
    FUNC
    +ADMIN
    +Achievement Orientation
    like clients are expecting pl z tell me how to approach in this requirement and what are prerequisites are needed ...plz help me it's urgent requirement.
    Regards,
    Srini

    Hi Gyan,
    Thank's for valuable suggestions
    My Requirement is This ...
    "Competencies" Table Layout: (InstTableLayout)
    COMPETENCIES (COLUMN NAME)
    BEHV.Generic.Communication(BEHV)
    BEHV.Generic.Planning, Organizing & Time Management
    BEHV.Generic.Problem Solving Ability
    BEHV.Generic.Productivity
    BEHV.Generic.Punctuality
    like number of rows are there now Tree format is row data
    like each column divided each one
    ex:
    + BEHV
    *|*
    +Generic
    *|*
    +Communication(BEHV)
    like he expected client in tree format pl z tell me how to approach in this requirement and what are the prerequisites are required .....
    Regards,
    Srini
    Edited by: 876628 on Dec 1, 2011 8:37 PM
    Edited by: 876628 on Dec 1, 2011 8:37 PM

Maybe you are looking for

  • Creating schema from an XML file in Java

    I know that there are a lot of tools out that that do this, but I need some special conversion done. Is there any source code out there that shows you how to take in an XML file and generate schema for it using Java? The only thing I found used C# an

  • Messages gettin stuck at outbound side..

    hi.. my files r gettin picked up successfully. i need to corelate them, i knw corelation is fine. they r suppose to enter the BPM. but they r not. at the outbound side its constantly showin "messages scheduled for outbound side (green flag). my queue

  • Product cost Collection - Revaluation

    I am trying to do the revaluation of product cost collectors at actual prices. My activity planning price entered manually (Rs.17500 - fixed) My activity actual price also entered manually (Rs.19500-fixed) Now I am doing the CON1/CON2 transactions. 

  • NI-FBUS Communications Manager 4.0.1 Error

    I have dual LAN connection on my notebook as enclosed in the first picture. The NI-FBUS Interface Configuration Utility recognises the connection to the HSE linking device without any errors, see second picture. Ping to the address is OK, but the NI-

  • Indesign CS4 - now very slow

    On one identity (mac), our indesign is super slow.  Pinwheel displays every time we try to move an object or type character, etc.  Its seems to be less of a problem on a different identity on the same mac.  We tried everything we know to troubleshoot