SELECT TOP 100 USER_ID ,
SUM(CASE WHEN type_code = '06' and sub_type_code = '01' THEN 1 ELSE 0 END) AS F6_1,
SUM(CASE WHEN type_code = '06' and sub_type_code = '02' THEN 2 ELSE 0 END) AS F6_2,
SUM(CASE WHEN type_code = '07' and sub_type_code = '01' THEN 1 ELSE 0 END) AS F7_1,
SUM(CASE WHEN type_code = '07' and sub_type_code = '02' THEN 2 ELSE 0 END) AS F7_2
FROM USER_SUB_DTL
WHERE USER_ID = 12
GROUP BY user_id
SELECT TOP 100 USER_ID ,
SUM(CASE WHEN type_code = '06' and sub_type_code = '01' THEN 1 ELSE 0 END) AS F6_1,
SUM(CASE WHEN type_code = '06' and sub_type_code = '02' THEN 2 ELSE 0 END) AS F6_2,
CASE WHEN type_code = '06' and sub_type_code = '02' THEN remark ELSE 0 END AS F6_2_name,
SUM(CASE WHEN type_code = '07' and sub_type_code = '01' THEN 1 ELSE 0 END) AS F7_1,
SUM(CASE WHEN type_code = '07' and sub_type_code = '02' THEN 2 ELSE 0 END) AS F7_2
FROM USER_SUB_DTL
WHERE USER_ID = 12
GROUP BY user_id , CASE WHEN type_code = '06' and sub_type_code = '02' THEN 2 ELSE 0 END AS F6_2_name
WITH USER_SUM AS
(
SELECT TOP 100 USER_ID ,
SUM(CASE WHEN [type_code] = '06' and [sub_type_code] = '01' THEN 1 ELSE 0 END) AS F6_1,
SUM(CASE WHEN [type_code] = '06' and [sub_type_code] = '02' THEN 2 ELSE 0 END) AS F6_2,
SUM(CASE WHEN [type_code] = '07' and [sub_type_code] = '01' THEN 1 ELSE 0 END) AS F7_1,
SUM(CASE WHEN [type_code] = '07' and [sub_type_code] = '02' THEN 2 ELSE 0 END) AS F7_2,
(SELECT [remark] + ',' FROM USER_SUB_DTL u2 WHERE u2.user_id = u1.user_id AND u2.remark <> '' FOR XML PATH('')) AS Remark
FROM USER_SUB_DTL u1
GROUP BY USER_ID
)
SELECT USER_ID, F6_1, F6_2, F7_1, F7_2, SUBSTRING(Remark, 0, LEN(Remark)) AS Remark FROM USER_SUM
var rhh = from rh in IResearch
join c in ICheckStatus on rh.ID_Research equals c.ID_Research into JoinedEmpDept1
from c in JoinedEmpDept1.DefaultIfEmpty()
join s in IStatusTasks on c.ID_StatusTask equals s.ID_StatusTask into JoinedEmpDept
from s in JoinedEmpDept.DefaultIfEmpty()
select new
{
ResearchNo = rh.ResearchNo, //รหัสโครงการ
ResearchN = rh.ResearchN, //ชื่องานวิจัย
Yearbudget = rh.Yearbudget, //ปี
StastusTask = s.StatusTask1, //สถานะ
};
GridView3.DataSource = rhh.ToList();
GridView3.DataBind();
โค้ดเทสใน SQL Code (SQL)
SELECT Research.*, CheckStatus.*,StatusTask.StatusTask
FROM [Irdbase].[dbo].[Research]
LEFT OUTER JOIN [Irdbase].[dbo].[CheckStatus] ON Research.ID_Research = CheckStatus.ID_Research
LEFT OUTER JOIN [Irdbase].[dbo].[StatusTask] ON CheckStatus.ID_StatusTask = StatusTask.ID_StatusTask