不同的值不起作用

Distinct values not working

大家好,我绞尽脑汁想弄清楚为什么我的代码无法正常工作。我正在尝试获取不同公司名称上不同列的值。这就是我想要的样子:

Correct Outcome Picture

但我得到的是这样的观点:

Incorrect Outcome Picture

我使用的代码是这样的:

IF OBJECT_ID('tempdb..#OutstandingClean') IS NOT NULL DROP TABLE #OutstandingClean PRINT ' DROP TEMP TABLE' SELECT * INTO #OutstandingClean FROM quality$ PRINT ' INSERT INTO TEMP TABLE' GO ALTER TABLE #OutstandingClean ADD [QC Date Only] NVARCHAR(255) PRINT 'DATE COLUMN CREATED IN TEMP' ALTER TABLE #OutstandingClean ADD [QC Time Only] NVARCHAR(255) PRINT 'TIME COLUMN CREATED IN TEMP' ALTER TABLE #OutstandingClean ADD [CMA Date Only] NVARCHAR(255) PRINT 'DATE COLUMN CREATED IN TEMP' ALTER TABLE #OutstandingClean ADD [CMA Time Only] NVARCHAR(255) PRINT 'TIME COLUMN CREATED IN TEMP' GO UPDATE #OutstandingClean SET [QC Date Only] = LEFT([TSQCApproved],LEN([TSQCApproved])-7) GO UPDATE #OutstandingClean SET [QC Time Only] = right([TSQCApproved],8) PRINT ' UPDATED DATE AND TIME IN TEMP TABLE' GO UPDATE #OutstandingClean SET [CMA Date Only] = LEFT([TSCMAStarted],LEN([TSCMAStarted])-7) GO UPDATE #OutstandingClean SET [CMA Time Only] = right([TSCMAStarted],8) PRINT ' UPDATED DATE AND TIME IN TEMP TABLE' GO SELECT distinct FIID, (select CONVERT(DECIMAL,(AVG(QualityScore))) from #OutstandingClean WHERE [QC Date Only] between '4/10/2018' and '4/11/2018' ) , (select count(distinct KYCRecordName) from #OutstandingClean where (RecordType) IN ('QC') and [QC Date Only] between '4/10/2018' and '4/11/2018' ) , (select count(kycrecordname) from #OutstandingClean where [TSCMAStarted] between '4/10/2018' and '4/11/2018' ) , (select count(ErrorType) from #OutstandingClean where [ErrorType] in ('Data Input') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' ), (select count(ErrorType) from #OutstandingClean where [ErrorType] in ('Editorial') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' ), (select count(ErrorType) from #OutstandingClean where [ErrorType] in ('Polity') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' ) from #OutstandingClean GROUP BY FIID

您的 select 语句生成相同的数据,因为条件相同

(select CONVERT(DECIMAL,(AVG(QualityScore))) from #OutstandingClean WHERE [QC Date Only] between '4/10/2018' and '4/11/2018' ) ,
    (select count(distinct KYCRecordName) from #OutstandingClean where (RecordType) IN ('QC') and [QC Date Only] between '4/10/2018' and '4/11/2018' ) ,
    (select count(kycrecordname) from #OutstandingClean where [TSCMAStarted] between '4/10/2018' and '4/11/2018' ) ,
    (select count(ErrorType) from #OutstandingClean where [ErrorType] in ('Data Input') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' ),
    (select count(ErrorType) from #OutstandingClean where [ErrorType] in ('Editorial') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' ),
    (select count(ErrorType) from #OutstandingClean where [ErrorType] in ('Polity') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' )

但是我建议您在查询中使用 Case 语句,试试这个

SELECT distinct FIID,
    CONVERT(DECIMAL,(AVG(CASE WHEN [QC Date Only] between '4/10/2018' and '4/11/2018' THEN QualityScore END))) Qualtity,
    COUNT(distinct (CASE WHEN (RecordType) IN ('QC') and [QC Date Only] between '4/10/2018' and '4/11/2018' THEN kycrecordname END)) Record_count,
    COUNT(CASE WHEN (RecordType) IN ('QC') and [QC Date Only] between '4/10/2018' and '4/11/2018' THEN kycrecordname END) Worked_on,
    COUNT(CASE WHEN [ErrorType] in ('Data Input') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' THEN ErrorType END) Data_input,
    COUNT(CASE WHEN [ErrorType] in ('Editorial') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' THEN ErrorType END) Editorial,
    COUNT(CASE WHEN [ErrorType] in ('Polity') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' THEN ErrorType END) Policy
    from #OutstandingClean
    GROUP BY FIID;

试试这个。我没有测试场景

IF OBJECT_ID('tempdb..#OutstandingClean') IS NOT NULL DROP TABLE #OutstandingClean PRINT ' DROP TEMP TABLE'
SELECT * INTO #OutstandingClean FROM quality$ PRINT ' INSERT INTO TEMP TABLE'
GO
ALTER TABLE #OutstandingClean ADD [QC Date Only] NVARCHAR(255) PRINT 'DATE COLUMN CREATED IN TEMP'
ALTER TABLE #OutstandingClean ADD [QC Time Only] NVARCHAR(255) PRINT 'TIME COLUMN CREATED IN TEMP'
ALTER TABLE #OutstandingClean ADD [CMA Date Only] NVARCHAR(255) PRINT 'DATE COLUMN CREATED IN TEMP'
ALTER TABLE #OutstandingClean ADD [CMA Time Only] NVARCHAR(255) PRINT 'TIME COLUMN CREATED IN TEMP'
GO
UPDATE #OutstandingClean SET [QC Date Only] = LEFT([TSQCApproved],LEN([TSQCApproved])-7)
GO
UPDATE #OutstandingClean SET [QC Time Only] = right([TSQCApproved],8) PRINT ' UPDATED DATE AND TIME IN TEMP TABLE'
GO
UPDATE #OutstandingClean SET [CMA Date Only] = LEFT([TSCMAStarted],LEN([TSCMAStarted])-7)
GO
UPDATE #OutstandingClean SET [CMA Time Only] = right([TSCMAStarted],8) PRINT ' UPDATED DATE AND TIME IN TEMP TABLE'
GO
SELECT distinct FIID,
        (select CONVERT(DECIMAL,(AVG(QualityScore))) from #OutstandingClean o WHERE o.FIID = #OutstandingClean.FIID AND [QC Date Only] between '4/10/2018' and '4/11/2018' ) ,
        (select count(distinct KYCRecordName) from #OutstandingClean WHERE o.FIID = #OutstandingClean.FIID AND (RecordType) IN ('QC') and [QC Date Only] between '4/10/2018' and '4/11/2018' ) ,
        (select count(kycrecordname) from #OutstandingClean WHERE o.FIID = #OutstandingClean.FIID AND [TSCMAStarted] between '4/10/2018' and '4/11/2018' ) ,
        (select count(ErrorType) from #OutstandingClean WHERE o.FIID = #OutstandingClean.FIID AND [ErrorType] in ('Data Input') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' ),
        (select count(ErrorType) from #OutstandingClean WHERE o.FIID = #OutstandingClean.FIID AND [ErrorType] in ('Editorial') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' ),
        (select count(ErrorType) from #OutstandingClean WHERE o.FIID = #OutstandingClean.FIID AND [ErrorType] in ('Polity') and [ReviewErrorCreateDate] between '4/10/2018' and '4/11/2018' )
        from #OutstandingClean
        GROUP BY FIID