Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,028

HOME > PHP > PHP Forum > ผมติดตรงหาผลรวมกิตเกรด A ต้องได้ 3 หน่วยเท่านั้นรวม 15 หน่วยกิต และเกรดในปีนั้นห้ามมี F และ W ของวิชาทั้งหมดในเทอมนั้น



 

ผมติดตรงหาผลรวมกิตเกรด A ต้องได้ 3 หน่วยเท่านั้นรวม 15 หน่วยกิต และเกรดในปีนั้นห้ามมี F และ W ของวิชาทั้งหมดในเทอมนั้น

 



Topic : 129901



โพสกระทู้ ( 80 )
บทความ ( 0 )



สถานะออฟไลน์




ผมติดตรงหาผลรวมกิตเกรด A จำนวน 3 หน่วยเ่านั้นรวม15 หน่วยกิต และเกรดในปีนั้นห้ามมี F และ W ของวิชาทั้งหมดในเทอมนั้น
Code ที่ผมใช้
Code (SQL)
SELECT  student1.ID,student1.MAJOR,student1.STATUS,scoreed.GRADE,scoreed.YRSM,count(scoreed.GRADE) as cnt  FROM [transcripts].[dbo].[student1]
INNER JOIN [transcripts].[dbo].[scoreed] ON scoreed.ID = student1.ID 
where ((((student1.MAJOR) NOT LIKE '%x%') AND ((student1.CAMPUS)='S') AND (((student1.STATUS)='y')
or ((student1.STATUS)='b')or  ((student1.STATUS)='r') or  ((student1.STATUS)='l') )
and ((scoreed.CS_CODE) NOT LIKE '%00%') and (scoreed.YRSM<=592) and ((student1.ID) NOT LIKE '%60%') 
and ((scoreed.YRSM) NOT LIKE '0%')) and scoreed.GRADE='A' )
group by student1.ID,scoreed.GRADE,student1.MAJOR,student1.STATUS,scoreed.YRSM
having  count(scoreed.GRADE)=5
order by student1.ID,scoreed.YRSM ASC


ปี 592
รหัส ชื่อวิชา หน่วยกิต เกรด
xxx Badminton 1 A
xxx Foundation English I 3 A
xxx The Use of Library Resources 1 A
xxx Calculus I 3 W
xxx Introduction to Computer Science 4 C
xxx Digital Computer Logic 3 D+
xxx Laboratory in Abridged Physics 1 B
xxx Abridged Physics 3 A
xxx Personality Development 3 A

x Bowling 1 A
x Foundation English II 3 A
x Philosophy of Sufficiency Economics and Buddhism 3 A
x Calculus I 3 A
x Computer Programming 3 A
x Health for Life 3 A
x Thai Language for Communication 3 C
x Concept of Science and Philosophy 3 C



Tag : PHP, Ms SQL Server 2016









ประวัติการแก้ไข
2018-01-10 16:07:22
2018-01-10 16:08:01
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2018-01-10 15:48:57 By : mininovaa View : 549 Reply : 4
 

 

No. 1



โพสกระทู้ ( 9,559 )
บทความ ( 2 )



สถานะออฟไลน์


Code (SQL)
select * from (
  select  id,  count(*) as c from table
  where grade=A and หน่วยกิต=3 
  group by id
  having c>4
) as t
where not exist ( select * from table where id=t.id and (grade=F or grade=W) limit 1 )


ก็ประมาณนี้ ใส่ condition ชื่อตาราง ชื่อฟีลด์ อื่นๆ เอาหน่อย








ประวัติการแก้ไข
2018-01-10 18:02:42
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-01-10 17:59:47 By : Chaidhanan
 


 

No. 2



โพสกระทู้ ( 80 )
บทความ ( 0 )



สถานะออฟไลน์


error
Msg 1033, Level 15, State 1, Line 13
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
Msg 156, Level 15, State 1, Line 23
Incorrect syntax near the keyword 'order'.


