Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Table 1
C#
Id	SegId	Fid	Amount
1	  7,8	101	200
2	    9	101	300

Table 2
C#
SegId	Fid	Orgin	Destination
7       101	  DEL	  BOM
8       101	  BOM	  GOA
9	  101	  DEL	  BOM


I have two table table 1 and table 2 i want sql query to find expected result in
table -3
C#
Fid	Segments	Amount
101	DEL-BOM-GOA	 200
101	DEL-BOM	         300


Thanks in advance
Posted
Comments
dan!sh 20-Jan-16 0:51am    
You should consider redesigning the tables. Comma separated values are going to cause problems like these while querying.
suneel kumar gupta 20-Jan-16 1:33am    
Thanks
Sri Nivas (Vasu) 20-Jan-16 0:52am    
Do you have this table structure already existed or you want to create this table structure? I think better to store table1 SegId in separate table. Instead of comma separated.
suneel kumar gupta 20-Jan-16 1:32am    
Thank for your reply Mr Sri Nivas.Table 1 and table 2 already exist. we need to work on this.
Sri Nivas (Vasu) 20-Jan-16 2:01am    
Okay, let me give you the query for you. But if possible alter the table structure it will saves the life of developer. Discuss with your team members regarding table structure changes.

1 solution

Following query will give the required result for you.
C#
SELECT
        Fid,
        (SELECT
            top 1
                STUFF(
                    (SELECT
                            '-' +  T4.Orgin
                        FROM
                            dbo.fnSplitString( ot.SegId ,',')  t3
                            join table2 t4 on t3.splitdata = t4.segid FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 1, '')
                + '-' +  t2.Destination
                FROM
            dbo.fnSplitString( ot.SegId ,',')  t1 join table2 t2 on t1.splitdata = t2.segid order by t1.splitdata desc)
         AS Segments,
        Amount,*
    FROM TABLE1 ot

In the above query fnSplitString() user defined function has been used the code for this function is

SQL
CREATE FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1
       
        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)
        
    END 
    RETURN 
END


Taken from How to Split a string by delimited char in SQL Server.............. - SQLServerCentral[^]

Good luck! But try to change the table structure if possible :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900