SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select an item more then once
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jacobgold
Starting Member

USA
5 Posts

Posted - 07/31/2012 :  18:06:20  Show Profile  Reply with Quote
hi
i have a simple problem
i have a small table that contains 20 rows with 2 columns id and name i want to enter numbers like this lets say= 1 3 2 5 5 4 3 2 2 1 and i should get the name of id 1 and then of 3 and then of 2 and then 5 and then 5 etc. i want to get them in this order repetitive
i can't make a simple select statement like this
select name where id =1 3 2 5 5 4 3 2 2 1
because it would not be repeaated and not in this row sow what should i do?

yosiasz
Flowing Fount of Yak Knowledge

USA
1610 Posts

Posted - 07/31/2012 :  19:21:38  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
what is the delimiter for 1 3 2 5 5 4 3 2 2 1? space?

declare @jacobsilver table(id int, name varchar(50))
declare @jacobatolympics table(id varchar(max))

insert into @jacobatolympics
values('1 3 2 5 5 4 3 2 2 1')

insert into @jacobsilver(id, name)
values(1, 'a'),(2,'b'),(3,'c'),(4,'c'),(5,'e'),(6,'f'),(7,'g')


 
select * 
  From @jacobsilver a
  inner join (
  SELECT Tags.val.value('.', 'int') AS id
  FROM(  
		  SELECT CAST('<t>' + REPLACE(id, ' ', '</t><t>') + '</t>' AS XML) AS TAG
			FROM @jacobatolympics
	  ) TAB 
 CROSS APPLY TAG.nodes('/t') as Tags(val)
  ) b
  
  on a.id = b.id



<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion

Edited by - yosiasz on 07/31/2012 19:33:41
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/31/2012 :  19:38:35  Show Profile  Reply with Quote
Here are two or three ways to do this. My recommendation is that you use the third approach, even though it requires you to install a function.

I just want to point out that this seems like a business requirement that can perhaps be recast to make it more palatable to a database. There is something not quite right in a design that sends the input data in this manner and requires output in the way you described.

------------------------------------------------------------------------
-- CREATE TEST TABLE AND POPULATE WITH DATA.
------------------------------------------------------------------------
CREATE TABLE #tmp(id INT, NAME VARCHAR(32), seq VARCHAR(255) )
INSERT INTO #tmp (id,NAME)VALUES  (1,'A'),(2,'B'),(3,'C'),(4,'D'),(5,'E');


------------------------------------------------------------------------
-- QUIRKY UPDATE METHOD. 
------------------------------------------------------------------------
DECLARE @y VARCHAR(255) = '';
DECLARE @x VARCHAR(32) = '1 3 2 5 5 4 3 2 2 1';


UPDATE #tmp
SET    seq = @x,
       @y = REPLACE(@x, CAST(id AS VARCHAR(32)), NAME),
       @x = @y
FROM #tmp WITH (TABLOCKX) OPTION(MAXDOP 1)	
	
SELECT TOP 1 seq FROM #tmp ORDER BY id DESC;

------------------------------------------------------------------------
-- CTE METHOD. 
------------------------------------------------------------------------
DECLARE @x VARCHAR(32) = '1 3 2 5 5 4 3 2 2 1';

;WITH cte AS
(
	SELECT id, REPLACE(@x,CAST(id AS VARCHAR(32)),NAME) AS Updated
	FROM #tmp WHERE id = 1
	UNION ALL
	SELECT t.id,REPLACE(Updated, CAST(t.id AS VARCHAR(32)), NAME)
	FROM #tmp t INNER JOIN cte c ON c.id + 1 = t.id
)
SELECT TOP 1 Updated FROM cte ORDER BY id DESC;

------------------------------------------------------------------------
-- XML PATH METHOD. You will need to install the function DelimitedSplit8K from
-- this article (in Fig 21).
-- http://www.sqlservercentral.com/articles/Tally+Table/72993/
------------------------------------------------------------------------
DECLARE @x VARCHAR(32) = '1 3 2 5 5 4 3 2 2 1';

;WITH cte AS
(
	SELECT * FROM MASTER.dbo.DelimitedSplit8K(@x,' ')
)
SELECT LTRIM(RTRIM(c)) FROM
(
SELECT
	' ' + NAME AS [text()]
FROM
	cte c
	INNER JOIN #tmp t ON t.id = c.Item
FOR XML PATH('') )T(c);

------------------------------------------------------------------------
-- CLEANUP
------------------------------------------------------------------------
DROP TABLE #tmp;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000