Showing posts with label pivot. Show all posts
Showing posts with label pivot. Show all posts

Tuesday, March 27, 2012

Create Table from Row Data

Hello,
In SQL Server 2000, is it possible to take a table with one field (column), and pivot the table so that the characters in the row data become the field (column) names of another table ( or in a View)? The number of records could vary.

If so, how would I do this?

Sample table;
Create Table dbo.MonthlyData
(
Categories varchar(30) NOT NULL
)

Sample data;

Sales Volume 2005-02
TotRefVol 2005-02
Sales Ratio 2005-02
Sales Volume 2005-03
TotRefVol 2005-03
Sales Ratio 2005-03
Sales Volume 2005-04
TotRefVol 2005-04
Sales Ratio 2005-04

If I am following you correctly; Are you wanting Sales Volume,etc.. to be a column name in a view or table?|||

First; Sorry, the subject should have been 'Create table fields from row data'. To answer your question, each row of sample data is contained within a single column called 'Categories'.

|||

Assuming you had some other value to use with the columns you reference you could do something like:

Create Table dbo.MonthlyData

(

CategoryID int IDENTITY(1,1)

,Categories varchar(30) NOT NULL

,Value varchar(50)

)

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Volume 2005-02', 'Small')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('TotRefVol 2005-02', 'Medium')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Ratio 2005-02', 'Large')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Volume 2005-03', 'Extra Large')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('TotRefVol 2005-03', 'Small')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Ratio 2005-03', 'Medium')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Volume 2005-04', 'Large')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('TotRefVol 2005-04', 'Extra Large')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Ratio 2005-04', 'Small')

DECLARE @.string nvarchar(1000)

SELECT @.string = ISNULL(@.string + ', ', '') + QUOTENAME([Value], '''') + QUOTENAME(Categories)

FROM dbo.MonthlyData

SET @.string = 'SELECT ' + @.string

EXEC sp_executesql @.string

This will give you Categories as your column header with associated Value column.

|||

Thank you for your help, I will try this!

cdun2

|||

Thanks again. I had a couple of questions;

-Procedure sp_executesql expects parameter '@.statement' of type 'ntext/nchar/nvarchar'; nvarchar has a 'size' limit of 4000, and ntext cannot be the datatype of local variable @.string. char will handle up to 8000 characters. How can I work around these limitations?

-Could a table be created from the results of sp_executesql @.string so that the column names become fields in the table?

cdun2

|||

Yes, you could alter the syntax

@.sql = 'select ' + @.sql

to

@.sql = 'select ' + @.sql + ' into myTable'

this would keep the columns dynamic. If the number of columns will be static you could create the table and do the following:

@.sql = 'insert myTable (valuelist) select ' + @.sql

|||I realized I didn't answer the first part of your question. If the string you are trying to pass is greater than the 4000 limit of nvarchar you can replace sp_executesql with exec() and make @.string a varchar(8000). It is better practice to use sp_executesql with dynamic sql but in this case it is your only real option.

Sunday, March 11, 2012

Create Pivot Table In SQL 2005 HELP ME....

Hi all, i have this problem
im using the newest SQL 2005 and i need to create a pivot table, and my
problem is
my pivot table is something like this
COL1 COL2, COL3 , ... COLN
ROW1 1 3 0 .......
ROW2 4 2 4 . ............
ROW3 23 23 34 .........
..... .....................................
ROWN ..............................
how can i do this in SQL 2005 ? is It possible '
thsk
JSBPivot (or Unpivot) Your Data
http://www.windowsitpro.com/SQLServ...901/pg/1/1.html
David Portas
SQL Server MVP
--|||Check out,
http://vadivel.blogspot.com/2004/12...el.blogspot.com
http://thinkingms.com/vadivel
"Jo?o Santa Bárbara" wrote:

> Hi all, i have this problem
> i′m using the newest SQL 2005 and i need to create a pivot table, and my
> problem is
> my pivot table is something like this
> COL1 COL2, COL3 , ... COLN
> ROW1 1 3 0 .......
> ROW2 4 2 4 . ............
> ROW3 23 23 34 .........
> ...... .....................................
> ROWN ..............................
> how can i do this in SQL 2005 ? is It possible '
> thsk
> JSB
>
>|||thks for your sample.
but it is no good for me ..
the problem is, i dont have the number of columns could be 10 could be 100
..
and the pivot statement requires that explicity we say the columns for
pivoting.
thsk
JSB
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1131363888.340562.246280@.g43g2000cwa.googlegroups.com...
> Pivot (or Unpivot) Your Data
> http://www.windowsitpro.com/SQLServ...901/pg/1/1.html
> --
> David Portas
> SQL Server MVP
> --
>|||> the problem is, i dont have the number of columns could be 10 could be
> 100 ..
> and the pivot statement requires that explicity we say the columns for
> pivoting.
That's right. If you don't know the number of columns, you won't be able to
use PIVOT. See the links from http://www.aspfaq.com/2462 ...|||Jo=E3o Santa B=E1rbara wrote:
> thks for your sample.
> but it is no good for me ..
> the problem is, i don=B4t have the number of columns could be 10 could b=
e 100
> ..
> and the pivot statement requires that explicity we say the columns for
> pivoting.
> thsk
> JSB
>
> "David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
> news:1131363888.340562.246280@.g43g2000cwa.googlegroups.com...
tml
Then you can't use static SQL because a query always returns a fixed
number of columns. That's why the best solution is usually to do this
in your reporting tool / presentation app.
--=20
David Portas
SQL Server MVP
--|||Try out the RAC utility @.
www.rac4sql.net
"Joo Santa Brbara" <joaosb@.i24portugal.com> wrote in message
news:upukG844FHA.252@.TK2MSFTNGP15.phx.gbl...
> Hi all, i have this problem
> im using the newest SQL 2005 and i need to create a pivot table, and my
> problem is
> my pivot table is something like this
> COL1 COL2, COL3 , ... COLN
> ROW1 1 3 0 .......
> ROW2 4 2 4 . ............
> ROW3 23 23 34 .........
> ..... .....................................
> ROWN ..............................
> how can i do this in SQL 2005 ? is It possible '
> thsk
> JSB
>
>