Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Transpose a Table

Author  Topic 

timgaunt
Posting Yak Master

115 Posts

Posted - 2005-08-12 : 13:53:57
Hi,

I've started going around in circles here, but bascially, I have a SProc which generates a temp table with the results:


ProductName InStock Purchased
Product1 746 24
Product2 732 NULL
Product3 1000 50
Product4 0 17
Product5 0 NULL
Product6 0 18


The table is basically built on the fly from the orders table using an agregate function (the InStock value is taken straght from the Stock table) but for a graph I want to make the data:


Product1 Product2 etc
InStock 746 732
Purchased 24 NULL


I've been going through a variaty of example SProcs which make pivot tables but thats not quite what I'm looking for, I just want to turn the data around hehe. Any advise would greatly be apprechiated, if it helps here's the SProc that makes the data (and all its joins) I'm probably going around the houses with this too:


create table #StockLevels
(
CategoryID int,
CategoryName nvarchar(50),
CurrentStock int,
BoughtStock int
)
--Simply get all the current stock levels
INSERT INTO #StockLevels
SELECT
CMRC_Categories.CategoryID,
CategoryName,
CurrentStock,
0

FROM
CMRC_Categories LEFT JOIN CMRC_StockLevels
ON CMRC_Categories.CategoryID = CMRC_StockLevels.CategoryID

GROUP BY
CMRC_Categories.CategoryID,
CategoryName,
CurrentStock

--Update the temp table with only the orders which have been completed
UPDATE
#StockLevels
SET
BoughtStock =
(
SELECT
SUM(dbo.CMRC_OrderDetails.Quantity * dbo.CMRC_Products.Quantity) AS BoughtStock
FROM dbo.CMRC_Orders INNER JOIN
dbo.CMRC_OrderDetails ON dbo.CMRC_Orders.OrderID = dbo.CMRC_OrderDetails.OrderID RIGHT OUTER JOIN
dbo.CMRC_Products ON dbo.CMRC_OrderDetails.ProductID = dbo.CMRC_Products.ProductID
WHERE
(dbo.CMRC_Orders.CompletedDate IS NULL)
AND
(CMRC_Products.CategoryID = #StockLevels.CategoryID)
GROUP BY
dbo.CMRC_Products.CategoryID
HAVING
SUM(dbo.CMRC_OrderDetails.Quantity * dbo.CMRC_Products.Quantity) >0
)

SELECT
CategoryName,
CurrentStock,
BoughtStock
FROM
#StockLevels

Drop table #StockLevels


Thanks

Tim

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-08-12 : 14:11:17
Something like this?

SELECT
SUM(CASE WHEN ProductName = 'Product1' THEN InStock END) AS [Product1],
SUM(CASE WHEN ProductName = 'Product2' THEN InStock END) AS [Product2],
SUM(CASE WHEN ProductName = 'Product3' THEN InStock END) AS [Product3],
SUM(CASE WHEN ProductName = 'Product4' THEN InStock END) AS [Product4],
SUM(CASE WHEN ProductName = 'Product5' THEN InStock END) AS [Product5],
SUM(CASE WHEN ProductName = 'Product6' THEN InStock END) AS [Product6]
FROM #stocklevels
UNION
SELECT
SUM(CASE WHEN ProductName = 'Product1' THEN Purchased END) AS [Product1],
SUM(CASE WHEN ProductName = 'Product2' THEN Purchased END) AS [Product2],
SUM(CASE WHEN ProductName = 'Product3' THEN Purchased END) AS [Product3],
SUM(CASE WHEN ProductName = 'Product4' THEN Purchased END) AS [Product4],
SUM(CASE WHEN ProductName = 'Product5' THEN Purchased END) AS [Product5],
SUM(CASE WHEN ProductName = 'Product6' THEN Purchased END) AS [Product6]
FROM #stocklevels
Go to Top of Page

timgaunt
Posting Yak Master

115 Posts

Posted - 2005-08-12 : 15:10:30
Hi,

Yeah that in theory would produce the results how I need them however one thing I didn't perhaps make clear was that there's a varying number of products but you've also given me part of a solution so will have more of a play.

Any other advise would be great though thanks.

Tim
Go to Top of Page

timgaunt
Posting Yak Master

115 Posts

Posted - 2005-08-12 : 15:58:27
Got it sortde thanks for your help.

Tim
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-13 : 00:38:38
See more Cross tabs here
http://weblogs.sqlteam.com/jeffs/archive/2005/05.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

magesh
Starting Member

23 Posts

Posted - 2005-08-16 : 06:25:27
Hi,

you may also use dynamic SQL for cross tabs..
here is an example ...

DDL:

CREATE TABLE [Prod_INOUT] (
[ProductName] [varchar] (50) COLLATE Japanese_CI_AS NULL ,
[InStock] [int] NULL ,
[Purchased] [int] NULL
) ON [PRIMARY]
GO


Query:

------------------------
DECLARE @SQLMAIN AS VARCHAR(8000)
DECLARE @SQL1 AS VARCHAR(200)

SET @SQL1 =' ,(SELECT Instock FROM Prod_INOUT where ProductName= '''
SET @SQLMAIN = 'SELECT ''InStock'' AS InStock' + @SQL1
SELECT @SQLMAIN = @SQLMAIN + ProductName + ''') AS ' + ProductName + @SQL1 FROM Prod_INOUT
SET @SQLMAIN= LEFT(@SQLMAIN,LEN(@SQLMAIN)-LEN(@SQL1)) + ' UNION ALL '

SET @SQL1 =' ,(SELECT Purchased FROM Prod_INOUT where ProductName= '''
SET @SQLMAIN = @SQLMAIN + 'SELECT ''Purchased'' AS Purchased' + @SQL1
SELECT @SQLMAIN = @SQLMAIN + ProductName + ''') AS ' + ProductName + @SQL1 FROM Prod_INOUT
SET @SQLMAIN= LEFT(@SQLMAIN,LEN(@SQLMAIN)-LEN(@SQL1))

EXEC (@SQLMAIN)

--------------------

warning : since we are using varchar(8000) there shuld be only limited Products

Magesh
Go to Top of Page
   

- Advertisement -