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


Query Help


Query Help

Author
Message
yogi123
yogi123
SSC-Addicted
SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)

Group: General Forum Members
Points: 458 Visits: 394
Hello Friends

i have one task, in that i need to delete records from tables and keep only 5th rows data.

so please help me to build this logic.


CREATE TABLE #temp1(
[cStudentId] [char](9) NOT NULL,
[iSchoolCode] [int] NOT NULL,
[dtEnrollmentDate] [datetime] NOT NULL,
[cGradeCode] [char](2) NOT NULL,
[cAbsenceCode] [char](1) NOT NULL,
[vcAbsenceType] [varchar](20) NOT NULL,
[iSchoolDayCategoryCode] [int] NOT NULL,
[dtEntryDate] [datetime] NOT NULL
)

insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-01')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-02')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Excused',8,'2012-10-03')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-04')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-05')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-06')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-07')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Excused',8,'2012-10-08')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Excused',8,'2012-10-09')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Excused',8,'2012-10-10')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-11')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-12')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-13')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-14')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Excused',8,'2012-10-15')
insert into #temp1 values ('003491714',469,'2012-10-08','12','E','Present',8,'2012-10-16')




Desired output is


cStudentId iSchoolCode dtEnrollmentDate cGradeCode cAbsenceCode vcAbsenceType iSchoolDayCategoryCode dtEntryDate

003491714 469 2012-10-08 00:00:00.000 12 E Present 8 2012-10-05 00:00:00.000
003491714 469 2012-10-08 00:00:00.000 12 E Excused 8 2012-10-10 00:00:00.000
003491714 469 2012-10-08 00:00:00.000 12 E Excused 8 2012-10-15 00:00:00.000



CapnHector
CapnHector
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3191 Visits: 1789
why the requirement not to use a CTE? Sounds like homework or an interview question to me.


EDIT: OP removed the requirement for with out a CTE.


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
CapnHector
CapnHector
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3191 Visits: 1789
Well i have an answer but im going to ask what have you tried?


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
yogi123
yogi123
SSC-Addicted
SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)

Group: General Forum Members
Points: 458 Visits: 394
capnhector (10/8/2012)
Well i have an answer but im going to ask what have you tried?


so far, i did


SELECT
*
FROM (
SELECT
ROW_NUMBER() OVER(PARTITION BY x.cStudentId,x.dtEnrollmentDate ORDER BY dtEnrollmentDate asc ) AS DSeq,
x.*
FROM #temp1 x) temp
WHERE DSeq not in (5,10,15)



but when i try to wrote delete syntax then i got an error

so please help me to do this
yogi123
yogi123
SSC-Addicted
SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)

Group: General Forum Members
Points: 458 Visits: 394
capnhector (10/8/2012)
Well i have an answer but im going to ask what have you tried?



I got it


DELETE temp
FROM (
SELECT
ROW_NUMBER() OVER(PARTITION BY x.cStudentId,x.dtEnrollmentDate ORDER BY dtEnrollmentDate asc ) AS DSeq,
x.*
FROM #temp1 x) temp
WHERE DSeq not in (5,10,15)


Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5974 Visits: 5280
You can use a Modulo operator to remove the hard coding of values

DELETE   temp
FROM (
SELECT ROW_NUMBER() OVER(PARTITION BY x.cStudentId,x.dtEnrollmentDate ORDER BY dtEnrollmentDate asc ) AS DSeq, x.*
FROM #temp1 x
) temp
WHERE ( DSeq % 5 ) > 0




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
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