Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query Help Expand / Collapse
Author
Message
Posted Wednesday, October 24, 2012 7:30 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 1, 2014 12:13 PM
Points: 115, Visits: 342
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
Post #1376465
Posted Wednesday, October 24, 2012 7:34 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, April 4, 2014 5:29 AM
Points: 965, Visits: 396
Are you saying that you only need to SELECT the data WHERE cAbsenceCode = 'P' OR cAbsenceCode = 'S'? and ORDER the results BY dtEnrollmentDate?


Post #1376471
Posted Wednesday, October 24, 2012 7:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:34 PM
Points: 12,923, Visits: 12,342
????

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 Moden's 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)
Post #1376473
Posted Wednesday, October 24, 2012 7:39 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 7:56 AM
Points: 2,372, Visits: 7,560
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



Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1376475
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse