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 (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)

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

I have one task


Create table #x1
(
iSchoolCode int,
iSchoolYearCode int,
dtCalendarDate datetime,
iCalendarDayId int
)

drop table #x1

insert into #x1 values (301,2012,'2012-08-10 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-11 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-12 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-13 00:00:00.000',4259)
insert into #x1 values (301,2012,'2012-08-14 00:00:00.000',4260)
insert into #x1 values (301,2012,'2012-08-15 00:00:00.000',4261)
insert into #x1 values (301,2012,'2012-08-16 00:00:00.000',4262)
insert into #x1 values (301,2012,'2012-08-17 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-18 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-19 00:00:00.000',4263)
insert into #x1 values (301,2012,'2012-08-20 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-21 00:00:00.000',4264)
insert into #x1 values (301,2012,'2012-08-22 00:00:00.000',4265)
insert into #x1 values (301,2012,'2012-08-23 00:00:00.000',4266)




Now I need to display row number when iCalendarDayId is not null
so desired output will be


iSchoolCode iSchoolYearCode dtCalendarDate iCalendarDayId ROW_Number
301 2012 2012-08-10 00:00:00.000 NULL NULL
301 2012 2012-08-11 00:00:00.000 NULL NULL
301 2012 2012-08-12 00:00:00.000 NULL NULL
301 2012 2012-08-13 00:00:00.000 4259 1
301 2012 2012-08-14 00:00:00.000 4260 2
301 2012 2012-08-15 00:00:00.000 4261 3
301 2012 2012-08-16 00:00:00.000 4262 4
301 2012 2012-08-17 00:00:00.000 NULL NULL
301 2012 2012-08-18 00:00:00.000 NULL NULL
301 2012 2012-08-19 00:00:00.000 4263 5
301 2012 2012-08-20 00:00:00.000 NULL NULL
301 2012 2012-08-21 00:00:00.000 4264 6
301 2012 2012-08-22 00:00:00.000 4265 7
301 2012 2012-08-23 00:00:00.000 4266 8

yogi123
yogi123
SSC-Addicted
SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)

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

so far I got this


SELECT iSchoolCode ,
iSchoolYearCode,
dtCalendarDate ,
ROW_NUMBER() OVER ( ORDER BY dtCalendarDate ) RowNum
FROM #x1
WHERE iCalendarDayId IS NOT NULL
UNION ALL
SELECT iSchoolCode ,
iSchoolYearCode,
dtCalendarDate ,
NULL RowNum
FROM #x1
WHERE iCalendarDayId IS NULL
order by dtCalendarDate



if someone has better way to do please let me know

Thanks
Andy Hyslop
Andy Hyslop
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2095 Visits: 3045
Hi

Another way without the UNION


Create table #x1
(
iSchoolCode int,
iSchoolYearCode int,
dtCalendarDate datetime,
iCalendarDayId int
)



insert into #x1 values (301,2012,'2012-08-10 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-11 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-12 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-13 00:00:00.000',4259)
insert into #x1 values (301,2012,'2012-08-14 00:00:00.000',4260)
insert into #x1 values (301,2012,'2012-08-15 00:00:00.000',4261)
insert into #x1 values (301,2012,'2012-08-16 00:00:00.000',4262)
insert into #x1 values (301,2012,'2012-08-17 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-18 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-19 00:00:00.000',4263)
insert into #x1 values (301,2012,'2012-08-20 00:00:00.000',NULL)
insert into #x1 values (301,2012,'2012-08-21 00:00:00.000',4264)
insert into #x1 values (301,2012,'2012-08-22 00:00:00.000',4265)
insert into #x1 values (301,2012,'2012-08-23 00:00:00.000',4266)


SELECT *

FROM

#x1 X1

LEFT JOIN
(
SELECT
iCalendarDayId
,ROW_NUMBER() OVER ( ORDER BY dtCalendarDate ) RowNum
FROM #x1
WHERE
iCalendarDayId
IS NOT NULL
) AS Row
ON x1.iCalendarDayId = Row.iCalendarDayId

drop table #x1




Cheers

Andy

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5202 Visits: 2767
I was also going to write the script with using UNION ALL as yogi already described...

i think you get your answer now :-P

_______________________________________________________________
To get quick answer follow this link:
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