May 13, 2011 at 9:13 am
Out Put of query result is
RnColDate
12010-02-01 00:00:00.000
22010-02-01 00:00:00.000
32010-02-01 00:00:00.000
42010-02-01 00:00:00.000
52010-02-02 00:00:00.000
12010-03-01 00:00:00.000
22010-03-01 00:00:00.000
32010-03-01 00:00:00.000
I want it to be
RnColDate
12010-02-01 00:00:00.000
12010-02-01 00:00:00.000
12010-02-01 00:00:00.000
12010-02-01 00:00:00.000
12010-02-02 00:00:00.000
22010-03-01 00:00:00.000
22010-03-01 00:00:00.000
22010-03-01 00:00:00.000
How could i do that
Thanks
May 13, 2011 at 9:16 am
With (Base query)
SELECT * FROM cte inner join
(SELECT Date ROW_NUMBER() FROM CTE GROUP BY Date) dta
...
May 13, 2011 at 9:21 am
thanks but this want solve mu issue
date cane be different for smae month something like
Rn ColDate
1 2010-02-01 00:00:00.000
2 2010-02-03 00:00:00.000
3 2010-02-05 00:00:00.000
4 2010-02-10 00:00:00.000
5 2010-02-12 00:00:00.000
1 2010-03-01 00:00:00.000
2 2010-03-11 00:00:00.000
3 2010-03-21 00:00:00.000
I want o/p AS
Rn ColDate
1 2010-02-01 00:00:00.000
1 2010-02-03 00:00:00.000
1 2010-02-05 00:00:00.000
1 2010-02-10 00:00:00.000
1 2010-02-12 00:00:00.000
2 2010-03-01 00:00:00.000
2 2010-03-11 00:00:00.000
2 2010-03-21 00:00:00.000
May 13, 2011 at 9:33 am
check out partition by in the row_number topics of books online.
May 13, 2011 at 9:35 am
Yes I did
ROW_NUMBER() OVER( PARTITION BY DATEPART(yy,date),DATEPART(mm,date) ORDER BY DATEPART(yy,date),DATEPART(mm,date))
but that's not giving me desire result
May 13, 2011 at 9:39 am
order by date
May 13, 2011 at 9:41 am
Try using DENSE_RANK instead of ROW_NUMBER
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537May 13, 2011 at 9:41 am
I tried that option too
May 13, 2011 at 9:44 am
DENSE_RANK () is not gives me desire result
May 13, 2011 at 9:52 am
Sorry I misread the question!!!!!
SET DATEFORMAT YMD
DECLARE @tbl table (dt datetime)
insert into @tbl (dt)
SELECT
'2010-02-01' UNION ALL SELECT
'2010-02-03' UNION ALL SELECT
'2010-02-05' UNION ALL SELECT
'2010-02-10' UNION ALL SELECT
'2010-02-12' UNION ALL SELECT
'2010-03-01' UNION ALL SELECT
'2010-03-11' UNION ALL SELECT
'2010-03-21'
SELECT dt, ROW_NUMBER() OVER(PARTITION BY YEAR(dt), MONTH(dt) ORDER BY dt) rn FROM @tbl
ORDER BY dt
dtrn
2010-02-011
2010-02-032
2010-02-053
2010-02-104
2010-02-125
2010-03-011
2010-03-112
2010-03-213
May 13, 2011 at 9:54 am
yes thats what I my O/P is
I wat o/p as
dt rn
2010-02-01 1
2010-02-03 1
2010-02-05 1
2010-02-10 1
2010-02-12 1
2010-03-01 2
2010-03-11 2
2010-03-21 2
Thanks
May 13, 2011 at 9:57 am
yes My o/p is same as this
I want o/p something like this
dt rn
2010-02-01 1
2010-02-03 1
2010-02-05 1
2010-02-10 1
2010-02-12 1
2010-03-01 2
2010-03-11 2
2010-03-21 2
so rn = 1 for forst month and 2 for 2nd month
Thanks
May 13, 2011 at 9:58 am
SELECT dt, DENSE_RANK() OVER(ORDER BY YEAR(dt), MONTH(dt)) rn FROM @tbl
ORDER BY dt
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537May 13, 2011 at 9:59 am
I had actually read it correctly the first time :hehe:
SET DATEFORMAT YMD
DECLARE @tbl table (dt datetime)
insert into @tbl (dt)
SELECT
'2010-02-01' UNION ALL SELECT
'2010-02-03' UNION ALL SELECT
'2010-02-05' UNION ALL SELECT
'2010-02-10' UNION ALL SELECT
'2010-02-12' UNION ALL SELECT
'2010-03-01' UNION ALL SELECT
'2010-03-11' UNION ALL SELECT
'2010-03-21'
SELECT t.dt, dt.rn FROM @tbl t INNER JOIN (
SELECT YEAR(dt) y, MONTH(dt) m, ROW_NUMBER() OVER(ORDER BY YEAR(dt), MONTH(dt)) rn FROM @tbl GROUP BY YEAR(dt), MONTH(dt)
) dt ON YEAR(t.dt) = dt.y AND MONTH(t.dt) = dt.m
ORDER BY t.dt
--1 2010-02-01
--1 2010-02-03
--1 2010-02-05
--1 2010-02-10
--1 2010-02-12
--2 2010-03-01
--2 2010-03-11
--2 2010-03-21
--
May 13, 2011 at 10:00 am
Mark-101232 (5/13/2011)
SELECT dt, DENSE_RANK() OVER(ORDER BY YEAR(dt), MONTH(dt)) rn FROM @tblORDER BY dt
Nice, had never seen that function before!
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply