คิวรี่ที่ 1
SELECT (sum((CAST(G AS INT))*(case GRADE
when 'A' then 4
when 'B+' then 3.5
when 'B' then 3
when 'C+' then 2.5
when 'C' then 2
when 'D+' then 1.5
when 'D' then 1
else 0
end
))/sum(CAST(G AS INT)))
FROM S
WHERE ID IN('533333333')and ((Y)<= '591')
and GRADE NOT LIKE '%P%'
and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%'
and GRADE NOT LIKE '%W%'
คิวรี่ที่ 2
SELECT (sum((CAST(G AS INT))*(case GRADE
when 'A' then 4
when 'B+' then 3.5
when 'B' then 3
when 'C+' then 2.5
when 'C' then 2
when 'D+' then 1.5
when 'D' then 1
else 0
end
))/sum(CAST(G AS INT)))
FROM S
WHERE ID IN('533333333')and ((Y)<= '592')
and GRADE NOT LIKE '%P%'
and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%'
and GRADE NOT LIKE '%W%'
Select q, m, if(q=2 and m>2,'normal',if(.....)) as g
From(
Select 1 as q, .............. As m from q1 where ......
Union all
Select 2,....... From q2 where .....
) as tmp
Select
m1, m2,
case
when m2=2 and m2>2
then 'normal'
else
case ตรงนี้ใส่ condition เพิ่มเอาเองทำตามตัวอย่างให้ครบ) end
end as g
From (
#คิวรี่ที่ 1
SELECT (sum((CAST(G AS INT))*(case GRADE
when 'A' then 4
when 'B+' then 3.5
when 'B' then 3
when 'C+' then 2.5
when 'C' then 2
when 'D+' then 1.5
when 'D' then 1
else 0
end
))/sum(CAST(G AS INT))) as m1
FROM S
WHERE ID IN('533333333')and ((Y)<= '591')
and GRADE NOT LIKE '%P%'
and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%'
and GRADE NOT LIKE '%W%') as q1
,
#คิวรี่ที่ 2
(SELECT (sum((CAST(G AS INT))*(case GRADE
when 'A' then 4
when 'B+' then 3.5
when 'B' then 3
when 'C+' then 2.5
when 'C' then 2
when 'D+' then 1.5
when 'D' then 1
else 0
end
))/sum(CAST(G AS INT))) as m2
FROM S
WHERE ID IN('533333333')and ((Y)<= '592')
and GRADE NOT LIKE '%P%'
and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%'
and GRADE NOT LIKE '%W%'
) as q2
Select
m1, m2,
case
when m2=2 and m2>2
then 'normal'
when m2>=1.75 and m2<2
then 'Hight'
when m2<1.75 and m2>1.5
then 'Low'
when m1<1.5 and m2<1.5
then 'OUT'
else
case
when m1<1.75 and m2<1.75
then 'OUT' end
end as g
From (
SELECT (sum((CAST(g AS INT))*(case GRADE
when 'A' then 4
when 'B+' then 3.5
when 'B' then 3
when 'C+' then 2.5
when 'C' then 2
when 'D+' then 1.5
when 'D' then 1
else 0
end
))/sum(CAST(g AS INT))) as m1
FROM s
WHERE ID IN('533333333')and ((Y)<= '591')
and GRADE NOT LIKE '%P%'
and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%'
and GRADE NOT LIKE '%W%') as q1
,
(SELECT (sum((CAST(g AS INT))*(case GRADE
when 'A' then 4
when 'B+' then 3.5
when 'B' then 3
when 'C+' then 2.5
when 'C' then 2
when 'D+' then 1.5
when 'D' then 1
else 0
end
))/sum(CAST(g AS INT))) as m2
FROM s
WHERE ID IN('533333333')and ((Y)<= '592')
and GRADE NOT LIKE '%P%'
and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%'
and GRADE NOT LIKE '%W%'
) as q2
Select
m1, m2,
case
when not(m2<2) then 'normal'
when not(m2<1.75) then 'Hight'
when m1<1.75 then 'OUT' # น้อยกว่า 1.75 ทั้งคู่
when not(m2<1.5) then 'Low' # m1 ไม่น้อยกว่า 1.75
else 'OUT'
end as g
Select id แบบไหนถึงไม่ error ครับ ผลรันต้องการ id m1 m2 g เหลือแต่ id
ผลลัพธ์แสดง 1 column และข้อมูลไม่ถูกต้อง ข้อมูลรหัสมี 300 ข้อมูล ซึ่งดูได้ด้านล่าง
Code (SQL)
Select
id,m1, m2,
case
when not(m2<2) then 'normal'
when not(m2<1.75) then 'Hight'
when m1<1.75 then 'OUT' # น้อยกว่า 1.75 ทั้งคู่
when not(m2<1.5) then 'Low' # m1 ไม่น้อยกว่า 1.75
else 'OUT'
end as g
sql ข้อมูลรหัส ID Code (SQL)
SELECT students.ID FROM students
WHERE (
((M) NOT LIKE '%x%') AND
((C)='SS') AND
(((S)='y') or
((S)='b') or
((S)='r') or
((S)='l')) and
((GPA)< 1.75)
)
Select
m1, m2,
case
when not(m2<2) then 'normal'
when not(m2<1.75) then 'Hight'
when m1<1.75 then 'OUT' -- น้อยกว่า 1.75 ทั้งคู่
when not(m2<1.5) then 'Low' -- m1 ไม่น้อยกว่า 1.75
else 'OUT'
end as Answer
From
(
SELECT (sum((CAST(CR_HR AS INT))*(case GRADE
when 'A' then 4
when 'B+' then 3.5
when 'B' then 3
when 'C+' then 2.5
when 'C' then 2
when 'D+' then 1.5
when 'D' then 1
else 0
end
))/sum(CAST(CR_HR AS INT))) as m1
FROM scoreed
WHERE
ID IN (SELECT s.ID FROM s
WHERE (
((M NOT LIKE '%x%') AND
((C)='S') AND
(((S)='y') or
((S)='b') or
((S)='r') or
((S)='l')) and
((GPA)< 1.75)
))
and ((scoreed.YRSM)<= '591')
and GRADE NOT LIKE '%P%'
and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%'
and GRADE NOT LIKE '%W%') as q1
,
(SELECT (sum((CAST(CR_HR AS INT))*(case GRADE
when 'A' then 4
when 'B+' then 3.5
when 'B' then 3
when 'C+' then 2.5
when 'C' then 2
when 'D+' then 1.5
when 'D' then 1
else 0
end
))/sum(CAST(CR_HR AS INT))) as m2
FROM scoreed
WHERE
ID IN (SELECT s.ID FROM s
WHERE (
((M NOT LIKE '%x%') AND
((C)='S') AND
(((S)='y') or
((S)='b') or
((S)='r') or
((S)='l')) and
((GPA)< 1.75)
))
and ((scoreed.YRSM)<= '592')
and GRADE NOT LIKE '%P%'
and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%'
and GRADE NOT LIKE '%W%'
) as q2
Select
ID, m1, m2,
case
when not(m2<2) then 'normal'
when not(m2<1.75) then 'Hight'
when m1<1.75 then 'OUT' # น้อยกว่า 1.75 ทั้งคู่
when not(m2<1.5) then 'Low' # m1 ไม่น้อยกว่า 1.75
else 'OUT'
end as g
From (
SELECT ID, (sum((CAST(g AS INT))*(
case GRADE when 'A' then 4 when 'B+' then 3.5 when 'B' then 3 when 'C+' then 2.5
when 'C' then 2 when 'D+' then 1.5 when 'D' then 1 else 0 end
))/sum(CAST(g AS INT))) as m1
FROM s WHERE ID = '533333333' and Y<592
and GRADE NOT LIKE '%P%' and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%' and GRADE NOT LIKE '%W%'
) as q1
, (
SELECT ID, (sum((CAST(g AS INT))*(
case GRADE when 'A' then 4 when 'B+' then 3.5 when 'B' then 3 when 'C+' then 2.5
when 'C' then 2 when 'D+' then 1.5 when 'D' then 1 else 0 end
))/sum(CAST(g AS INT))) as m2
FROM s WHERE ID = '533333333' and Y<593
and GRADE NOT LIKE '%P%' and GRADE NOT LIKE '%NP%'
and GRADE NOT LIKE '%L%' and GRADE NOT LIKE '%W%'
) as q2
where q1.ID=q2.ID
ส่วน การหาค่า status คุณต้องว่าง condition ให้ครบถ้วน
แต่ละ status ได้มายังไง
เช่น
normal ต้องมี m1 เท่าไหร่ และมี m2 เท่าไหร่
high ต้องมี m1 เท่าไหร่ และมี m2 เท่าไหร่
อื่นๆ ......
จากโจทย์ที่คุณทำ normal มีเฉพาะค่า m2 ไม่ได้เอา m1 มาเกี่ยว