Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am having a table

Table A
--------
A
B
C
D

I need a result table as below

Table A
--------
A
B
C
D
AB
BC
CD
ABC
BCD
ABCD


Any help appreciated.

What I have tried:

I tried bringing two words combination.

DECLARE @tbl_wordlist TABLE (
    [Id]        int identity,
    [Word]   nvarchar(max)
)
INSERT INTO @tbl_wordlist
SELECT [Data] FROM Split('A B C D', ' ')

-- LOGIC :::::: USING WHILE LOOP, GROUP TWO-TWO WORDS AND THEN INSERT INTO THE SAME TABLE
DECLARE @MaxCount INTEGER
DECLARE @Count INTEGER, @I INT
DECLARE @Txt VARCHAR(MAX)
SET @Count = 1
SET @I = 2
SET @Txt = (SELECT [Word] FROM @tbl_wordlist WHERE ID = @Count AND [Word] IS NOT NULL)
SET @MaxCount = (SELECT MAX(ID) FROM @tbl_wordlist) 
WHILE @Count<@MaxCount
    BEGIN
    IF @Txt!=''
        SET @Txt=@Txt+' ' + (SELECT [Word] FROM @tbl_wordlist WHERE ID = (@I))
    ELSE
        SET @Txt=(SELECT [Word] FROM @tbl_wordlist WHERE ID=@Count)
    SET @Count=@Count+1
    SET @I=@I+1
    INSERT INTO @tbl_wordlist([Word])Values(@Txt)
    SET @Txt = (SELECT [Word] FROM @tbl_wordlist WHERE ID = @Count AND [Word] IS NOT NULL)
    END

SELECT * FROM @tbl_wordlist;
Posted
Updated 28-Oct-17 6:33am
Comments
Karthik_Mahalingam 26-Oct-17 21:27pm    
What about ac,bd,... Etc
Member 13486774 27-Oct-17 2:26am    
No. I just want consecutive words to be together.
CHill60 27-Oct-17 14:16pm    
You should not need any loops at all. When I get to a laptop I'll try to post a solution

I know you have already accepted an answer but as I said yesterday, there is no need to use a loop, in fact the number of times you need to use any loop in SQL is very rare - because it is SET based.

A very simple way of producing your output is just to use a series of self-joins...
SQL
-- Get the AB, BC, CD entries with a self join
INSERT INTO @tbl_wordlist 
SELECT A.Word + B.Word FROM @tbl_wordlist A
LEFT OUTER JOIN @tbl_wordlist B ON A.Id + 1 = B.Id
WHERE B.Word IS NOT NULL

-- Get the ABC, BCD entries with another self join
INSERT INTO @tbl_wordlist 
select A.Word + B.Word
from @tbl_wordlist A
LEFT OUTER JOIN @tbl_wordlist B ON A.Id + 5 = B.Id	-- NB The 5 here is important!
WHERE B.Word IS NOT NULL

-- Get the ABCD entry with yet another self join
INSERT INTO @tbl_wordlist 
select A.Word + B.Word
from @tbl_wordlist A
LEFT OUTER JOIN @tbl_wordlist B ON A.Id + 8 = B.Id	-- NB The 8 here is also important!
WHERE B.Word IS NOT NULL
There are other and better approaches that could be used, this is just the first one that came to mind.

If this is your homework then your tutor is probably going to start teaching you more about combinations soon, and they are absolutely not going to need you to keep coding loops in SQL (if they do then leave that college!). I got so passionate about loops being used I wrote an article about it - Processing Loops in SQL Server[^] - my article only touches the surface of what can be done (what should be done) in SQL without going anywhere near a conventional "loop" so do do more research.
 
Share this answer
 
Modified, please try

SQL
DECLARE @tbl_wordlist TABLE (
    [Id]        int identity,
    [Word]   nvarchar(max)
)
INSERT INTO @tbl_wordlist
SELECT [Data] FROM Split--('A B C D', ' ')
 
DECLARE @COUNTER AS INTEGER

DECLARE @RUNNINGCOUNTER AS INTEGER 
DECLARE @NUMBEROFRUNS AS INTEGER = 1

--DECLARE @INNERCOUNTER AS INTEGER
DECLARE @TOTAL AS INTEGER  = (SELECT COUNT(*) FROM SPLIT)

SET @RUNNINGCOUNTER = (SELECT COUNT(*) FROM SPLIT)
sET @COUNTER = 1

DECLARE @WORD AS VARCHAR(MAX)

WHILE @RUNNINGCOUNTER > 0
BEGIN
	SET @NUMBEROFRUNS = 1
	WHILE @NUMBEROFRUNS <= @RUNNINGCOUNTER -1
	BEGIN

		SET @WORD = ''		
		SET @COUNTER = 1
		SET @WORD =''
		
		--PRINT 'RUN Counter '  + cast (@NUMBEROFRUNS as nvarchar(10))
		
		WHILE @COUNTER <= @TOTAL - (@RUNNINGCOUNTER -2)
		BEGIN
		--PRINT 'Counter '  + cast (@cOUNTER as nvarchar(10))
			SET @WORD = @WORD  + (SELECT WORD FROM @tbl_wordlist 
			WHERE ID =@NUMBEROFRUNS -1 + @COUNTER)
			SET @COUNTER=@COUNTER+1
		END
	
		pRINT @WORD
		INSERT INTO @tbl_wordlist (WORD) VALUES(@WORD)	
		
		
		SET @NUMBEROFRUNS =@NUMBEROFRUNS +1
	END
	
	SET @NUMBEROFRUNS = 1
	SET @RUNNINGCOUNTER = @RUNNINGCOUNTER -1
END
sELECT * FROM @tbl_wordlist
 
Share this answer
 
v2
Comments
Member 13486774 27-Oct-17 2:24am    
Hi @itsmypassion , Thanks for reply. The code is awesome, but it is giving a different pattern than what I want.
It gives

Table A
---------
A
B
C
D
A
AB
ABC
ABCD
itsmypassion 27-Oct-17 4:45am    
OK
Member 13486774 27-Oct-17 9:12am    
It worked like a charm. Thanks a lot. I wouldn't have ever made it.

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