Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 March SQL Puzzle: SQL Advance Query – Get the DISTINCT String data after CONCAT

SQL Puzzle: SQL Advance Query – Get the DISTINCT String data after CONCAT

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output and get the list of distinct concat string values.

Input Data:

1
2
3
4
5
6
7
8
9
10
11
id strname
----------- ------------
1 abc
2 xyz
3 abc
4 pqr
5 pqr
6 xyz
7 wre
8 abc
9 wre

Expected Output:

1
2
3
result
-----------------------
abc, pqr, wre, xyz,

Create a table with sample data:

1
2
3
4
5
6
7
8
CREATE TABLE tbl_strings (id int, strname varchar(50))
GO
 
INSERT INTO tbl_strings VALUES
(1,'abc'),(2,'xyz'),(3,'abc')
,(4,'pqr'),(5,'pqr'),(6,'xyz')
,(7,'wre'),(8,'abc'),(9,'wre')
GO

Solution using XML PATH:

1
2
3
SELECT DISTINCT strname + ', '
FROM tbl_strings
FOR XML PATH('')

Please try the different solution for this puzzle and share it via comment...

Mar 14, 2018Anvesh Patel
SQL Puzzle: SQL Advance Query - Concat the String records without using STUFF functionSQL Puzzle: SQL Advance Query - How to get the Name of Month
Comments: 5
  1. Sandeep
    July 18, 2018 at 7:35 am

    SELECT CAST(Abs(rand()*100) AS INT)%50

    ReplyCancel
  2. Ankush
    September 14, 2018 at 8:07 am

    Select LISTAGG(strname, ‘,’) WITHIN GROUP (ORDER BY strname)
    from
    (Select distinct strname from table)foo

    ReplyCancel
  3. Dpan
    April 16, 2019 at 4:44 am

    SELECT Distinct(strname) as strname into #tbl_strings3 from #tbl_strings

    select ROW_NUMBER() over (Order by strname) as rownumber, strname into #tbl_strings4 from #tbl_strings3
    Declare @i int,@str nvarchar(max) ; set @i=1;set @str=”;
    while (@i<=4)
    begin
    select @str=Concat(@str,strname,',') from #tbl_strings4 where rownumber=@i
    print @str;
    set @i=@i+1;
    end

    ReplyCancel
  4. HIC
    April 28, 2019 at 8:05 am

    declare
    s varchar2(255) := ”;
    begin
    for c in (select distinct strname from tbl_strings)
    loop
    s := s || c.strname || ‘, ‘;
    end loop;

    s := rtrim(s, ‘, ‘);

    dbms_output.put_line(s);
    end;

    ReplyCancel
  5. koustav Modak
    November 2, 2019 at 6:50 am

    select array_to_string(array_agg(replace(replace(result::text,'(‘,”),’)’,”)), ‘,’) rel_name
    from
    (select distinct strname from tbl_strings order by 1)result

    ReplyCancel

Leave a Reply to Dpan Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

March 14, 2018 5 Comments SQL PuzzleAnvesh Patel, CONCAT, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks, String Data, XML PATH
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....