แก้ขอย่างไรครับ
Code (SQL)
select * from (
SELECT  student1.ID,student1.MAJOR,student1.STATUS,scoreed.GRADE,scoreed.YRSM
,count(scoreed.GRADE) as cnt , sum(CAST((scoreed.CR_HR) AS INT)) as credit FROM [transcripts].[dbo].[student1]
INNER JOIN [transcripts].[dbo].[scoreed] ON scoreed.ID = student1.ID 
where ((((student1.MAJOR) NOT LIKE '%x%') AND ((student1.CAMPUS)='S') AND (((student1.STATUS)='y')
or ((student1.STATUS)='b')or  ((student1.STATUS)='r') or  ((student1.STATUS)='l') )
and ((scoreed.CS_CODE) NOT LIKE '%00%') and (scoreed.YRSM=601)
and ((scoreed.YRSM) NOT LIKE '0%') and scoreed.GRADE!='F') and (scoreed.GRADE='A' and scoreed.CR_HR!=1) 
) 
group by student1.ID,scoreed.GRADE,student1.MAJOR,student1.STATUS,scoreed.YRSM,scoreed.CR_HR
having  count(scoreed.GRADE)=5 
order by student1.ID,scoreed.YRSM
)as qqqqqqqqqqqq
where NOT EXISTS(

SELECT  student1.ID,count(scoreed.CS_CODE) as cnt,scoreed.CS_CODE FROM [transcripts].[dbo].[student1]
INNER JOIN [transcripts].[dbo].[scoreed] ON scoreed.ID = student1.ID 
where ((((student1.MAJOR) NOT LIKE '%x%') AND ((student1.CAMPUS)='S') AND (((student1.STATUS)='y')
or ((student1.STATUS)='b')or  ((student1.STATUS)='r') or  ((student1.STATUS)='l') )
and ((scoreed.CS_CODE) NOT LIKE '%00%'))) 
group by student1.ID,scoreed.CS_CODE
having  count(scoreed.CS_CODE)>1 
order by student1.ID ASC
)



ประวัติการแก้ไข
2018-01-11 14:53:51
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-01-11 14:53:18 By : mininovaa
 

 

No. 3



โพสกระทู้ ( 9,559 )
บทความ ( 2 )



สถานะออฟไลน์


เอา บันทั 23 ออก ในส่วนนี้ไม่ต้อง จัดเรียง
และ ควรใส่ top 1 หลัง select บันทัด 26

เป็น sub query หลัง where
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-01-11 15:58:53 By : Chaidhanan
 


 

No. 4



โพสกระทู้ ( 9,559 )
บทความ ( 2 )



สถานะออฟไลน์


ในส่วนของ sub query ไม่มีการอ้างอิง student1.ID กับตารางหลัก ก็จะเอาใครมาก็ได้

และควรหัดใช้ alias name อ้างอิง ชื่อตารางแทน จะได้สั้นๆ หน่อย
ในส่วนของ or ถ้าเป็น field เดียวกัน ลอง ใช้ in () ดู จะสั้นกว่า

Code (SQL)
select * from (
SELECT
	st.ID,st.MAJOR,st.STATUS,sc.GRADE,sc.YRSM, 
	count(sc.GRADE) as cnt , sum(CAST((sc.CR_HR) AS INT)) as credit 
FROM [transcripts].[dbo].[student1] as st
INNER JOIN [transcripts].[dbo].[scoreed] as sc ON sc.ID = st.ID 
where sc.GRADE='A' 
and sc.CR_HR!=1 
and (st.MAJOR NOT LIKE '%x%') 
AND st.CAMPUS='S' 
AND st.STATUS in('y','b','r','l' )
and (sc.CS_CODE NOT LIKE '%00%') 
and sc.YRSM=601
and (sc.YRSM NOT LIKE '0%') 
and sc.GRADE!='F'
group by st.ID,sc.GRADE,st.MAJOR,st.STATUS,sc.YRSM,sc.CR_HR
having  cnt = 5 
)as qqqqqqqqqqqq
where NOT EXISTS(
	SELECT st1.ID, sc1.CS_CODE, count(sc1.CS_CODE) as cnt
	FROM [transcripts].[dbo].[student1] as st1
	INNER JOIN [transcripts].[dbo].[scoreed] as sc1 ON sc1.ID = st1.ID 
	where 
		(st1.MAJOR NOT LIKE '%x%')
		AND (st1.CAMPUS='S')
		AND st1.STATUS in ('y','b','r','l')
		and (sc1.CS_CODE NOT LIKE '%00%')
		and st1.ID=qqqqqqqqqqqq.ID   #อ้างอิง ตารางหลักด้วย ไม่อย่างนั้นเอาใครก็ได้
	group by st1.ID,sc1.CS_CODE
	having cnt>1 
)
order by qqqqqqqqqqqq.ID, qqqqqqqqqqqq.YRSM


ปล. order by จะไม่ใส่ใน sub query
ปล. อีกครั้ง ในส่วนของ having อ้างอิงชื่อ alias field ได้เลย


ประวัติการแก้ไข
2018-01-11 16:29:24
2018-01-11 16:32:30
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-01-11 16:26:53 By : Chaidhanan
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ผมติดตรงหาผลรวมกิตเกรด A ต้องได้ 3 หน่วยเท่านั้นรวม 15 หน่วยกิต และเกรดในปีนั้นห้ามมี F และ W ของวิชาทั้งหมดในเทอมนั้น
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่