數據庫整合數據報表SQL實戰

協助同事整理sql統計報表。sql


---創建由avalue的視圖,要過濾重複數據
CREATE VIEW vLectAnswerRecord
as
SELECT t2.OpenID,t2.Qguid,t1.AValue av FROM TbLectAnswer t1,TbLectAnswerRecord t2 WHERE t1.AID=t2.AIDui

---獲取用戶組合答案
SELECT Qguid,OpenID, data=STUFF((
SELECT ','+[t].[av] FROM vLectAnswerRecord t WHERE t.Qguid=t1.Qguid AND t.openid=t1.OpenID FOR XML PATH('')
), 1, 1, '')
FROM vLectAnswerRecord t1 GROUP BY qguid,OpenIDio

---將組合答案插入到臨時表gui

INSERT into tbtmp( Qguid,OpenID,avs) 統計

SELECT Qguid,OpenID, data=STUFF((
SELECT ','+[t].[av] FROM vLectAnswerRecord t WHERE t.Qguid=t1.Qguid AND t.openid=t1.OpenID FOR XML PATH('')
), 1, 1, '')
FROM vLectAnswerRecord t1 GROUP BY qguid,OpenID 數據

---將組合的逗號去掉
UPDATE tbtmp SET avs2=REPLACE(avs,',','')db

---關聯試題正確答案表,得出用戶題目是否正確
---SELECT * FROM tbtmp co

UPDATE tbtmp SET istrue=1
FROM tbtmp INNER JOIN
TBLectQuestion ON tbtmp.Qguid=TBLectQuestion.Qguid AND tbtmp.avs=TBLectQuestion.QResultopenid

--- SELECT TOP 100 Lguid,QResult,Qguid,QType FROM dbo.TBLectQuestion WHERE qguid='5ff05fb1ff62ddbc31324dc30aae5c31'data

相關文章
相關標籤/搜索