[英]SQL filtering, select specific values count when condition is met
也許我只是傻瓜,但是由於某種原因,這是可行的,是的,它很好用,但是我們想添加一個帶有子表但保持相同格式的條件。
開始
SELECT
v.[id]
,v.[Vacante]
,v.[deptoId]
,v.[StatusId]
,v.[scholarYearId]
,v.[tipoVacanteId]
,v.[detalle]
,v.[createdDate]
,v.[createdBy]
,d.nombre as DeptoNombre
,s.nombre as statusNombre
,y.nombre as scholarYearNombre
,t.nombre as tipoVacanteNombre
,count(uv.id) as totalCandidatos
FROM
[dbo].[tbl_vacantes] v
LEFT JOIN
tbl_usuariosPorVacante uv on v.id = uv.vacanteId
--LEFT JOIN
-- dbo.[tbl_user] u on uv.userId=u.id
INNER JOIN
dbo.[tbl_depto] d ON d.Id = v.[deptoId]
INNER JOIN
dbo.[tbl_status] s ON s.Id = v.[statusId]
LEFT JOIN
tbl_scholarYear y ON v.scholarYearId=y.Id
LEFT JOIN
tbl_tipoVacante t ON v.tipoVacanteId=t.Id
--WHERE
-- u.progressId =3 OR u.progressId is null --Solo usuarios que ya temrinaron su proceso.
GROUP BY
v.[id]
,v.[Vacante]
,v.[deptoId]
,v.[StatusId]
,v.[scholarYearId]
,v.[tipoVacanteId]
,v.[detalle]
,v.[createdDate]
,v.[createdBy]
,d.nombre
,s.nombre
,y.nombre
,t.nombre
ORDER BY
v.id DESC
結束
我們想要做的是在totalCandidatos(計數)中保留一個計數,是的,但是僅在dbo。[tbl_user] u具有progressId = 3和4時才計數。從現在開始,它正在對所有progressId進行計數。
我知道,可能是愚蠢的。 但是我陷入了這一困境。
謝謝!
您可以使用sum(case when <condition> then 1 else 0 end)
來計算返回的符合特定條件的記錄數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.