sql数据多表联合查询行转列

自己学习使用笔记
public async Task<IEnumerable<GetExamSubjInfoListTemp>> GetExamSubjectMainInfoList(string uni_id)
        {
            string sql = @" SELECT 
                                 T.ttm_id,
                                 T.ttd_id,
                                 T.class_group_uid,G.class_group_name,
                                 R.ttr_level,
                                 T.sub_id,S.sub_name,
                                 T.total_score,T.passing_score, 
                                (case R.ttr_name when '不及格'  then r.ttr_minscore else null end)   score10,
                                (case R.ttr_name when '不及格'  then r.ttr_maxscore else null end)   score11,
                                (case R.ttr_name when '及格'    then r.ttr_minscore else null end)   score20,
                                (case R.ttr_name when '及格'    then r.ttr_maxscore else null end)   score21,
                                (case R.ttr_name when '中等'    then r.ttr_minscore else null end)   score30,
                                (case R.ttr_name when '中等'    then r.ttr_maxscore else null end)   score31,
                                (case R.ttr_name when '良好'    then r.ttr_minscore else null end)   score40,
                                (case R.ttr_name when '良好'    then r.ttr_maxscore else null end)   score41,
                                (case R.ttr_name when '优秀'    then r.ttr_minscore else null end)   score50,
                                (case R.ttr_name when '优秀'    then r.ttr_maxscore else null end)   score51
                                  FROM test_detail T 
                                 LEFT JOIN test_range R ON T.ttd_id=R.ttd_id AND T.sub_id=R.sub_id
                                 LEFT JOIN subject S ON T.sub_id=S.sub_id
                                 LEFT JOIN class_group G ON T.class_group_uid=G.class_group_uid
                                  WHERE T.isdel=0 AND R.isdel=0 AND G.isdel=0 AND S.isdel=0 and R.ttd_id IS NOT null
                                 AND R.uni_id=@uni_id 
                                  GROUP BY R.sub_id 
                                order by t.udate,r.udate  desc";
            return await ZRF_DapperHelper.QueryAsync<GetExamSubjInfoListTemp>(sql, new { uni_id = uni_id });
        }
View Code
{
    "data": [
        {
            "ttm_id": "f3653488-460b-4e9e-a4ce-b5b7c3303520",
            "ttd_id": "0042a047-51ed-487b-9f4f-7ab51ff596a5",
            "class_group_uid": "222FECAB-F778-269C-D4E5-1170DDA9BEB5",
            "class_group_name": "2020",
            "level": 0,
            "level_name": "0年级",
            "total_score": 100.0,
            "passing_score": 60.0,
            "sub_id": "800688e1-17df-45c3-8b70-8e5225411bb7",
            "sub_name": "古文鉴赏",
            "score10": "0.0",
            "score11": "60.0",
            "score20": null,
            "score21": null,
            "score30": null,
            "score31": null,
            "score40": null,
            "score41": null,
            "score50": null,
            "score51": null
        }
    ],
    "statesCode": 1001,
    "message": "查询成功"
}
View Code

 表结构:

 

posted @ 2020-05-07 17:24  天天向上518  阅读(701)  评论(0编辑  收藏  举报