01.
select
*
from
(
02.
SELECT
03.
st.ID,st.MAJOR,st.STATUS,sc.GRADE,sc.YRSM,
04.
count
(sc.GRADE)
as
cnt ,
sum
(
CAST
((sc.CR_HR)
AS
INT
))
as
credit
05.
FROM
[transcripts].[dbo].[student1]
as
st
06.
INNER
JOIN
[transcripts].[dbo].[scoreed]
as
sc
ON
sc.ID = st.ID
07.
where
sc.GRADE=
'A'
08.
and
sc.CR_HR!=1
09.
and
(st.MAJOR
NOT
LIKE
'%x%'
)
10.
AND
st.CAMPUS=
'S'
11.
AND
st.STATUS
in
(
'y'
,
'b'
,
'r'
,
'l'
)
12.
and
(sc.CS_CODE
NOT
LIKE
'%00%'
)
13.
and
sc.YRSM=601
14.
and
(sc.YRSM
NOT
LIKE
'0%'
)
15.
and
sc.GRADE!=
'F'
16.
group
by
st.ID,sc.GRADE,st.MAJOR,st.STATUS,sc.YRSM,sc.CR_HR
17.
having
cnt = 5
18.
)
as
qqqqqqqqqqqq
19.
where
NOT
EXISTS(
20.
SELECT
st1.ID, sc1.CS_CODE,
count
(sc1.CS_CODE)
as
cnt
21.
FROM
[transcripts].[dbo].[student1]
as
st1
22.
INNER
JOIN
[transcripts].[dbo].[scoreed]
as
sc1
ON
sc1.ID = st1.ID
23.
where
24.
(st1.MAJOR
NOT
LIKE
'%x%'
)
25.
AND
(st1.CAMPUS=
'S'
)
26.
AND
st1.STATUS
in
(
'y'
,
'b'
,
'r'
,
'l'
)
27.
and
(sc1.CS_CODE
NOT
LIKE
'%00%'
)
28.
and
st1.ID=qqqqqqqqqqqq.ID #อ้างอิง ตารางหลักด้วย ไม่อย่างนั้นเอาใครก็ได้
29.
group
by
st1.ID,sc1.CS_CODE
30.
having
cnt>1
31.
)
32.
order
by
qqqqqqqqqqqq.ID, qqqqqqqqqqqq.YRSM