1

我正在尝试创建此存储过程,问题是当我将 count() 放在 SELECT 列表中时,它返回错误:“列 'QM_Analysis.DefectLocation' 在选择列表中无效,因为它不包含在任何一个聚合函数或 GROUP BY 子句。”

我已经尝试了很多东西,但仍然返回相同的错误。

有人有小费吗?

提前致谢

USE [JEMS]
GO
/****** Object:  StoredProcedure [dbo].[up_RE_Rpt_Defect_new]    Script Date: 01/08/2013 11:35:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

/*
[up_RE_Rpt_Defect_new] 
@Factory ="",
@Customer_ID ='25',
@Manuf_Area = null,--'136',
@Route =null,--361,
@Step = null,--1848,
@DateFrom ='2012-10-25  00:00:00.000',
@DateTo ='2012-10-25  09:00:00.000',
@hrFrom = '06:00',
@hrTo = '14:00',
@language = 9,
@family = null
*/

ALTER  PROCEDURE [dbo].[up_RE_Rpt_Defect_new]

-- Plant> Customer> Manufacturing Area> Route> Step> DateFrom> DateTo> TopNvalues;

@Factory as varchar(20)= null,
@Customer_ID as varchar(50)= null,
@Manuf_Area as varchar(50)= null,
@Route as varchar(400) = null,
@Step as varchar(400)= null,
@DateFrom as datetime= null,
@DateTo datetime = null,
@hrFrom as varchar(5) = null,
@hrTo as varchar(5) = null,
@TopN as varchar(20)= null,
@language as int = null,
@family as varchar(5) = null

AS

--declare @Factory as varchar(20),
--@Customer_ID as varchar(20),
--@Manuf_Area as varchar(20),
--@Route as varchar(50) ,
--@Step as varchar(20),
--@DateFrom as smalldatetime,
--@DateFrom2 as smalldatetime,
--@hrFrom as varchar(5) ,
--@hrTo as varchar(5) ,
--@DateTo smalldatetime ,
--@DateTo2 varchar(20) ,
--@TopN as varchar(20)

--SET @Customer_ID = 25
--SET @Factory = 7
--SET @Manuf_Area = 136
--SET @DateFrom  = '2012-10-23  05:00:00.000' -- CONVERT(varchar(12),@DateFrom,103)
--SET @DateTo  = '2012-10-23  09:00:00.000'
--SET @hrFrom = '08:00'
--SET @hrTo = '09:00'
--SET @language = 9

IF @Factory = ""
    BEGIN SET @Factory = null 
END

IF @Manuf_Area= ""
    BEGIN SET @Manuf_Area = null 
END

IF @Route = ""
    BEGIN SET @Route = null 
END

IF @Step = ""
    BEGIN SET @Step = null 
END

IF @family  = ""
    BEGIN SET @family = null 
END

DECLARE @Select as varchar(7000)
DECLARE @Where as varchar(7000)

SET @Select = 
'SELECT TOP 10 
    COUNT(qman.DefectLocation) as cont,
    qman.DefectLocation
