January 12, 2011 at 3:59 pm
How can I turn this table:
10032 64 12/9/2010
10032 65 155
10196 64 12/22/2010
10196 65 90
10199 64 12/22/2010
10199 65 200
10200 64 12/22/2010
10200 65 123
to this table
ID [64] [65]
10032 12/9/2010 155
10196 12/22/2010 90
10199 12/22/2010 200
10200 12/22/2010 123
January 12, 2011 at 4:03 pm
Its called a CROSSTAB and I believe SQL 2008 has a function for it.. Do a Books Online Search and you should get syntax..
CEWII
January 12, 2011 at 4:10 pm
I was hoping I could get a sample query with my data.
January 12, 2011 at 4:54 pm
SELECT ID,
[64] = MAX(CASE WHEN UnknownColumnName = 64 THEN AnotherUnknownColumnName ELSE NULL END),
[65] = MAX(CASE WHEN UnknownColumnName = 65 THEN AnotherUnknownColumnName ELSE NULL END)
FROM YetAnotherTableWherePosterWantsAQueryButDidntSuppleAnyTableStructureInformation
GROUP BY ID
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2011 at 4:57 pm
WayneS (1/12/2011)
SELECT ID,
[64] = MAX(CASE WHEN UnknownColumnName = 64 THEN AnotherUnknownColumnName ELSE NULL END),
[65] = MAX(CASE WHEN UnknownColumnName = 65 THEN AnotherUnknownColumnName ELSE NULL END)
FROM YetAnotherTableWherePosterWantsAQueryButDidntSuppleAnyTableStructureInformation
GROUP BY ID
Nice table/columns ! 😛
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
January 12, 2011 at 5:05 pm
My apologies, I didn't have that information either. Thank you for the reply.
January 12, 2011 at 5:29 pm
The issue is the code needs to be dynamic as it relates to column 2 (values 64, 65 in this example)
Thanks for all your help!
January 12, 2011 at 7:12 pm
mister.magoo (1/12/2011)
WayneS (1/12/2011)
SELECT ID,
[64] = MAX(CASE WHEN UnknownColumnName = 64 THEN AnotherUnknownColumnName ELSE NULL END),
[65] = MAX(CASE WHEN UnknownColumnName = 65 THEN AnotherUnknownColumnName ELSE NULL END)
FROM YetAnotherTableWherePosterWantsAQueryButDidntSuppleAnyTableStructureInformation
GROUP BY ID
Nice table/columns ! 😛
Ya gotta play the hand that was dealt to ya! :-D:w00t:
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2011 at 7:15 pm
SQLSeTTeR (1/12/2011)
The issue is the code needs to be dynamic as it relates to column 2 (values 64, 65 in this example)Thanks for all your help!
What I did isn't really dynamic - more like cross-tab/pivot. Does it handle your situation? If so, you can get a better understanding by reading the two cross-tab/pivot table links in my signature!
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2011 at 8:14 pm
The problem why I can't use the Pivot function requires the value to be an aggregate function..these are strings.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply