Excel and I am trying to normalize it. There is one could with an ID and
between 2 and 29 columns of varchar data. These columns are NULL for the
remainder of columns. here are a few rows:
1 abc def NULL NULL NULL etc...
2 qwe rty erd dsw NULL NULL etc...
3 abc fgh NULL NULL NULL NULL etc...
How do I write a query that will normalize this data?
I want something like this:
1 abc
1 def
2 qwe
2 rty
2 erd
2 dsw
3 abc
3 fgh
Thanks in advance,
TonyThis is a multi-part message in MIME format.
--=_NextPart_000_0312_01C37ECA.0AA38CD0
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Try:
select
ID
, Col
from
(
select
b.ID
, case x.Period
when 1 then b.Col1
when 2 then b.Col2
when 3 then b.Col3 -- and so on
end as Col
from
Budgets as b
cross join
(
select 1 as Period
union all
select 2
union all
select 3 -- and so on
) as x
) as y
where
Col is not null
order by
ID
-- Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
"Anthony Zessin" <Anthony.Zessin@.rrtc.com> wrote in message =news:#JrqsoufDHA.1700@.TK2MSFTNGP10.phx.gbl...
Plastic is on the bar, have a drink on me. I have a table that came =from
Excel and I am trying to normalize it. There is one could with an ID =and
between 2 and 29 columns of varchar data. These columns are NULL for =the
remainder of columns. here are a few rows:
1 abc def NULL NULL NULL etc...
2 qwe rty erd dsw NULL NULL etc...
3 abc fgh NULL NULL NULL NULL etc...
How do I write a query that will normalize this data?
I want something like this:
1 abc
1 def
2 qwe
2 rty
2 erd
2 dsw
3 abc
3 fgh
Thanks in advance,
Tony
--=_NextPart_000_0312_01C37ECA.0AA38CD0
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&
Try:
select
ID
, Col
from
(
select
b.ID
, =case x.Period
=when 1 then b.Col1
=when 2 then b.Col2
=when 3 then b.Col3 -- and so on
end =as Col
from
=Budgets as b
cross join
(
=select 1 as Period
=union all
=select 2
=union all
=select 3 -- and so on
) as x
) as y
where
Col is not null
order by
ID
-- Tom
---T=homas A. Moreau, BSc, PhD, MCSE, MCDBASQL Server MVPColumnist, SQL =Server ProfessionalToronto, ON Canadahttp://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=/sql
"Anthony Zessin" =wrote in message news:#JrqsoufDHA.1700=@.TK2MSFTNGP10.phx.gbl...Plastic is on the bar, have a drink on me. I have a table that came =fromExcel and I am trying to normalize it. There is one could with an ID andbetween 2 and 29 columns of varchar data. These columns are =NULL for theremainder of columns. here are a few rows:1 abc =def NULL NULL =NULL etc...2 qwe =rty erd dsw NULL NULL etc...3 =abc fgh NULL =NULL NULL NULL etc...How do I =write a query that will normalize this data?I want something like this:1 abc1 def2 qwe2 =rty2 erd2 dsw3 =abc3 fghThanks in advance,Tony
--=_NextPart_000_0312_01C37ECA.0AA38CD0--|||Cross-tab query / pivot table.
Code:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q175574
http://msdn.microsoft.com/library/en-us/acdata/ac_8_qd_14_04j7.asp
http://www.sqlmag.com/Articles/Index.cfm?ArticleID=15608
http://www.sqlteam.com/item.asp?ItemID=2955
Software:
http://www.rac4sql.net
http://www.ag-software.com/xp_ags_crosstab.asp
http://www.tysonsoftware.co.uk/TheQueryTool.asp
"Anthony Zessin" <Anthony.Zessin@.rrtc.com> wrote in message
news:#JrqsoufDHA.1700@.TK2MSFTNGP10.phx.gbl...
> Plastic is on the bar, have a drink on me. I have a table that came from
> Excel and I am trying to normalize it. There is one could with an ID and
> between 2 and 29 columns of varchar data. These columns are NULL for the
> remainder of columns. here are a few rows:
> 1 abc def NULL NULL NULL etc...
> 2 qwe rty erd dsw NULL NULL etc...
> 3 abc fgh NULL NULL NULL NULL etc...
> How do I write a query that will normalize this data?
> I want something like this:
> 1 abc
> 1 def
> 2 qwe
> 2 rty
> 2 erd
> 2 dsw
> 3 abc
> 3 fgh
>
> Thanks in advance,
> Tony
>
>|||An approach using UDF and cursor.
Create sample data :
drop table tt
create table tt (i int, col1 varchar(5), col2 varchar(5))
insert into tt values(1, 'val1', 'val2')
insert into tt values(2, 'val1', 'val2')
insert into tt values(3, 'val1', 'val2')
--you will have to create a function that will change columnn seperated values to the rows.
--create a function as it is no need to change it. only change the way you call it as
--shown in the following script.
CREATE FUNCTION seperator
(@.str varchar(8000), @.del varchar(1))
RETURNS @.fin_tab TABLE (val varchar(8000)) AS
BEGIN
DECLARE @.pos int
DECLARE @.final_val varchar(8000)
SELECT @.str = LTRIM(RTRIM(@.str))
IF RIGHT(@.str,1) = @.del
set @.str = LEFT(@.str, Len(@.str) - 1)
set @.pos = charindex(@.del, @.str)
WHILE @.pos > 0
BEGIN
SET @.final_val = left(@.str, charindex(@.del, @.str) - 1)
INSERT @.fin_tab VALUES(@.final_val )
select @.str = substring(@.str, @.pos + 1, len(@.str)), @.pos = charindex(@.del, @.str)
END
INSERT @.fin_tab VALUES(@.str)
RETURN
END
go
--Following is the T-SQL code to call the above function and loop through the table.
declare @.i int, @.col1 varchar(5), @.col2 varchar(5)
declare @.str varchar(8000)
if object_id('tempdb..#temp_tab') is not null
drop table #temp_tab
create table #temp_tab(i int, crostab_col varchar(5))
declare c1 cursor static for
SELECT i, col1, col2 from tt
open c1
fetch next from c1 into @.i, @.col1, @.col2
while @.@.fetch_status = 0
begin
set @.str = @.col1 + ';' + @.col2
insert into #temp_tab
select @.i idd ,val from seperator(@.str , ';')
fetch next from c1 into @.i, @.col1, @.col2
end
close c1
deallocate c1
--final table with the columns turned to rows. of the above TT table
select * from #temp_Tab
--
- Vishal|||This is a multi-part message in MIME format.
--=_NextPart_000_0015_01C37EBB.3E88E950
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Brilliant! Thank you.
"Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message =news:OsY3UuufDHA.3616@.TK2MSFTNGP11.phx.gbl...
Try:
select
ID
, Col
from
(
select
b.ID
, case x.Period
when 1 then b.Col1
when 2 then b.Col2
when 3 then b.Col3 -- and so on
end as Col
from
Budgets as b
cross join
(
select 1 as Period
union all
select 2
union all
select 3 -- and so on
) as x
) as y
where
Col is not null
order by
ID
-- Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
"Anthony Zessin" <Anthony.Zessin@.rrtc.com> wrote in message =news:#JrqsoufDHA.1700@.TK2MSFTNGP10.phx.gbl...
Plastic is on the bar, have a drink on me. I have a table that came =from
Excel and I am trying to normalize it. There is one could with an ID =and
between 2 and 29 columns of varchar data. These columns are NULL for =the
remainder of columns. here are a few rows:
1 abc def NULL NULL NULL etc...
2 qwe rty erd dsw NULL NULL etc...
3 abc fgh NULL NULL NULL NULL etc...
How do I write a query that will normalize this data?
I want something like this:
1 abc
1 def
2 qwe
2 rty
2 erd
2 dsw
3 abc
3 fgh
Thanks in advance,
Tony
--=_NextPart_000_0015_01C37EBB.3E88E950
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns:o =3D "urn:schemas-microsoft-com:office:office"><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.2800.1226" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#c0c0c0>
<DIV><FONT face=3DArial size=3D2>Brilliant! </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Thank you.</FONT></DIV>
<BLOCKQUOTE style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Tom Moreau" <<A =href=3D"mailto:tom@.dont.spam.me.cips.ca">tom@.dont.spam.me.cips.ca</A>>= wrote in message <A =href=3D"news:OsY3UuufDHA.3616@.TK2MSFTNGP11.phx.gbl">news:OsY3UuufDHA.3616=@.TK2MSFTNGP11.phx.gbl</A>...</DIV>
<DIV><FONT face=3DTahoma size=3D2>Try:</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>select</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2> ID</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>, Col</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>from</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>(</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>select<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> </SPAN>b.ID</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>,<SPAN style=3D"mso-spacerun: yes"> =</SPAN>case x.Period<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =
</SPAN>when 1 then b.Col1<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =
</SPAN>when 2 then b.Col2<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =
</SPAN>when 3 then b.Col3 -- and so on</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>end as Col</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>from<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>Budgets as b<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>cross join<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>(<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>select 1 as Period<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>union all<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>select 2<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>union all<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>select 3 -- and so on<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>) as x</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>) as y</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>where</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><o:p><FONT size=3D2> Col is not null</FONT></o:p></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>order by<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> </SPAN>ID</FONT></SPAN></P></DIV>
<DIV><BR>-- <BR>Tom</DIV>
<DIV> </DIV>
=<DIV>---<BR>T=homas A. Moreau, BSc, PhD, MCSE, MCDBA<BR>SQL Server MVP<BR>Columnist, SQL =Server Professional<BR>Toronto, ON Canada<BR><A =href=3D"www.pinnaclepublishing.com=">http://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=
/sql</A></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>"Anthony Zessin" <<A =href=3D"mailto:Anthony.Zessin@.rrtc.com">Anthony.Zessin@.rrtc.com</A>> =wrote in message <A =href=3D"news:#JrqsoufDHA.1700@.TK2MSFTNGP10.phx.gbl">news:#JrqsoufDHA.1700=@.TK2MSFTNGP10.phx.gbl</A>...</DIV>Plastic is on the bar, have a drink on me. I have a table that came from<BR>Excel and I am trying to normalize it. There is one =could with an ID and<BR>between 2 and 29 columns of varchar data. These =columns are NULL for the<BR>remainder of columns. here are a few rows:<BR><BR>1 abc =def NULL NULL =NULL etc...<BR>2 qwe =rty erd dsw NULL NULL etc...<BR>3 =abc fgh NULL =NULL NULL NULL etc...<BR><BR>How do I =write a query that will normalize this data?<BR>I want something like this:<BR><BR>1 abc<BR>1 def<BR>2 qwe<BR>2 rty<BR>2 erd<BR>2 dsw<BR>3 abc<BR>3 =fgh<BR><BR><BR>Thanks in advance,<BR>Tony<BR><BR><BR></BLOCKQUOTE></BODY></HTML>
--=_NextPart_000_0015_01C37EBB.3E88E950--|||This is a multi-part message in MIME format.
--=_NextPart_000_0025_01C37EE6.F21FBD10
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Um, about that plastic on the bar. I'd like a cold beer and a Dr. =McGillicuddy's Peppermint Schnapps - to go. ;-)
-- Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
.
"Anthony Zessin" <Anthony.Zessin@.rrtc.com> wrote in message =news:uCO9rXvfDHA.2188@.TK2MSFTNGP10.phx.gbl...
Brilliant! Thank you.
"Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message =news:OsY3UuufDHA.3616@.TK2MSFTNGP11.phx.gbl...
Try:
select
ID
, Col
from
(
select
b.ID
, case x.Period
when 1 then b.Col1
when 2 then b.Col2
when 3 then b.Col3 -- and so on
end as Col
from
Budgets as b
cross join
(
select 1 as Period
union all
select 2
union all
select 3 -- and so on
) as x
) as y
where
Col is not null
order by
ID
-- Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
"Anthony Zessin" <Anthony.Zessin@.rrtc.com> wrote in message =news:#JrqsoufDHA.1700@.TK2MSFTNGP10.phx.gbl...
Plastic is on the bar, have a drink on me. I have a table that came =from
Excel and I am trying to normalize it. There is one could with an ID =and
between 2 and 29 columns of varchar data. These columns are NULL for =the
remainder of columns. here are a few rows:
1 abc def NULL NULL NULL etc...
2 qwe rty erd dsw NULL NULL etc...
3 abc fgh NULL NULL NULL NULL etc...
How do I write a query that will normalize this data?
I want something like this:
1 abc
1 def
2 qwe
2 rty
2 erd
2 dsw
3 abc
3 fgh
Thanks in advance,
Tony
--=_NextPart_000_0025_01C37EE6.F21FBD10
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns:o =3D "urn:schemas-microsoft-com:office:office"><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.2800.1226" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#c0c0c0>
<DIV><FONT face=3DTahoma size=3D2>Um, about that plastic on the =bar. I'd like a cold beer and a Dr. McGillicuddy's Peppermint Schnapps - to go. ;-)</FONT></DIV>
<DIV><BR>-- <BR> Tom</DIV>
<DIV> </DIV>
<DIV>----<BR>Thomas A. =Moreau, BSc, PhD, MCSE, MCDBA<BR>SQL Server MVP<BR>Columnist, SQL Server Professional<BR>Toronto, ON Canada<BR><A href=3D"www.pinnaclepublishing.com=">http://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=
/sql</A><BR>.</DIV>
<DIV>"Anthony Zessin" <<A href=3D"mailto:Anthony.Zessin@.rrtc.com">Anthony.Zessin@.rrtc.com</A>> =wrote in message <A href=3D"news:uCO9rXvfDHA.2188@.TK2MSFTNGP10.phx.gbl">news:uCO9rXvfDHA.2188=@.TK2MSFTNGP10.phx.gbl</A>...</DIV>
<DIV><FONT face=3DArial size=3D2>Brilliant! </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Thank you.</FONT></DIV>
<BLOCKQUOTE style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Tom Moreau" <<A =href=3D"mailto:tom@.dont.spam.me.cips.ca">tom@.dont.spam.me.cips.ca</A>>= wrote in message <A =href=3D"news:OsY3UuufDHA.3616@.TK2MSFTNGP11.phx.gbl">news:OsY3UuufDHA.3616=@.TK2MSFTNGP11.phx.gbl</A>...</DIV>
<DIV><FONT face=3DTahoma size=3D2>Try:</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>select</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2> ID</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>, Col</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>from</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>(</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>select<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> </SPAN>b.ID</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>,<SPAN style=3D"mso-spacerun: yes"> =</SPAN>case x.Period<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =
</SPAN>when 1 then b.Col1<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =
</SPAN>when 2 then b.Col2<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =
</SPAN>when 3 then b.Col3 -- and so on</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>end as Col</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>from<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>Budgets as b<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>cross join<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>(<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>select 1 as Period<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>union all<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>select 2<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>union all<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> =</SPAN>select 3 -- and so on<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>) as x</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>) as y</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>where</FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><o:p><FONT size=3D2> Col is not null</FONT></o:p></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2>order by<o:p></o:p></FONT></SPAN></P>
<P class=3Dbodytext style=3D"MARGIN: 0in 0in 0pt"><SPAN lang=3DEN-US style=3D"FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; =mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New =Roman'"><FONT size=3D2><SPAN style=3D"mso-spacerun: yes"> </SPAN>ID</FONT></SPAN></P></DIV>
<DIV><BR>-- <BR>Tom</DIV>
<DIV> </DIV>
=<DIV>---<BR>T=homas A. Moreau, BSc, PhD, MCSE, MCDBA<BR>SQL Server MVP<BR>Columnist, SQL =Server Professional<BR>Toronto, ON Canada<BR><A =href=3D"www.pinnaclepublishing.com=">http://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=
/sql</A></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>"Anthony Zessin" <<A =href=3D"mailto:Anthony.Zessin@.rrtc.com">Anthony.Zessin@.rrtc.com</A>> =wrote in message <A =href=3D"news:#JrqsoufDHA.1700@.TK2MSFTNGP10.phx.gbl">news:#JrqsoufDHA.1700=@.TK2MSFTNGP10.phx.gbl</A>...</DIV>Plastic is on the bar, have a drink on me. I have a table that came from<BR>Excel and I am trying to normalize it. There is one =could with an ID and<BR>between 2 and 29 columns of varchar data. These =columns are NULL for the<BR>remainder of columns. here are a few rows:<BR><BR>1 abc =def NULL NULL =NULL etc...<BR>2 qwe =rty erd dsw NULL NULL etc...<BR>3 =abc fgh NULL =NULL NULL NULL etc...<BR><BR>How do I =write a query that will normalize this data?<BR>I want something like this:<BR><BR>1 abc<BR>1 def<BR>2 qwe<BR>2 rty<BR>2 erd<BR>2 dsw<BR>3 abc<BR>3 =fgh<BR><BR><BR>Thanks in advance,<BR>Tony<BR><BR><BR></BLOCKQUOTE></BODY></HTML>
--=_NextPart_000_0025_01C37EE6.F21FBD10--|||This is a multi-part message in MIME format.
--=_NextPart_000_0032_01C37EE6.FA7CB300
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
A cursor, you say? ;-)
-- Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
.
"Vishal Parkar" <vgparkar@.hotmail.com> wrote in message =news:uhz$hzufDHA.3200@.tk2msftngp13.phx.gbl...
An approach using UDF and cursor.
Create sample data :
drop table tt
create table tt (i int, col1 varchar(5), col2 varchar(5))
insert into tt values(1, 'val1', 'val2')
insert into tt values(2, 'val1', 'val2')
insert into tt values(3, 'val1', 'val2')
--you will have to create a function that will change columnn seperated =values to the rows.
--create a function as it is no need to change it. only change the way =you call it as
--shown in the following script.
CREATE FUNCTION seperator
(@.str varchar(8000), @.del varchar(1))
RETURNS @.fin_tab TABLE (val varchar(8000)) AS
BEGIN
DECLARE @.pos int
DECLARE @.final_val varchar(8000)
SELECT @.str =3D LTRIM(RTRIM(@.str))
IF RIGHT(@.str,1) =3D @.del
set @.str =3D LEFT(@.str, Len(@.str) - 1)
set @.pos =3D charindex(@.del, @.str)
WHILE @.pos > 0
BEGIN
SET @.final_val =3D left(@.str, charindex(@.del, @.str) - 1)
INSERT @.fin_tab VALUES(@.final_val )
select @.str =3D substring(@.str, @.pos + 1, len(@.str)), @.pos =3D =charindex(@.del, @.str)
END
INSERT @.fin_tab VALUES(@.str)
RETURN
END
go
--Following is the T-SQL code to call the above function and loop =through the table.
declare @.i int, @.col1 varchar(5), @.col2 varchar(5)
declare @.str varchar(8000)
if object_id('tempdb..#temp_tab') is not null
drop table #temp_tab
create table #temp_tab(i int, crostab_col varchar(5))
declare c1 cursor static for
SELECT i, col1, col2 from tt
open c1
fetch next from c1 into @.i, @.col1, @.col2
while @.@.fetch_status =3D 0
begin
set @.str =3D @.col1 + ';' + @.col2
insert into #temp_tab
select @.i idd ,val from seperator(@.str , ';')
fetch next from c1 into @.i, @.col1, @.col2
end
close c1
deallocate c1
--final table with the columns turned to rows. of the above TT table
select * from #temp_Tab
--
- Vishal
--=_NextPart_000_0032_01C37EE6.FA7CB300
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&
A cursor, you say? =;-)
-- Tom
----Thomas A. =Moreau, BSc, PhD, MCSE, MCDBASQL Server MVPColumnist, SQL Server ProfessionalToronto, ON Canadahttp://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=/sql.
"Vishal Parkar"
--=_NextPart_000_0032_01C37EE6.FA7CB300--|||This is a multi-part message in MIME format.
--=_NextPart_000_0554_01C37F38.61804C00
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
That wasn't as brilliant as yours, Tom :-)
-- - Vishal
--=_NextPart_000_0554_01C37F38.61804C00
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&
That wasn't as brilliant as yours, Tom =:-)
-- - =Vishal
--=_NextPart_000_0554_01C37F38.61804C00--