Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6588447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:02:58+00:00 2026-05-25T17:02:58+00:00

probably sql makes me dizzy when complexity level increases. It is easier to put

  • 0

probably sql makes me dizzy when complexity level increases. It is easier to put a for loop and work in c#.

I have a query like

select.field1,.field2, field3,field4
from table1 

Suppose this returns rows 1, 2, 3, 4, 5, 6.

I want to return summarized one row if this result has same field2 and field3. if ANY of the rows is different then return all the rows.

Thanks in advance.

Here is Sample data. In this lis row number 1 and row 4 are parent items and others child items.
When Summarizing, row 1 is summarized with all the child items but row number 4 is not summarized with children rows since row number 6 is has a different value field 2.

Field1 Field2 Field3 Field4(parentid)
1      paper cash    null
2      Paper cash     1
3      paper cash     1
4      paper cash     null
5      paper cash     4
6      pen    cash    4

Here I want to return

field1  Field2 Field3  field4(all the child's id)
1       paper cash     (2,3)
4       paper cash     null
5       paper cash     null
6       pen cash     null 

Hope this is better.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:02:58+00:00Added an answer on May 25, 2026 at 5:02 pm

    With SQL Server you’ll have to create a function to assist with the concatenation and a #temp table to assist with post-processing (to avoid repeated calls to the function across the whole source table). If you move to a database platform released in the last 10 years, you’ll have much more efficient solutions at your fingertips. 🙂

    Setup:

    USE tempdb;
    GO
    
    CREATE TABLE dbo.[Sample]
    (
        Field1 INT, 
        Field2 VARCHAR(32),
        Field3 VARCHAR(32), 
        Field4 INT
    );
    
    INSERT dbo.[Sample] SELECT 1,'paper','cash', NULL
    UNION ALL   SELECT 2,'Paper','cash', 1
    UNION ALL   SELECT 3,'paper','cash', 1
    UNION ALL   SELECT 4,'paper','cash', NULL
    UNION ALL   SELECT 5,'paper','cash', 4
    UNION ALL   SELECT 6,'pen',  'cash', 4;
    GO
    

    Function:

    CREATE FUNCTION dbo.ConcatIDs
    (
      @Field1 INT
    )
    RETURNS VARCHAR(8000)
    AS
    BEGIN
        DECLARE @s VARCHAR(8000);
        SELECT @s = COALESCE(@s + ',', '')
            + CONVERT(VARCHAR(12), Field1)
          FROM dbo.[Sample] AS s
          WHERE Field4 = @Field1 
          AND NOT EXISTS
          (
            SELECT 1
              FROM dbo.[Sample]
              WHERE Field4 = s.Field4
              AND Field1 <> s.Field1
              AND (Field2 <> s.Field2 OR Field3 <> s.Field3)
          );
        RETURN @s;
    END
    GO
    

    The query:

    SELECT Field1, Field2, Field3, Field4, f4 = dbo.ConcatIDs(Field1) 
      INTO #x
      FROM dbo.[Sample];
    
    SELECT Field1, Field2, Field3, 
      [field4(all the child's id)] = '(' + f4 + ')'
    FROM #x AS x1
    WHERE NOT EXISTS 
    (
        SELECT 1 FROM #x AS x2
        WHERE x2.Field1 = x1.Field4
        AND x2.f4 IS NOT NULL
    );
    
    DROP TABLE #x;
    

    Results:

    Field1 Field2 Field3 field4(all the child's id)      
    ------ ------ ------ --------------------------
    1      paper  cash   (2,3)
    4      paper  cash   NULL
    5      paper  cash   NULL
    6      pen    cash   NULL
    

    Cleanup:

    DROP TABLE dbo.[Sample];
    DROP FUNCTION dbo.ConcatIDs;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My production is on full blown SQL Server 2008. I would like to have
Probably a long question for a simple solution, but here goes... I have a
This probably has a simple answer, but I must not have had enough coffee
This probably sounds really stupid but I have noo idea how to implement jquery's
This question would probably apply equally as well to other languages with C-like multi-line
As many of you probably know, online banks nowadays have a security system whereby
I'm not too good with SQL and I know there's probably a much more
Related LINQ-to-SQL vs stored procedures? I have heard a lot of talk back and
Using LINQ-to-SQL, I would like to automatically create child records when inserting the parent
How can I write an SQL query that returns a record only if ALL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.