Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Transpose 1 datarow from Hor to vert w/o using tbl var or ..

 
   Database Help (Home) -> Programming RSS
Next:  Strange Connection Issue  
Author Message
Rich

External


Since: Feb 07, 2008
Posts: 112



(Msg. 1) Posted: Wed May 19, 2010 12:33 pm
Post subject: Transpose 1 datarow from Hor to vert w/o using tbl var or #tmp tbl
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have to populate a list (vertically) for a report (in Reporting Services)
with content from a single row (the selected row) of a (non-normalized)
table. Some of the fields from the selected row may be empty. I have to
exclude these fields from the list.

I was originally hardcoding the fields in the Report list (stacking
textboxes vertically and referencing the respective field from the report
dataset), but this was leaving gaps in the list (showing empty fields) if not
all the fields contained a value. So I decided to transpose the row data of
the selected row from a horizontal orientation to a vertical orientation
(where each field is its own row) by adding each field value to a table var
which only contains one field (one column) and then selecting only the rows
from that table var where the field was not empty. The following snippet
shows how I do this, but I want to know if this can be done without using a
table var (or #tmp table). And I need to exclude the empty fields:

declare @s1 table(Item varchar(400))

insert into @s1 select top 1 contact from tbl1 where subs = 14360 order by
invoice
insert into @s1 select top 1 title from tbl1 where subs = 14360 order by
invoice
insert into @s1 select top 1 firm from tbl1 where subs = 14360 order by
invoice
insert into @s1 select top 1 address from tbl1 where subs = 14360 order by
invoice
insert into @s1 select top 1 city + ', ' + state + ' ' + zip from tbl1
where subs = 14360 order by invoice
insert into @s1 select top 1 subs from tbl1 where subs = 14360 order by
invoice

select * from @s1 where len(item) > 0

My goal is to eliminate the Insert Into statement so that I don't have to
stuff this into a stored procedure. How to do this (sql server 2000 or
2005)?

Thanks

 >> Stay informed about: Transpose 1 datarow from Hor to vert w/o using tbl var or .. 
Back to top
Login to vote
John Bell

External


Since: Jan 11, 2008
Posts: 157



(Msg. 2) Posted: Wed May 19, 2010 6:25 pm
Post subject: Re: Transpose 1 datarow from Hor to vert w/o using tbl var or #tmp tbl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Wed, 19 May 2010 12:33:01 -0700, Rich
wrote:

>I have to populate a list (vertically) for a report (in Reporting Services)
>with content from a single row (the selected row) of a (non-normalized)
>table. Some of the fields from the selected row may be empty. I have to
>exclude these fields from the list.
>
>I was originally hardcoding the fields in the Report list (stacking
>textboxes vertically and referencing the respective field from the report
>dataset), but this was leaving gaps in the list (showing empty fields) if not
>all the fields contained a value. So I decided to transpose the row data of
>the selected row from a horizontal orientation to a vertical orientation
>(where each field is its own row) by adding each field value to a table var
>which only contains one field (one column) and then selecting only the rows
>from that table var where the field was not empty. The following snippet
>shows how I do this, but I want to know if this can be done without using a
>table var (or #tmp table). And I need to exclude the empty fields:
>
>declare @s1 table(Item varchar(400))
>
>insert into @s1 select top 1 contact from tbl1 where subs = 14360 order by
>invoice
>insert into @s1 select top 1 title from tbl1 where subs = 14360 order by
>invoice
>insert into @s1 select top 1 firm from tbl1 where subs = 14360 order by
>invoice
>insert into @s1 select top 1 address from tbl1 where subs = 14360 order by
>invoice
>insert into @s1 select top 1 city + ', ' + state + ' ' + zip from tbl1
>where subs = 14360 order by invoice
>insert into @s1 select top 1 subs from tbl1 where subs = 14360 order by
>invoice
>
>select * from @s1 where len(item) > 0
>
>My goal is to eliminate the Insert Into statement so that I don't have to
>stuff this into a stored procedure. How to do this (sql server 2000 or
>2005)?
>
>Thanks
Hi

You could do something similar to:

select top 1 CAST(contact as varchar(400)) AS item from tbl1 where
subs = 14360 and NULLIF(contact,'') IS NOT NULL order by invoice
UNION ALL select top 1 CAST(title as varchar(400)) from tbl1 where
subs = 14360 and NULLIF(title,'') IS NOT NULL order by invoice
UNION ALL select top 1 CAST(firm as varchar(400)) from tbl1 where
subs = 14360 and NULLIF(firm,'') IS NOT NULL order by invoice
UNION ALL select top 1 CAST(address as varchar(400)) from tbl1 where
subs = 14360 and NULLIF(address,'') IS NOT NULL order by invoice
UNION ALL select top 1 CAST(city + ', ' + state + ' ' + zip as
varchar(400)) from tbl1
where subs = 14360 and NULLIF(city + ', ' + state + ' ' + zip,'') IS
NOT NULL order by invoice
UNION ALL select top 1 CAST(subs as varchar(400)) from tbl1 where
subs = 14360 and NULLIF(subs,'') IS NOT NULL order by invoice

John

 >> Stay informed about: Transpose 1 datarow from Hor to vert w/o using tbl var or .. 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 672



(Msg. 3) Posted: Wed May 19, 2010 11:13 pm
Post subject: Re: Transpose 1 datarow from Hor to vert w/o using tbl var or #tmp tbl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On SQL Server 2005 you can use UNPIVOT:

SELECT item
FROM (
SELECT TOP (1) CAST(contact AS VARCHAR(400)) AS contact,
CAST(title AS VARCHAR(400)) AS title,
CAST(firm AS VARCHAR(400)) AS firm,
CAST(address AS VARCHAR(400)) AS address,
CAST(city + ', ' + state + ' ' + zip AS VARCHAR(400))
AS address2,
CAST(subs AS VARCHAR(400)) AS subs
FROM tbl1
WHERE subs = 14360
ORDER BY invoice) AS T
UNPIVOT
(item FOR col IN (contact, title, firm, address, address2, subs)) AS
U;

--
Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Transpose 1 datarow from Hor to vert w/o using tbl var or .. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
need help with query - I want to sum the records in a table (u) which have 'P.M.' in a field value and test against a fixed limit something to the effect of : case when SUM(substring(u.sTime,1,4)='P.M.') <= 64 Then .... except it should work ...

Next?? - I have a field stored as NTEXT(16). I need to replace some of the content in this field. I know the replace function doesnt work. I see there is UPDATETEXT - is this the ONLY way to do it ?? 'cos it looks quite complicated? thanks, Justin

How can I get the resultset from a stored procedure - I have a stored procedure which will output a table. How can I use a sql statement to get the result of the sp? Thanks a lot

Audit V2 - I am looking to write a SQL script that will show me for each table, in all the databases on a server, which users / groups have access. In addition, I want to break the access into whether it is SELECT, UPDATE, INSERT or DELETE permissions. Is this..

Detecting an existence of local temporary table - Hi, I create a temporary table. for example: <font color=purple> ; create table #t1 (ID_T1 integer)</font> Next I would like to detect If the table already exists: exec sp_table #t1 if @@ROWCOUNT > 0 <font color=purple>&...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]