﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Unpivot question, struggling with data please help / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 12:23:07 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Unpivot question, struggling with data please help</title><link>http://www.sqlservercentral.com/Forums/Topic1353268-392-1.aspx</link><description>Let me explain a little further about the CROSS APPLY.[code="sql"]CROSS APPLY (    VALUES (Column1a, Column2a, Column3a, Column4a)        ,(Column1b, Column2b, Column3b, Column4b)        ,(Column1c, Column2c, Column3c, Column4c)        ,(Column1d, Column2d, Column3d, Column4d)) alphas(col1, col2, col3, col4)[/code]Looking down through the columns constructed by VALUES, for col1 it consists of Column1a, Column1b, Column1c and Column1d.  So all of these must be of the same data type (or CAST to the same data type).To add more columns, e.g., e, f and g, you'd want something like:[code="sql"]CROSS APPLY (    VALUES (Column1a, Column2a, Column3a, Column4a)        ,(Column1b, Column2b, Column3b, Column4b)        ,(Column1c, Column2c, Column3c, Column4c)        ,(Column1d, Column2d, Column3d, Column4d)        ,(Column1e, Column2e, Column3e, Column4e)        ,(Column1f, Column2f, Column3f, Column4f)        ,(Column1g, Column2g, Column3g, Column4g)       ) alphas(col1, col2, col3, col4)[/code]The number of columns produced in table "alphas" corresponds to the number of columns appearing in each () grouping of the VALUES set (Table Row Constructor).</description><pubDate>Sun, 02 Sep 2012 21:04:00 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Unpivot question, struggling with data please help</title><link>http://www.sqlservercentral.com/Forums/Topic1353268-392-1.aspx</link><description>[quote][b]Dwight617 (9/2/2012)[/b][hr]Sorry dwain  .. dataINSERT INTO [Table]SELECT 1234, 1, 'True', 'False', 'False', 2, 'False', 'False', 'False', 3, 'True', 'True', 'True', 4, 'False', 'False', 'False'One other complication, looking to filter and only show column1,2,3,4    When 2 or 3, or 4  = 'True'Result should be something likerecordid column1, column2, column3, column41234       1           true          f         f1234       3            true        true      trueLooking at your cross apply now.  Thanks for the link hadn't seen that.  [/quote]Ah!  Clarity![code="sql"]DECLARE @t TABLE ( [record_id] [int]  NOT NULL, [Column1a] [int] NULL, [Column2a] [nvarchar](30) NULL, [Column3a] [nvarchar](30) NULL, [Column4a] [nvarchar](30) NULL, [Column1b] [int] NULL, [Column2b] [nvarchar](30) NULL, [Column3b] [nvarchar](30) NULL, [Column4b] [nvarchar](30) NULL, [Column1c] [int] NULL, [Column2c] [nvarchar](30) NULL, [Column3c] [nvarchar](30) NULL, [Column4c] [nvarchar](30) NULL, [Column1d] [int] NULL, [Column2d] [nvarchar](30) NULL, [Column3d] [nvarchar](30) NULL, [Column4d] [nvarchar](30) NULL)INSERT INTO @tSELECT 1234, 1, 'True', 'False', 'False', 2, 'False', 'False', 'False', 3, 'True', 'True', 'True', 4, 'False', 'False', 'False'SELECT record_id, col1, col2, col3, col4 FROM @tCROSS APPLY (    VALUES (Column1a, Column2a, Column3a, Column4a)        ,(Column1b, Column2b, Column3b, Column4b)        ,(Column1c, Column2c, Column3c, Column4c)        ,(Column1d, Column2d, Column3d, Column4d)) alphas(col1, col2, col3, col4)WHERE col2 = 'True' OR col3 = 'True' OR col4 = 'True'[/code][b]Edit[/b]: Minor but important correction to the query.</description><pubDate>Sun, 02 Sep 2012 19:28:57 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Unpivot question, struggling with data please help</title><link>http://www.sqlservercentral.com/Forums/Topic1353268-392-1.aspx</link><description>Sorry dwain  .. dataINSERT INTO [Table]SELECT 1234, 1, 'True', 'False', 'False', 2, 'False', 'False', 'False', 3, 'True', 'True', 'True', 4, 'False', 'False', 'False'One other complication, looking to filter and only show column1,2,3,4    When 2 or 3, or 4  = 'True'Result should be something likerecordid column1, column2, column3, column41234       1           true          f         f1234       3            true        true      trueLooking at your cross apply now.  Thanks for the link hadn't seen that.  </description><pubDate>Sun, 02 Sep 2012 19:15:54 GMT</pubDate><dc:creator>Dwight617</dc:creator></item><item><title>RE: Unpivot question, struggling with data please help</title><link>http://www.sqlservercentral.com/Forums/Topic1353268-392-1.aspx</link><description>You're a bit scant on sample data, but we'll take a stab at it anyway.  You can see in this example how to UNPIVOT single or multiple columns, so you should be able to manipulate it to get it where you need it to be.[code="sql"]DECLARE @t TABLE ( [record_id] [int] IDENTITY(1,1) NOT NULL, [Column1a] [int] NULL, [Column2a] [nvarchar](30) NULL, [Column3a] [nvarchar](30) NULL, [Column4a] [nvarchar](30) NULL, [Column1b] [int] NULL, [Column2b] [nvarchar](30) NULL, [Column3b] [nvarchar](30) NULL, [Column4b] [nvarchar](30) NULL)INSERT INTO @tSELECT 1, 'A', 'B', 'C', 2, 'D', 'E', 'F'SELECT record_id, num, alpha1, alpha2 FROM @tCROSS APPLY (    VALUES (Column1a), (Column1b)) nums(num)CROSS APPLY (    VALUES (Column2a, Column2b), (Column3a, Column3b), (Column4a, Column4b)) alphas(alpha1, alpha2)[/code]You can read about the CROSS APPLY VALUES approach to UNPIVOT here:[url]http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/[/url]The discussion thread has some good information on performance compared to UNPIVOT.</description><pubDate>Sun, 02 Sep 2012 18:54:44 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Unpivot question, struggling with data please help</title><link>http://www.sqlservercentral.com/Forums/Topic1353268-392-1.aspx</link><description>Not an option at this point.  Again it's from a form.  The form was designed like a grid.  Any thoughts?</description><pubDate>Sun, 02 Sep 2012 17:40:30 GMT</pubDate><dc:creator>Dwight617</dc:creator></item><item><title>RE: Unpivot question, struggling with data please help</title><link>http://www.sqlservercentral.com/Forums/Topic1353268-392-1.aspx</link><description>Yuck! But what does it mean? What is it supposed to model? This needs to  be thrown out so you start over.</description><pubDate>Sun, 02 Sep 2012 17:26:29 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Unpivot question, struggling with data please help</title><link>http://www.sqlservercentral.com/Forums/Topic1353268-392-1.aspx</link><description>Celko- thanks for the response.  Archaic is right on the money.  Your guess is right on about the structure.  I was wrong it's actually nvarchar(30) instead of bit.  Holding "True" and "False" data and the int columns are an ID.  (but no foreign key constraint... of course)   [record_id] [int] IDENTITY(1,1) NOT NULL,		[Column1a] [int] NULL,	[Column2a] [nvarchar](30) NULL,	[Column3a] [nvarchar](30) NULL,	[Column4a] [nvarchar](30) NULL,	[Column1b] [int] NULL,	[Column2b] [nvarchar](30) NULL,	[Column3b] [nvarchar](30) NULL,	[Column4b] [nvarchar](30) NULL,</description><pubDate>Sun, 02 Sep 2012 16:37:20 GMT</pubDate><dc:creator>Dwight617</dc:creator></item><item><title>RE: Unpivot question, struggling with data please help</title><link>http://www.sqlservercentral.com/Forums/Topic1353268-392-1.aspx</link><description>&amp;gt;&amp;gt; I have a table with the following structure &amp;lt;&amp;lt;Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. The use of BIT is a nightmare of non-RDBMS mindsets and we need to know if it is NULL-able or not. I also loved the use of “record_id” for a data element. Still stuck in punch cards and mag tapes. Here is my guess:CREATE TABLE Garbage(record_id INTEGER NOT NULL PRIMARY KEY,  column1a INTEGER NOT NULL,  column2a BIT DEFAULT CAST (0 AS BIT) NOT NULL, column3a BIT DEFAULT CAST (0 AS BIT) NOT NULL, column4a BIT DEFAULT CAST (0 AS BIT) NOT NULL,  column1b INTEGER NOT NULL,  column2b BIT DEFAULT CAST (0 AS BIT) NOT NULL,  column3b BIT DEFAULT CAST (0 AS BIT) NOT NULL,  column4b BIT DEFAULT CAST (0 AS BIT) NOT NULL);&amp;gt;&amp;gt; it's one of those forms converted to a table... I know terrible format. Anyway, I need to change it to: record_id, column1, column2, column3, column4abcde... etc. &amp;lt;&amp;lt;No, you need to fire the moron that wrote this and re-do all his code. Then you need to put it into a normalized table  with usable data element names. This is just a spreadsheet and not something you could use  in s correct RDBMS. Again, without specs, I would guess that each block of {column1x INTEGER NOT NULL,  column2x BIT DEFAULT CAST (0 AS BIT) NOT NULL, column3x BIT DEFAULT CAST (0 AS BIT) NOT NULL, column4x BIT DEFAULT CAST (0 AS BIT) NOT NULL}is an attribute splitting problem that is more like a hand grenade than we usually see in a bad design. I think these are really a single attribute of whatever this thing is supposed to model. Can you explain what this means? </description><pubDate>Sun, 02 Sep 2012 16:07:19 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>Unpivot question, struggling with data please help</title><link>http://www.sqlservercentral.com/Forums/Topic1353268-392-1.aspx</link><description>Have a table with the following structure ..recordid (int), column1a (int), column2a (bit), columns3a (bit), column4a (bit), column1b (int), column2b (bit), column3b (bit), column4b (bit)up to 100.   (it's one of those forms converted to a table... i know terrible format) Anyway, I need to change it to.recordid, column1, column2, column3, column4abcde... etc.  The unpivot seems like the best solution, but I can't figure out how to group more than 1 column.  Any help would be very much appreciated.</description><pubDate>Sun, 02 Sep 2012 12:49:02 GMT</pubDate><dc:creator>Dwight617</dc:creator></item></channel></rss>