<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Alter SQL table</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/alter-table/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/</link>
	<description></description>
	<pubDate>Sat, 28 Nov 2009 03:49:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: Carlosdl</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64118</link>
		<dc:creator>Carlosdl</dc:creator>
		<pubDate>Fri, 29 May 2009 22:39:55 +0000</pubDate>
		<guid isPermaLink="false">#comment-64118</guid>
		<description>For the above code to work in Oracle, you need to qualify the '*' in the subquery.

Something like this:

select number, max(case when rnk=1 then code end) code1, max(case when rnk=2 then code end) code2
from (
select [B]y.[/B]*, RANK() over(partition by number order by code) rnk
from yourtable [B]y[/B]
) x
group by number;</description>
		<content:encoded><![CDATA[<p>For the above code to work in Oracle, you need to qualify the &#8216;*&#8217; in the subquery.</p>
<p>Something like this:</p>
<p>select number, max(case when rnk=1 then code end) code1, max(case when rnk=2 then code end) code2<br />
from (<br />
select <b>y.</b>*, RANK() over(partition by number order by code) rnk<br />
from yourtable <b>y</b><br />
) x<br />
group by number;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Msi77</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64115</link>
		<dc:creator>Msi77</dc:creator>
		<pubDate>Fri, 29 May 2009 22:05:15 +0000</pubDate>
		<guid isPermaLink="false">#comment-64115</guid>
		<description>Somewhat ansi solution:

select number, max(case when rnk=1 then code end) code1, max(case when rnk=2 then code end) code2
from (
select *, RANK() over(partition by number order by code) rnk
from yourtable
) x
group by number</description>
		<content:encoded><![CDATA[<p>Somewhat ansi solution:</p>
<p>select number, max(case when rnk=1 then code end) code1, max(case when rnk=2 then code end) code2<br />
from (<br />
select *, RANK() over(partition by number order by code) rnk<br />
from yourtable<br />
) x<br />
group by number</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carlosdl</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64111</link>
		<dc:creator>Carlosdl</dc:creator>
		<pubDate>Fri, 29 May 2009 20:17:42 +0000</pubDate>
		<guid isPermaLink="false">#comment-64111</guid>
		<description>But, 

Do you really want to change the table structure ?

Depending on the data and business rules, your current structure could be a better design than the one you want to change to.  If you add a column to identify the code type, your design could support many different codes for each number without changes, but under the new design, you would need to alter the table structure (and applications) each time a new code is needed.</description>
		<content:encoded><![CDATA[<p>But, </p>
<p>Do you really want to change the table structure ?</p>
<p>Depending on the data and business rules, your current structure could be a better design than the one you want to change to.  If you add a column to identify the code type, your design could support many different codes for each number without changes, but under the new design, you would need to alter the table structure (and applications) each time a new code is needed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carlosdl</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64110</link>
		<dc:creator>Carlosdl</dc:creator>
		<pubDate>Fri, 29 May 2009 20:16:06 +0000</pubDate>
		<guid isPermaLink="false">#comment-64110</guid>
		<description>Another way:

-----------------------

create table your_table_2 as
SELECT t1.numbers,t1.c code_1,DECODE(t2.c,t1.c,null,t2.c) code_2
FROM 
(SELECT numbers,min(code) c FROM your_table GROUP BY numbers) t1,
(SELECT numbers,max(code) c FROM your_table GROUP BY numbers) t2
WHERE t1.numbers = t2.numbers;

drop table your_table;

rename your_table_2 to your_table;

-----------------------</description>
		<content:encoded><![CDATA[<p>Another way:</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>create table your_table_2 as<br />
SELECT t1.numbers,t1.c code_1,DECODE(t2.c,t1.c,null,t2.c) code_2<br />
FROM<br />
(SELECT numbers,min(code) c FROM your_table GROUP BY numbers) t1,<br />
(SELECT numbers,max(code) c FROM your_table GROUP BY numbers) t2<br />
WHERE t1.numbers = t2.numbers;</p>
<p>drop table your_table;</p>
<p>rename your_table_2 to your_table;</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kccrosser</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64105</link>
		<dc:creator>Kccrosser</dc:creator>
		<pubDate>Fri, 29 May 2009 18:46:11 +0000</pubDate>
		<guid isPermaLink="false">#comment-64105</guid>
		<description>There is also the brute force way.  Note - this will work for relatively small tables - if you have a large table you may have trouble with rollback segments.

1.  
Alter table MyTable add Code2 varchar(something)

2.  
Update MyTable MT2 
set Code2 = (select Code1 from MyTable MT1
where MT1.Number = MT2.Number and MT1.Code1 &#62; MT2.Code1)

3.
Delete from MyTable MT2
where exists (select 1 from MyTable MT1
where MT1.Number = MT2.Number and MT1.Code1 &#62; MT2.Code1)

This isn't pretty, but should achieve what you asked.  Obviously, I would back up the table before trying this.</description>
		<content:encoded><![CDATA[<p>There is also the brute force way.  Note - this will work for relatively small tables - if you have a large table you may have trouble with rollback segments.</p>
<p>1.<br />
Alter table MyTable add Code2 varchar(something)</p>
<p>2.<br />
Update MyTable MT2<br />
set Code2 = (select Code1 from MyTable MT1<br />
where MT1.Number = MT2.Number and MT1.Code1 &gt; MT2.Code1)</p>
<p>3.<br />
Delete from MyTable MT2<br />
where exists (select 1 from MyTable MT1<br />
where MT1.Number = MT2.Number and MT1.Code1 &gt; MT2.Code1)</p>
<p>This isn&#8217;t pretty, but should achieve what you asked.  Obviously, I would back up the table before trying this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carlosdl</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64067</link>
		<dc:creator>Carlosdl</dc:creator>
		<pubDate>Fri, 29 May 2009 13:56:59 +0000</pubDate>
		<guid isPermaLink="false">#comment-64067</guid>
		<description>One option could be using a function like this:

[CODE]CREATE OR REPLACE FUNCTION codes_of (p_Number in number) RETURN varchar2 IS
	l_Codes varchar2(20);
BEGIN
  FOR i IN (SELECT code FROM your_table WHERE numbers = p_number) LOOP
  	l_Codes := l_Codes &#124;&#124; i.code &#124;&#124; ',';
  END LOOP;
  RETURN SUBSTR(l_Codes,1,length(l_Codes)-1);
END;[/CODE]

Then you could use a query like this:

SELECT DISTINCT numbers,codes_of(numbers)
FROM your_table;

Notice that this will return all codes (no matter how many codes a number can have) in a single column, separated by commas.

Please let us know if this works for you, so we can put it as an answer.  If that's not what you need, let us know.</description>
		<content:encoded><![CDATA[<p>One option could be using a function like this:</p>
<pre>CREATE OR REPLACE FUNCTION codes_of (p_Number in number) RETURN varchar2 IS
	l_Codes varchar2(20);
BEGIN
  FOR i IN (SELECT code FROM your_table WHERE numbers = p_number) LOOP
  	l_Codes := l_Codes || i.code || &#8216;,&#8217;;
  END LOOP;
  RETURN SUBSTR(l_Codes,1,length(l_Codes)-1);
END;</pre>
<p>Then you could use a query like this:</p>
<p>SELECT DISTINCT numbers,codes_of(numbers)<br />
FROM your_table;</p>
<p>Notice that this will return all codes (no matter how many codes a number can have) in a single column, separated by commas.</p>
<p>Please let us know if this works for you, so we can put it as an answer.  If that&#8217;s not what you need, let us know.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lazy167</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64059</link>
		<dc:creator>Lazy167</dc:creator>
		<pubDate>Fri, 29 May 2009 07:45:26 +0000</pubDate>
		<guid isPermaLink="false">#comment-64059</guid>
		<description>so does anyone have any idea how i should go abouts doing this?</description>
		<content:encoded><![CDATA[<p>so does anyone have any idea how i should go abouts doing this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lazy167</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64053</link>
		<dc:creator>Lazy167</dc:creator>
		<pubDate>Thu, 28 May 2009 23:40:58 +0000</pubDate>
		<guid isPermaLink="false">#comment-64053</guid>
		<description>well i have created some basic sum and count function but nothing like this</description>
		<content:encoded><![CDATA[<p>well i have created some basic sum and count function but nothing like this</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lazy167</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64052</link>
		<dc:creator>Lazy167</dc:creator>
		<pubDate>Thu, 28 May 2009 21:35:22 +0000</pubDate>
		<guid isPermaLink="false">#comment-64052</guid>
		<description>how would i go abouts doing that? i've never created a function before
i'm using oracle,</description>
		<content:encoded><![CDATA[<p>how would i go abouts doing that? i&#8217;ve never created a function before<br />
i&#8217;m using oracle,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carlosdl</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/alter-table/#comment-64024</link>
		<dc:creator>Carlosdl</dc:creator>
		<pubDate>Thu, 28 May 2009 14:35:38 +0000</pubDate>
		<guid isPermaLink="false">#comment-64024</guid>
		<description>You will probably need to create a function to achieve that.

What database are you using ?</description>
		<content:encoded><![CDATA[<p>You will probably need to create a function to achieve that.</p>
<p>What database are you using ?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- dynamic -->