July 24, 2012 at 3:16 am
After thinking again and scratching my head decided to post in the forum
My table structure looks like this
CREATE TABLE #Test
(
ProductID INT NULL,
Name NVARCHAR(50),
ProductNumber NVARCHAR(25),
MakeFlag BIT null,
FinishedGoodsFlag BIT null,
Color BIT null,
SafetyStockLevel SMALLINT NULL
)
INSERT #Test(ProductID,Name,ProductNumber,MakeFlag,FinishedGoodsFlag,Color,SafetyStockLevel)
SELECT '716','Long-Sleeve Logo Jersey','XL','LJ-0192-X','0','1','Multi,4'
i want the result to look like this
ProductID ->716
Name->Long-Sleeve Logo Jersey, XL
ProductNumber->LJ-0192-X
MakeFlag>0
FinishedGoodsFlag->1
Color->Multi
SafetyStockLevel->4
July 24, 2012 at 3:19 am
Look up UNPIVOT in Books Online
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 24, 2012 at 3:29 am
Your table schema and the insert values are not matching. Please check and re-post the values properly.
Regards,
Karthik.
SQL Developer.
July 24, 2012 at 7:32 pm
After only minor fixes to your DDL and sample INSERT, I propose the following as your solution instead of UNPIVOT.
CREATE TABLE #Test
(
ProductID INT NULL,
Name NVARCHAR(50),
ProductNumber NVARCHAR(25),
MakeFlag BIT null,
FinishedGoodsFlag BIT null,
Color VARCHAR(10),
SafetyStockLevel SMALLINT NULL
)
INSERT #Test(ProductID,Name,ProductNumber,MakeFlag,FinishedGoodsFlag,Color,SafetyStockLevel)
SELECT '716','Long-Sleeve Logo Jersey, XL','LJ-0192-X','0','1','Multi',4
--ProductID ->716
--Name->Long-Sleeve Logo Jersey, XL
--ProductNumber->LJ-0192-X
--MakeFlag>0
--FinishedGoodsFlag->1
--Color->Multi
--SafetyStockLevel->4
SELECT attribute, value
FROM #Test
CROSS APPLY (
VALUES ('ProductID', CAST(ProductID AS NVARCHAR(50)))
,('Name', Name)
,('ProductNumber', CAST(ProductNumber AS NVARCHAR(50)))
,('MakeFlag', CAST(MakeFlag AS NVARCHAR(50)))
,('FinishedGoodsFlag', CAST(FinishedGoodsFlag AS NVARCHAR(50)))
,('Color', CAST(Color AS NVARCHAR(50)))
,('SafetyStockLevel', CAST(SafetyStockLevel AS NVARCHAR(50)))
) a(attribute, value)
DROP TABLE #Test
I happen to love the CROSS APPLY VALUES approach to UNPIVOT because I think the syntax of UNPIVOT is unwieldy and I've tested and found a slight improvement in speed using the "other UNPIVOT" approach.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply