这是一个容易理解我需要的例子 在下面的图片我已经把两个表数据,
表B中的与tableA,
链接来自tableA ID - > 1在一个主题中失败
ID - > 2是通过所有主题
ID - > 3在2个科目中失败。
所以我需要像这样的确切结果
我使用的查询是
Select A.ID,A.name,case when B.Result='fail' then 'Fail' else 'Pass' End from tableA A
inner join
TableB B ON B.id=A.Aid
使用此名称将重复。 ,但我只需要一个名字,只有一个结果
答案 0 :(得分:1)
你想要的是在每个学生的成绩中得到最差的结果(如果有一个“失败”则“失败”,否则“通过”)。您可以使用$sort = function ($a, $b){
static $czechCharsS = array('Á', 'Č', 'Ď', 'É', 'Ě' , 'Ch' , 'Í', 'Ň', 'Ó', 'Ř', 'Š', 'Ť', 'Ú', 'Ů' , 'Ý', 'Ž', 'á', 'č', 'ď', 'é', 'ě' , 'ch' , 'í', 'ň', 'ó', 'ř', 'š', 'ť', 'ú', 'ů' , 'ý', 'ž');
static $czechCharsR = array('AZ','CZ','DZ','EZ','EZZ','HZZZ','IZ','NZ','OZ','RZ','SZ','TZ','UZ','UZZ','YZ','ZZ','az','cz','dz','ez','ezz','hzzz','iz','nz','oz','rz','sz','tz','uz','uzz','yz','zz');
$A = str_replace($czechCharsS, $czechCharsR, $a);
$B = str_replace($czechCharsS, $czechCharsR, $b);
return strnatcasecmp($A, $B);
};
uasort($options, $sort);
和group by
作为分组功能
min
这是有效的,因为按字母顺序排列Select A.ID, A.Name, min(B.Result)
from TableA A
join TableB B
on A.ID = B.Aid
group by A.ID, A.Name
答案 1 :(得分:1)
;with TableA(Id,Name,Class)
As
(
SELECT 1,'Johny' ,6 Union all
SELECT 2,'George',6 Union all
SELECT 3,'Jisha',6
)
,TableB(Id,Aid,Subname,Result)
AS
(
SELECT 1,1,'SEcinece','Fail' UNION ALL
SELECT 2,1,'Social' ,'Pass' UNION ALL
SELECT 3,1,'English' ,'Pass' UNION ALL
SELECT 4,2,'SEcinece','Pass' UNION ALL
SELECT 5,2,'Social' ,'Pass' UNION ALL
SELECT 6,2,'English' ,'Pass' UNION ALL
SELECT 7,3,'SEcinece','Fail' UNION ALL
SELECT 8,3,'Social' ,'Fail' UNION ALL
SELECT 9,3,'English' ,'Pass'
)
,Final
AS
(
SELECT *,CASE WHEN Dt.CNT<3 Then 'Fail'
WHEN Dt.CNT=3 Then 'PASS' END AS FinalResult
FROM
(
SELECT A.Id,A.Name,B.RESULT,COUNT(B.Result)OVER(PARTITION BY B.AId,A.Name ORDER BY B.AId)AS CNT,
ROW_NUMBER()OVER(PARTITION BY B.AId,A.Name ORDER BY B.AId )AS Seq FROM TableA A
INNER JOIN TableB B
ON A.Id=b.Aid
WHERE B.Result='Pass'
)Dt
)
SELECT Id,Name,FinalResult AS Result FROM Final WHERE Seq=1 ORDER BY 1
输出
Id Name Result
1 Johny Fail
2 George PASS
3 Jisha Fail
答案 2 :(得分:1)
SELECT CASE WHEN MIN(BitField+0) = 1 THEN 'True' ELSE 'False' END AS MyColumn
FROM MyTable
答案 3 :(得分:0)
根据我的理解,
create table #temp(id int, name varchar(1))
insert into #temp values(1,'p')
,(1,'p')
,(1,'f')
,(2,'p')
,(2,'p')
,(2,'p')
,(3,'p')
,(3,'f')
,(3,'f')
select id, min(name) from #temp group by id, name
with cte as (
select id, name, ROW_NUMBER() over(partition by id order by name) rn from #temp group by id, name
) select id, name, rn from cte where rn = 1