 |
ต้องการนำ SELECT 2 คิวรี่ มาเปรียบเทียบกันใน SQL เงื่อนไข 1.คิวรี่ที่ 1 หรือ คิวรี่ที่ 2 <1.2 แสดงผล |
|
 |
|
|
 |
 |
|
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
|
 |
 |
 |
 |
Date :
2017-09-11 14:53:36 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่เข้าใจช่วยอธิบายด้วยครับ
|
 |
 |
 |
 |
Date :
2017-09-11 15:11:19 |
By :
mininovaa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผิดๆ mssql
Code (SQL)
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
|
ประวัติการแก้ไข 2017-09-11 18:25:41
 |
 |
 |
 |
Date :
2017-09-11 18:20:02 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
case
when m2=2 and m2>2
then 'normal' as g
ผมรันแล้วครับ as g ที่สร้างขึ้นมีค่า NULL แทนที่จะขึ้น normal และเงื่อนไขอื่นๆ NULL เหมือนกันครับ

|
ประวัติการแก้ไข 2017-09-11 19:26:53 2017-09-11 19:28:00
 |
 |
 |
 |
Date :
2017-09-11 19:22:58 |
By :
mininovaa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาที่รันมาดูหน่อย
|
 |
 |
 |
 |
Date :
2017-09-11 19:27:03 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

SQL ที่ใช้รัน
Code (SQL)
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
|
ประวัติการแก้ไข 2017-09-11 19:42:23 2017-09-11 19:43:46
 |
 |
 |
 |
Date :
2017-09-11 19:41:53 |
By :
mininovaa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
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
|
 |
 |
 |
 |
Date :
2017-09-11 20:54:15 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
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)
)
SQL ที่ใส่เพิ่มเติมสังกตได้จาก sql ข้อมูลรหัส ID ด้านล่าง
หลัังรัน sql ผลลัพธ์แสดง 1 column และข้อมูลไม่ถูกต้อง ข้อมูลรหัสมี 300 ข้อมูล

Code (SQL)
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
|
ประวัติการแก้ไข 2017-09-11 21:04:43 2017-09-11 21:05:24 2017-09-11 21:33:22 2017-09-11 21:36:53 2017-09-11 21:40:16 2017-09-11 21:40:51 2017-09-11 22:10:06 2017-09-11 22:11:01
 |
 |
 |
 |
Date :
2017-09-11 21:03:34 |
By :
mininovaa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ลองใส่เองดูบ้างเหรอครับ
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
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 มาเกี่ยว
|
 |
 |
 |
 |
Date :
2017-09-12 01:33:13 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ไขตาม No. 9
Error
Msg 207, Level 16, State 1, Line 155
Invalid column name 'ID'.
และเปลี่ยน ID as id1 , ID as id2 , on q1.id1 =q2.id2
Error
Msg 212, Level 16, State 1, Line 155
Invalid column name 'ID'.
|
 |
 |
 |
 |
Date :
2017-09-12 12:47:38 |
By :
mininovaa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Select q1.ID, m1, m2
|
 |
 |
 |
 |
Date :
2017-09-12 15:07:37 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ   
|
 |
 |
 |
 |
Date :
2017-09-13 09:16:01 |
By :
mininovaa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|