Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
Hello

I need one help to develop logic which i can use in my script,


CREATE TABLE #TEMP
(cStudentID VARCHAR(09),
School_Year INT,
CourseID VARCHAR(10),
TermCode VARCHAR(05),
Mark VARCHAR(02))

INSERT INTO #TEMP VALUES ('003804186',2013,'007501','Q1','B')
INSERT INTO #TEMP VALUES ('003804186',2013,'007503','Q1','B')
INSERT INTO #TEMP VALUES ('003804186',2013,'504712','FE1','D')
INSERT INTO #TEMP VALUES ('003804186',2013,'504712','FG1','C')
INSERT INTO #TEMP VALUES ('003804186',2013,'504712','Q1','B')



I get below output


cStudentID School_Year CourseID TermCode Mark
003804186 2013 007501 Q1 B
003804186 2013 007503 Q1 B
003804186 2013 504712 FE1 D
003804186 2013 504712 FG1 C
003804186 2013 504712 Q1 B


The requirement is, If there is FG1 TermCode for same Student and Course Then Select Mark for FG1 TermCode Otherwise select Q1 TermCode.
meaning if FG1 THEN FG1
else Q1 and Filter other termcode

see below the desired output is,


cStudentID School_Year CourseID TermCode Mark
003804186 2013 007501 Q1 B
003804186 2013 007503 Q1 B
003804186 2013 504712 FG1 C


Thanks for Your Help.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8554 Visits: 18142
Something like this?

WITH CTE AS(
SELECT *,
RANK() OVER(PARTITION BY cStudentID, CourseID ORDER BY CASE Termcode WHEN 'FG1' THEN 1 WHEN 'Q1' THEN 2 ELSE 3 END) rn
FROM #TEMP
)
SELECT *
FROM CTE
WHERE rn = 1




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14959 Visits: 38981
great job posting the DDL;
in this case, i think using ROW_NUMBER(), and a custom CASE expression to control the special order might work well:



SELECT * FROM
(
SELECT row_number() over(partition by cStudentID,School_Year,CourseID
ORDER BY CASE
WHEN TermCode='FG1'
THEN 1
WHEN TermCode='Q1'
THEN 2
ELSE 3
END) AS RW,*
FROM #TEMP
) MyAlias

WHERE
RW = 1



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
Awesome

Thanks You so much. It works Perfect for me...
Rahul Bhosale
Rahul Bhosale
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 Visits: 192
Here is i think what you are looking for..

select distinct
a.cStudentID,
a.School_Year,
a.CourseID,
case when b.termcode is not null then b.termcode else a.termcode end as 'termcode' ,
case when b.termcode is not null then b.mark else a.mark end as 'mark'
from
#temp a left outer join
(
select
b.*
from #temp a inner join #temp b
on a.cStudentID = b.cStudentID and a.School_Year = b.School_Year and a.CourseID = b.CourseID
where a.TermCode = 'Q1' and b.termcode = 'FG1'
)
b on a.cStudentID = b.cStudentID and a.CourseID = b.CourseID


For the articles and blogs about SQL please visit... SQL Concepts, scripts and much more
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8554 Visits: 18142
Are you really suggesting to read the table 3 times instead of once?


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Rahul Bhosale
Rahul Bhosale
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 Visits: 192
just a first thought...the solution you given looks perfect...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search