SQL 2008 Filtering on distinct objects from a Pivot
When I try and use distinct to filter I recieve an error. I have tried to insert:
DISTINCT [Product_Code] = @MyProduct
in to my query but receive error:
The report parameter ‘MyProduct’ has a DefaultValue or a ValidValue that depends on the report parameter “MyProduct”.
Forward dependencies are not valid.
So then I tried to create a new dataset with:
SELECT DISTINCT [Product_Code]
FROM pvt
ORDER BY [Product_Code]
and recieve error:Invalid object name 'pvt'
So Itried to use the original table name CAT2.CCPSamples in place of pvt but recieve:
Invalid column name 'Product_Code'
How should I be using the Distinct to filter based on Pivot results? Original Query is below.
;with cte as (SELECT CAT2.Employees.EMPNAME AS [Employee Name],
dateadd(minute,datediff(minute,'20000101',F.[Date/Time]),'20000101') as [Date_Time], CAT2.CCPsamples.MEASURE AS Measure,
CAT2.CCPsamples.SAMPLEVALUE AS Result,CAT2.CCPsamples.SAMPLECOMMENT AS Comments
FROM CAT2.Employees INNER JOIN
CAT2.CCPsamples ON CAT2.Employees.EMPLOYEENO = CAT2.CCPsamples.EMPLOYEENO INNER JOIN
CAT2.Activities ON CAT2.CCPsamples.ACTIVITY_NAME = CAT2.Activities.ACTIVITY_NAME
CROSS APPLY (select dbo.SQLDateTime(CAT2.CCPsamples.SAMPLEDATE, CAT2.CCPsamples.SAMPLETIME)
AS [Date/Time]) F
WHERE F.[Date/Time] >= @MyStartDate AND
F.[Date/Time] <= @MyEndDate AND
CAT2.Activities. DEPARTMENT = 381)
select [Employee Name] as [Employee Name], [Date_Time] as [Date_Time],[Percent Fat] as [Percent Fat],[Percent Lean] as [Percent Lean],[Product Code] as [Product Code],[Comments] as [Comments],
case when cast([In Spec] as int) = 0 then 'Yes' else Null end as [In Spec]
from cte
pivot (sum(result)
FOR Measure IN ([Percent Fat],[Percent Lean],[Product Code],[In Spec])) AS pvt
order by Date_Time
David Donovan
February 15th, 2011 4:14pm
Hi David,
I am not sure that i understand you exactly. Did you want to eliminate the duplicate rows? If yes, you can do it either in the query or on report,
in the query, write more common table expressions like this:
;with cte1 as (SELECT CAT2.Employees.EMPNAME AS [Employee Name],
dateadd(minute,datediff(minute,'20000101',F.[Date/Time]),'20000101') as [Date_Time], CAT2.CCPsamples.MEASURE AS Measure,
CAT2.CCPsamples.SAMPLEVALUE AS Result,CAT2.CCPsamples.SAMPLECOMMENT AS Comments
FROM CAT2.Employees INNER JOIN
CAT2.CCPsamples ON CAT2.Employees.EMPLOYEENO = CAT2.CCPsamples.EMPLOYEENO INNER JOIN
CAT2.Activities ON CAT2.CCPsamples.ACTIVITY_NAME = CAT2.Activities.ACTIVITY_NAME
CROSS APPLY (select dbo.SQLDateTime(CAT2.CCPsamples.SAMPLEDATE, CAT2.CCPsamples.SAMPLETIME)
AS [Date/Time]) F
WHERE F.[Date/Time] >= @MyStartDate AND
F.[Date/Time] <= @MyEndDate AND
CAT2.Activities. DEPARTMENT = 381)
,cte2 as (
select [Employee Name] as [Employee Name], [Date_Time] as [Date_Time],[Percent Fat] as [Percent Fat],[Percent Lean] as [Percent Lean],[Product Code] as [Product Code],[Comments] as [Comments],
case when cast([In Spec] as int) = 0 then 'Yes' else Null end as [In Spec]
from cte
pivot (sum(result)
FOR Measure IN ([Percent Fat],[Percent Lean],[Product Code],[In Spec])) AS pvt
order by Date_Time)
,cte3 as
(write the query based on cte2 to eliminate the duplicate rows)
on report, add group logic to display one of the duplicate rows.
let me know more details if you have questions about this.
thanks,
Jerry
Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2011 12:46am