I am trying to create a stored procedure which will return counts so that I can graph the results. I am using a GROUP BY ... WITH ROLLUP to create the default recordset which will have the count column, and two others (selected by the user). I would like to pass the names of the user selected columns as @Column1 and @Column2, along with the other query parameters when the stored procedure is called. Unfortunately I cannot figure out the syntax so that the resulting recordset lists all the values in @Column1 and @Column2 (currently it just prints the associated field name vice the field value. Thanks for the help.
Desired Result
Count Column1 Column2
10 EXC F14
5 FLA F14
26 ROU F14
8 EXC P3
. . .
. . .
Current Result
Count Column1 Column2
10 tblMishapFactors.[3rdLevelCode_FK] tblMishaps.Aircraft_FK
5 tblMishapFactors.[3rdLevelCode_FK] tblMishaps.Aircraft_FK
26 tblMishapFactors.[3rdLevelCode_FK] tblMishaps.Aircraft_FK
8 tblMishapFactors.[3rdLevelCode_FK] tblMishaps.Aircraft_FK
. . .
. . .
Create Procedure spGraphQuery2
(
@AC_Type varchar(100) = NULL,
@Mishap_Type varchar(50) = NULL,
@Mishap_Class varchar(50) = NULL,
@Location varchar(50) = NULL,
@Service varchar(100) = NULL,
@Year int = NULL,
@1stLevel varchar(255) = NULL,
@2ndLevel varchar(255) = NULL,
@3rdLevel varchar(255) = NULL,
@Column1 varchar(100) ='tblMishapFactors.[3rdLevelCode_FK]',
@Column2 varchar(100) ='tblMishaps.Aircraft_FK'
)
As
Set nocount on
SELECT Count(MishapID) as Count, @Column1, @Column2
FROM tblMishaps INNER JOIN tblMishapFactors ON tblMishaps.MishapID =
tblMishapFactors.MishapID_FK INNER JOIN
tblFactors ON tblMishapFactors.[3rdLevelCode_FK] = tblFactors.[3rdLevelCode]
WHERE tblMishaps.Aircraft_FK = COALESCE(@AC_Type, tblMishaps.Aircraft_FK) AND
tblMishaps.Type_FK = COALESCE(@Mishap_Type, tblMishaps.Type_FK) AND
tblMishaps.Class_FK = COALESCE(@Mishap_Class, tblMishaps.Class_FK) AND
tblMishaps.LocationID_FK = COALESCE(@Location, tblMishaps.LocationID_FK) AND
tblMishaps.OrgID_FK = COALESCE(@Service, tblMishaps.OrgID_FK) AND
Year(tblMishaps.MishapDate) = COALESCE(@Year, Year(tblMishaps.MishapDate)) AND
tblFactors.[1stLevelCode] = COALESCE(@1stLevel, tblFactors.[1stLevelCode]) AND
tblFactors.[2ndLevelCode] = COALESCE(@2ndLevel, tblFactors.[2ndLevelCode]) AND
tblMishapFactors.[3rdLevelCode_FK] = COALESCE(@3rdLevel, tblMishapFactors.[3rdLevelCode_FK])
GROUP BY @Column1, @Column2 WITH ROLLUP
Return
Desired Result
Count Column1 Column2
10 EXC F14
5 FLA F14
26 ROU F14
8 EXC P3
. . .
. . .
Current Result
Count Column1 Column2
10 tblMishapFactors.[3rdLevelCode_FK] tblMishaps.Aircraft_FK
5 tblMishapFactors.[3rdLevelCode_FK] tblMishaps.Aircraft_FK
26 tblMishapFactors.[3rdLevelCode_FK] tblMishaps.Aircraft_FK
8 tblMishapFactors.[3rdLevelCode_FK] tblMishaps.Aircraft_FK
. . .
. . .
Create Procedure spGraphQuery2
(
@AC_Type varchar(100) = NULL,
@Mishap_Type varchar(50) = NULL,
@Mishap_Class varchar(50) = NULL,
@Location varchar(50) = NULL,
@Service varchar(100) = NULL,
@Year int = NULL,
@1stLevel varchar(255) = NULL,
@2ndLevel varchar(255) = NULL,
@3rdLevel varchar(255) = NULL,
@Column1 varchar(100) ='tblMishapFactors.[3rdLevelCode_FK]',
@Column2 varchar(100) ='tblMishaps.Aircraft_FK'
)
As
Set nocount on
SELECT Count(MishapID) as Count, @Column1, @Column2
FROM tblMishaps INNER JOIN tblMishapFactors ON tblMishaps.MishapID =
tblMishapFactors.MishapID_FK INNER JOIN
tblFactors ON tblMishapFactors.[3rdLevelCode_FK] = tblFactors.[3rdLevelCode]
WHERE tblMishaps.Aircraft_FK = COALESCE(@AC_Type, tblMishaps.Aircraft_FK) AND
tblMishaps.Type_FK = COALESCE(@Mishap_Type, tblMishaps.Type_FK) AND
tblMishaps.Class_FK = COALESCE(@Mishap_Class, tblMishaps.Class_FK) AND
tblMishaps.LocationID_FK = COALESCE(@Location, tblMishaps.LocationID_FK) AND
tblMishaps.OrgID_FK = COALESCE(@Service, tblMishaps.OrgID_FK) AND
Year(tblMishaps.MishapDate) = COALESCE(@Year, Year(tblMishaps.MishapDate)) AND
tblFactors.[1stLevelCode] = COALESCE(@1stLevel, tblFactors.[1stLevelCode]) AND
tblFactors.[2ndLevelCode] = COALESCE(@2ndLevel, tblFactors.[2ndLevelCode]) AND
tblMishapFactors.[3rdLevelCode_FK] = COALESCE(@3rdLevel, tblMishapFactors.[3rdLevelCode_FK])
GROUP BY @Column1, @Column2 WITH ROLLUP
Return