Results 1 to 2 of 2
  1. #1
    Somnath_IT2006 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    4

    Combine Two Rows - SQL Query

    Dear All



    I have table as like below:

    Code:
    ID  Ref   Name      Type       Sal
    ----------------------------------------
    1   1     A         W          2000
    2   1     B         X          6000
    3   1     C         Y          3000
    4   1     D         Z          3000
    5   2     K         W          2000
    6   2     D         X          1000
    7   2     D         Y          1000
    8   2     D         Z          1000

    I want result like this:

    Code:
    ID  Ref    Name       Type        Sal
    ----------------------------------------
    1   1      A           W          8000
    3   1      C           Y          3000
    4   1      D           Z          3000
    5   2      K           W          3000
    7   2      D           Y          1000
    8   2      D           Z          1000
    As per the group of "Ref" field, in case of "Type" W, X "Sal" column needs to get sum of both the rows and keep the other fields from W row.

    In each Ref group (as like 1 or 2 in above data) there won't be duplication of "Type" field.

    How can I create SQL statement for get this result.

    All your help appreciated.


    Regards
    Somnath

  2. #2
    Somnath_IT2006 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    4

    Solved - SQL Query

    Hi All

    I got the solution:

    Code:
    SELECT a.ID, a.Ref, a.Name1, a.Type, 
          IIF(a.Type ='W',(SELECT SUM(b.Sal) FROM tblTest b WHERE b.Ref = a.Ref AND (b.Type = 'W' OR b.Type = 'X')),a.Sal) as Sal1
    FROM tblTest a WHERE a.Type <> 'X';
    Regards
    Somnath

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query to find and COMBINE rows based on 2 fields
    By admessing in forum Queries
    Replies: 2
    Last Post: 12-13-2011, 12:59 PM
  2. combine query result
    By Fenvy in forum Queries
    Replies: 1
    Last Post: 06-22-2011, 05:02 PM
  3. combine rows
    By summerAIS in forum Queries
    Replies: 3
    Last Post: 07-31-2010, 10:13 PM
  4. Combine multiple rows into one cell
    By zarfx4 in forum Queries
    Replies: 8
    Last Post: 06-08-2009, 10:42 AM
  5. combine three tables in a query
    By neuenglander in forum Queries
    Replies: 0
    Last Post: 08-21-2008, 04:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums