Saturday, September 7, 2013

[T-SQL] Pivot on changing columns

[T-SQL] Pivot on changing columns


Pivot on changing columns

Posted: 06 Sep 2013 05:01 AM PDT

It doesn't look like Microsoft made it easy to pivot (like Matrix in SSRS) when the number of columns you end up with depends on the data.Using the below data.. if I run by Destination.. when I choose Russell I would need to place 281, 282, and 374 in the pivot statement, but if I am running for Raceland I only need 490, and LEX.The data I am working with will have between 10 and 75 columns.. and will depend on the data. I have not been able to find any solution that I can follow for how to pivot this data.Note that I am using a CTE to pull the data together.. so it looks like the below.. and would need to pivot this on Code.CREATE TABLE [dbo].[a_dcp]( [Destination_TX] [varchar](100) NULL, [Code] [varchar](128) NULL, [Arrived] [int] NULL, [In_Trans] [int] NULL) ON [PRIMARY] insert into a_dcp values ('Russell','281','0','1') insert into a_dcp values ('Russell','282','5','3') insert into a_dcp values ('Russell','281','9','1') insert into a_dcp values ('Ashland','281','11','9') insert into a_dcp values ('Ashland','374','1','15') insert into a_dcp values ('Ashland','490','10','1') insert into a_dcp values ('Ashland','Casco','1','1') insert into a_dcp values ('Raceland','490','13','12') insert into a_dcp values ('Raceland','Lex','30','22')

SELECT???? Query idea???

Posted: 06 Sep 2013 08:15 AM PDT

Hello,I have 1 table with some fields in it. Two of the fields are important to me.I need to check if for each value in field 1 I have more than 1 value in field 2...For example:Customer Contract1 (Peter) A2 (John) N2 (John) P3 (Lisa) SSo basically how can I identify that for customer John I have 2 contracts N and P?Thanks in advance!

Help with Output in a Merge Statement

Posted: 06 Sep 2013 06:38 AM PDT

Hi Everyone I have a merge statement which ends with the followingWHEN NOT MATCHED BY SOURCE THEN DELETE;Now I need to record the deleted records in a table and I am having no luck getting the output to work. The records I need to record look like the following.OID (int)Version (int)QK varchar(32)GD varchar(32)Geometry (Geometry)and I would like to add a date column to record when the delete took place.

Updating a Temp Table column with data from another column in same Temp Table (data from previous month)

Posted: 06 Sep 2013 06:24 AM PDT

Using T-SQL (SS2K8) in a stored procedure, I have created a temp table (#Month) with (Month, Year, Department, Count1, Count2, CurrPerc, and PrevPerc)I am trying to update that table with data (CurrPerc) from the previous month in the new column (the previous month's data in PrevPerc) UPDATE #Month SET [PrevPerc] = [CurrPerc] WHERE [MONTH] = (IF [MONTH] = 1 THEN 12 ELSE [MONTH] - 1 END WHEN [MONTH] = 1 THEN T.[YEAR] - 1 ELSE T.[YEAR] END INNER JOIN #Month P ON [DEPARTMENT] = P.DEPARTMENT I know my T-Sql is off, so any help would be great. Thanks!

No comments:

Post a Comment

Search This Blog