嗨,伙计们,我已经把头发拉出来试图找出为什么我的代码无效。我试图获取不同公司名称上不同列的值。这就是我想要的样子:
但我得到了这个观点:
我使用的代码是:
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
答案 0 :(得分:0)
您的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 Statement,试试这个
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;
答案 1 :(得分:0)
试试这个。我没有测试场景
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