This is an academic question at this point, but I was just wondering....
(I had dozens of these tables to deal with... I was just wondering if there was a less painful way of creating the unpivoted view of the table)
I have a table with an absurd number of repeating groups
This is the query I'm trying to create:
Is there an easy(ish) way to write this in TSQL? I can find the Causality[n] columns and return the previous and next columns easily. The hard part is building the VALUES () section. If I query the sys.all_columns table, I can return the field names and order in T-SQL... nothing doing.
Do I have to loop through the columns in Currentdb().TableDefs("Toxicity").Fields collection? I mean, I could turn it into a table, but ... Then I can find the "Causality" columns and look left and right to find the related columns and build this dynamically. Can I filter the fields collection without iterating over the entire table? I mean, I could just persist that into another table and do it that way...
Is there an easier option that I'm just not seeing?
(I had dozens of these tables to deal with... I was just wondering if there was a less painful way of creating the unpivoted view of the table)
I have a table with an absurd number of repeating groups
SQL:
USE [Itchy]
GO
/****** Object: Table [dbo].[Toxicity] Script Date: 3/8/2026 2:39:09 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Toxicity](
[RecordID] [int] IDENTITY(1,1) NOT NULL,
[PatientID] [int] NOT NULL,
[Cycle] [tinyint] NOT NULL,
[ANEMIA] [tinyint] NULL,
[Causality1] [tinyint] NULL,
[Relatedness1] [tinyint] NULL,
[ALOPECIA] [tinyint] NULL,
[Causality2] [tinyint] NULL,
[Relatedness2] [tinyint] NULL,
[ANOREXIA] [tinyint] NULL,
[Causality3] [tinyint] NULL,
[Relatedness3] [tinyint] NULL
) ON [PRIMARY]
GO
This is the query I'm trying to create:
SQL:
SELECT PatientID, Cycle, Symptom, Grade, Causality, Relatedness
FROM dbo.Toxicity
CROSS APPLY (
VALUES ('Anemia', ANEMIA, Causality1, Relatedness1),
('Alopecia',ALOPECIA, Causality2, Relatedness2),
('Anorexia', ANOREXIA, Causality3, Relatedness3)
) ca(Symptom, Grade, Causality, Relatedness);
Is there an easy(ish) way to write this in TSQL? I can find the Causality[n] columns and return the previous and next columns easily. The hard part is building the VALUES () section. If I query the sys.all_columns table, I can return the field names and order in T-SQL... nothing doing.
SQL:
SELECT ao.name AS TableName, ac.name AS ColumnName, ac.column_id
FROM sys.all_objects ao
INNER JOIN sys.all_columns ac
ON ao.object_id = ac.object_id
WHERE ao.Name = 'Toxicity'
AND ao.type_desc = 'USER_TABLE';
Do I have to loop through the columns in Currentdb().TableDefs("Toxicity").Fields collection? I mean, I could turn it into a table, but ... Then I can find the "Causality" columns and look left and right to find the related columns and build this dynamically. Can I filter the fields collection without iterating over the entire table? I mean, I could just persist that into another table and do it that way...
Is there an easier option that I'm just not seeing?