SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
yogi123
yogi123
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1258 Visits: 401
Hello Every one

I have one Task


CREATE TABLE #x1(
[cStudentId] [char](9) NOT NULL,
[iSchoolCode] [int] NOT NULL,
[dtEnrollmentDate] [datetime] NOT NULL,
[cGradeCode] [char](2) NOT NULL,
[cAbsenceCode] [char](1) NOT NULL,
[dtEntryDate] [datetime] NOT NULL
)

insert into #x1 values ('004095871',356,'2012-09-21','09','P','2012-10-24')
insert into #x1 values ('004095871',356,'2012-09-22','09','P','2012-10-24')
insert into #x1 values ('004095871',356,'2012-09-23','09','S','2012-10-24')
insert into #x1 values ('004095871',356,'2012-09-23','09','U','2012-10-24')
insert into #x1 values ('004095871',356,'2012-09-24','09','S','2012-10-24')
insert into #x1 values ('004095871',356,'2012-09-24','09','U','2012-10-24')
insert into #x1 values ('004095871',356,'2012-09-25','09','P','2012-10-24')
insert into #x1 values ('004095871',356,'2012-09-26','09','P','2012-10-24')


SELECT
ROW_NUMBER() OVER(PARTITION BY x.cStudentId ORDER BY dtEnrollmentDate desc ) AS DaySeq,
x.*
from #x1 x



I got output as below


DaySeq cStudentId iSchoolCode dtEnrollmentDate cGradeCode cAbsenceCode dtEntryDate
1 004095871 356 2012-09-26 00:00:00.000 09 P 2012-10-24 00:00:00.000
2 004095871 356 2012-09-25 00:00:00.000 09 P 2012-10-24 00:00:00.000
3 004095871 356 2012-09-24 00:00:00.000 09 S 2012-10-24 00:00:00.000
4 004095871 356 2012-09-24 00:00:00.000 09 U 2012-10-24 00:00:00.000
5 004095871 356 2012-09-23 00:00:00.000 09 S 2012-10-24 00:00:00.000
6 004095871 356 2012-09-23 00:00:00.000 09 U 2012-10-24 00:00:00.000
7 004095871 356 2012-09-22 00:00:00.000 09 P 2012-10-24 00:00:00.000
8 004095871 356 2012-09-21 00:00:00.000 09 P 2012-10-24 00:00:00.000


but desired output i need as



DaySeq cStudentId iSchoolCode dtEnrollmentDate cGradeCode cAbsenceCode dtEntryDate
1 004095871 356 2012-09-21 00:00:00.000 09 P 2012-10-24 00:00:00.000
2 004095871 356 2012-09-22 00:00:00.000 09 P 2012-10-24 00:00:00.000
3 004095871 356 2012-09-23 00:00:00.000 09 S 2012-10-24 00:00:00.000
4 004095871 356 2012-09-24 00:00:00.000 09 S 2012-10-24 00:00:00.000
5 004095871 356 2012-09-25 00:00:00.000 09 P 2012-10-24 00:00:00.000
6 004095871 356 2012-09-26 00:00:00.000 09 P 2012-10-24 00:00:00.000


so please help me to develop logic.

Thanks
Thomas Rushton
Thomas Rushton
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: Moderators
Points: 11774 Visits: 6171
Are you saying that you only need to SELECT the data WHERE cAbsenceCode = 'P' OR cAbsenceCode = 'S'? and ORDER the results BY dtEnrollmentDate?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)

Group: General Forum Members
Points: 147595 Visits: 18565
????


SELECT
ROW_NUMBER() OVER(PARTITION BY x.cStudentId ORDER BY dtEnrollmentDate desc ) AS DaySeq,
x.*
from #x1 x
where cAbsenceCode <> 'u'
order by dtEnrollmentDate



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Cadavre
Cadavre
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21822 Visits: 8519
Change your DESC to ASC for your ROW_NUMBER to get them in the correct order.

Why do you want to get rid of the records that are gone? Is it because of the cAbsenceCode? Or some other reason?

If it is because the cAbsenceCode is U, try this: -
SELECT ROW_NUMBER() OVER (PARTITION BY x.cStudentId ORDER BY dtEnrollmentDate ASC) AS DaySeq, 
[cStudentId], [iSchoolCode], [dtEnrollmentDate], [cGradeCode], [cAbsenceCode], [dtEntryDate]
FROM (SELECT [cStudentId], [iSchoolCode], [dtEnrollmentDate],
[cGradeCode], [cAbsenceCode], [dtEntryDate]
FROM #x1
WHERE [cAbsenceCode] <> 'U') x;



Which produces: -
DaySeq               cStudentId iSchoolCode dtEnrollmentDate        cGradeCode cAbsenceCode dtEntryDate
-------------------- ---------- ----------- ----------------------- ---------- ------------ -----------------------
1 004095871 356 2012-09-21 00:00:00.000 09 P 2012-10-24 00:00:00.000
2 004095871 356 2012-09-22 00:00:00.000 09 P 2012-10-24 00:00:00.000
3 004095871 356 2012-09-23 00:00:00.000 09 S 2012-10-24 00:00:00.000
4 004095871 356 2012-09-24 00:00:00.000 09 S 2012-10-24 00:00:00.000
5 004095871 356 2012-09-25 00:00:00.000 09 P 2012-10-24 00:00:00.000
6 004095871 356 2012-09-26 00:00:00.000 09 P 2012-10-24 00:00:00.000


If it is for some other reason, you'll need to explain


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
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