下面的代碼看起來應該可以工作,但是我得到了Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
。我認爲這是關於testrgstr_testname上avg(testResults)的關鍵。SQL查詢主鍵INSIDE case case error
我有一張桌子上充滿了一天中不同時間的測試結果。我想把日期範圍內的所有污泥,灰塵和顆粒物測試結果,並將它們平均排列在一行上。
這導致了使用數據透視。唯一的問題是測試名稱會根據從JAMES到FRANK的其他名稱的一年中的時間而變化,這導致使用case語句來允許不同的測試名稱,從而允許在關鍵點之後的不同列。
道歉的任何錯誤,因爲這是剝離我的生產代碼版本,我無法測試它。
SELECT [JDTD JAMES Cutting Sludge AT] as s_at, [JDTD JAMES Cutting Sludge AO] as s_ao,
[JDTD JAMES Dust AT] as d_at, [JDTD JAMES Dust AO)] as d_ao,
[JDTD JAMES Particle_AT] as p_at, [JDTD JAMES Particle_AO] p_ao
...
返回結果集,而不是一個單一的值:
WITH TestData as ( SELECT TestName, CONVERT(DECIMAL(10,2),result) as TestResult FROM TestReg WHERE ResultDate BETWEEN @pFromDate AND @pToDate and (testrgstr_testname like '%Sludge%' or testrgstr_testname like '%Dust%' or testrgstr_testname like '%Particle%') ) SELECT CASE WHEN @pCampType = 'JAMES' THEN ( SELECT [JDTD JAMES Cutting Sludge AT] as s_at, [JDTD JAMES Cutting Sludge AO] as s_ao, [JDTD JAMES Dust AT] as d_at, [JDTD JAMES Dust AO)] as d_ao, [JDTD JAMES Particle_AT] as p_at, [JDTD JAMES Particle_AO] p_ao FROM TestData PIVOT ( avg(TestResult) FOR testrgstr_testname IN ([JDTD JAMES Cutting Sludge AT],[JDTD JAMES Cutting Sludge AO], [JDTD JAMES Dust AT, [JDTD JAMES Dust AO], [JDTD JAMES Particle_AT],[JDTD JAMES Particle_AO]) ) as pvt ) WHEN @pCampType = 'FRANK' THEN ( SELECT [JDTD FRANK Cutting Sludge AT] as s_at, [JDTD JAMES FRANK Sludge AO] as s_ao, [JDTD FRANK Dust AT] as d_at, [JDTD FRANK Dust AO)] as d_ao, [JDTD FRANK Particle_AT] as p_at, [JDTD FRANK Particle_AO] as p_ao FROM TestData PIVOT ( avg(TestResult) FOR testrgstr_testname IN ([JDTD JAMES Cutting Sludge AT],[JDTD JAMES Cutting Sludge AO], [JDTD JAMES Dust AT, [JDTD JAMES Dust AO], [JDTD JAMES Particle_AT],[JDTD JAMES Particle_AO]) ) as pvt ) ELSE ( SELECT 'ERROR' ) END
難道只是我或者是第二次使用詹姆斯/弗蘭克沒有采取PIVOT中的賬戶?我在PLSQL更好,因此很高興在這裏學習。 – goneos
這就是說。謝謝。我得到我要去的地方錯了,儘管這個錯誤似乎並沒有指向我。 – goneos