FROM QM_Analysis qman WITH(NOLOCK)
    INNER JOIN QM_TestData td           WITH(NOLOCK) on qman.Wip_ID = td.Wip_ID AND qman.Process_ID = td.Process_ID
    INNER JOIN QM_Fixture qf            WITH(NOLOCK) on td.Fixture_ID = qf.Fixture_ID
    INNER JOIN QM_Defects def           WITH(NOLOCK) on qman.Defect_ID = def.Defect_ID
    INNER JOIN CR_Text defText          WITH(NOLOCK) on def.Defect = defText.Text_ID
    INNER JOIN CT_FeederTrayTrack ftt   WITH(NOLOCK) on qman.FeederTrayTrack_ID = ftt.FeederTrayTrack_ID
    INNER JOIN QM_DataAnalysis da       WITH(NOLOCK) on qman.Analysis_ID = da.Analysis_ID 
    INNER JOIN CR_Text ansText1         WITH(NOLOCK) on ansText1.Language_ID = 0 AND da.AnalysisStatus = ansText1.Translation
    INNER JOIN QM_AnalysisStatus ans    WITH(NOLOCK) on ansText1.Text_ID = ans.AnalysisStatus
    INNER JOIN CR_Text ansText2         WITH(NOLOCK) on ans.Descr = ansText2.Text_ID AND ansText2.Language_ID = defText.Language_ID
    INNER JOIN WP_Wip w                 WITH(NOLOCK) on td.Wip_ID = w.Wip_ID
    INNER JOIN CR_Materials m           WITH(NOLOCK) on qman.Material_ID = m.Material_ID
    INNER JOIN CR_Assemblies a          WITH(NOLOCK) on td.Assembly_ID = a.Assembly_ID
    INNER JOIN CR_Families fam          WITH(NOLOCK) on a.Family_ID = fam.Family_ID
    INNER JOIN CR_Text famText          WITH(NOLOCK) on fam.Family = famText.Text_ID AND famText.Language_ID = defText.Language_ID
    INNER JOIN CR_Customers c           WITH(NOLOCK) on a.Customer_ID = c.Customer_ID
    INNER JOIN CR_Text cText            WITH(NOLOCK) on c.Customer = cText.Text_ID AND cText.Language_ID = defText.Language_ID
    INNER JOIN CR_Text dText            WITH(NOLOCK) on c.Division = dText.Text_ID AND dText.Language_ID = defText.Language_ID
    INNER JOIN CR_Text eText            WITH(NOLOCK) on qf.Fixture = eText.Text_ID AND eText.Language_ID = defText.Language_ID
    LEFT JOIN CT_EquipmentSETup es      WITH(NOLOCK) on qman.EquipmentOrRouteStep_ID = es.EquipmentSETup_ID AND qman.IsEquipment = 1
    LEFT JOIN CR_Equipment_V e          WITH(NOLOCK) on es.Equipment_ID = e.Equipment_ID
    LEFT JOIN CR_FMRS4_V fmrs1          WITH(NOLOCK) on es.RouteStep_ID = fmrs1.RouteStep_ID AND fmrs1.Language_ID = defText.Language_ID
    LEFT JOIN CT_RouteStepSETup rss     WITH(NOLOCK) on qman.EquipmentOrRouteStep_ID = rss.RouteStepSETup_ID AND qman.IsEquipment = 0
    LEFT JOIN CR_FMRS4_V fmrs2          WITH(NOLOCK) on rss.RouteStep_ID = fmrs2.RouteStep_ID AND fmrs2.Language_ID = defText.Language_ID
    LEFT JOIN CR_Equipment_V e3         WITH(NOLOCK) on td.Equipment_ID = e3.Equipment_ID
    INNER JOIN CR_FMRS4_V fmrs3         WITH(NOLOCK) on td.RouteStep_ID = fmrs3.RouteStep_ID AND fmrs3.Language_ID = defText.Language_ID 
    --- separeted
    JOIN CR_FMRS4_V fmrs4               WITH(NOLOCK) on qman.RouteStep_ID = fmrs4.RouteStep_ID AND fmrs4.Language_ID = defText.Language_ID
    INNER JOIN SC_Users ut              WITH(NOLOCK) on td.OperatorID_ID = ut.UserID_ID
    INNER JOIN SC_Users ua              WITH(NOLOCK) on qman.AnalysisBy_ID = ua.UserID_ID
    LEFT JOIN QM_DataRec dr             WITH(NOLOCK) on da.Data_ID = dr.Data_ID AND da.Wip_ID = dr.Wip_ID AND da.Process_ID = dr.Process_ID'

SET @Where = ' WHERE ' +        
'C.Customer_ID = ' + CONVERT(varchar(8),@Customer_ID ) +
' AND defText.Language_ID = ' + CAST(@language as varchar(3)) +
' AND da.AssociatedDefectFlag = '+'''F''' + -- AND a.Assembly_ID = '10587' --P266-8446100
' AND a.Active=1 AND def.defect_ID = 126 ' 

IF @Factory IS NOT NULL 
    BEGIN
    SET @Where = @Where + ' AND (fmrs2.Factory_ID = ' + CAST(@Factory as varchar(10)) + ' or fmrs4.Factory_ID = ' + 
    CAST(@Factory as varchar(2)) + ')'  -- AND fmrs3.Factory_ID = @Factory AND fmrs4.Factory_ID = @Factory 
END

IF @Manuf_Area IS NOT NULL
    BEGIN
    SET @Where = @Where + ' AND fmrs3.FactoryMA_ID = ' + CAST(@Manuf_Area as varchar(5)) 
END

IF @family IS NOT NULL
    BEGIN
    SET @Where = @Where + ' AND fam.Family_ID = ' + CAST(@family AS VARCHAR(5)) 
END

IF @Route IS NOT NULL
    BEGIN
    SET @Where = @Where + ' AND fmrs3.FactoryMARoute_ID in(' + CAST(@Route as varchar(10)) + ') ' +
                          ' AND fmrs4.FactoryMARoute_ID in(' + CAST(@Route as varchar(10)) + ') '
END

IF @Step IS NOT NULL
    BEGIN
    SET @Where = @Where + ' AND (fmrs3.RouteStep_ID in(' + CAST(@Step as varchar(50)) + ') or fmrs4.RouteStep_ID in (' + 
    CAST(@Step as varchar(50)) + '))'
END

SET @Where = @Where + 'AND CONVERT(varchar(5),td.LastUpdated,114) between ''' + @hrFrom + ''' AND ''' + @hrTo + '''' + 
'AND td.LastUpdated between ''' + CONVERT(varchar(12),@DateFrom,111) + ' ' + @hrFrom + ':00.000' + ''' AND ''' + 
CONVERT(varchar(12),@DateTo,111) + ' ' + @hrTo + ':00.000'''

SET @Where = @Where + 'GROUP BY qman.DefectLocation ORDER BY count(qman.DefectLocation) DESC'

PRINT (@select + @Where)
EXEC  (@select + @Where)
4

0 回答 0