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


Split data


Split data

Author
Message
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3069 Visits: 2766
Hi all, I was trying to split the values using DENSE RANK but I faced some issues...
here is the problem below:
Create table tbl1
(
ID INT identity primary key,
voucherno varchar(10),
modifieddate datetime
)

insert into tbl1
values
('V00001','2013-07-07 11:32:44.810'),
('V00002','2013-07-07 11:32:44.810'),
('V00003','2013-07-08 12:32:44.810'),
('V00004','2013-07-08 12:32:44.810'),
('V00005','2013-07-07 11:32:44.810')

OUTPUT
MinVoucher MaxVoucher Modifieddate
V00001 V00002 2013-07-07 11:32:44.810
V00003 V00004 2013-07-08 12:32:44.810
V00005 V00005 2013-07-07 11:32:44.810

I tried using DENSE_RANK but i didnt get my desired output.

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
mahavidhya24
mahavidhya24
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 55
CREATE TABLE Emp(
EmpName VARCHAR(9),
Age INT,
MaritalStatus char(1))

INSERT INTO Emp VALUES ('Abhinav',40,'S')
INSERT INTO Emp VALUES ('Dhvani',20,'M')
INSERT INTO Emp VALUES ('Nehal',20,'M')
INSERT INTO Emp VALUES ('Sunil',95,'M')
INSERT INTO Emp VALUES ('Suvrendu',40,'M')
INSERT INTO Emp VALUES ('Bill',11,'S')
INSERT INTO Emp VALUES ('Ram',100,'S')
INSERT INTO Emp VALUES ('Nirmal',50,'S')
INSERT INTO Emp VALUES ('R',30,'S')

SELECT Dense_RANK() OVER (ORDER BY Age) AS [Rank by Age],
EmpName,
Age
FROM Emp



Like this model u try this example.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16164 Visits: 19543
-- Extend the sample data set:
DROP table #tbl1
Create table #tbl1
(
ID INT identity primary key,
voucherno varchar(10),
modifieddate datetime
)

insert into #tbl1
values
('V00001','2013-07-07 11:32:44.810'),
('V00002','2013-07-07 11:32:44.810'),
('V00003','2013-07-08 12:32:44.810'),
('V00004','2013-07-08 12:32:44.810'),
('V00005','2013-07-07 11:32:44.810'),
('V00006','2013-07-07 11:32:44.810'),
('V00007','2013-07-09 11:32:44.810'),
('V00008','2013-07-09 11:32:44.810'),
('V00009','2013-07-08 11:32:44.810')

-- examine the result of this
SELECT *,
rn1 = ROW_NUMBER() OVER(ORDER BY voucherno),
rn2 = ROW_NUMBER() OVER(ORDER BY modifieddate, voucherno)
FROM #tbl1


-- to see how this works
SELECT
ID,
voucherno,
modifieddate,
rn = ROW_NUMBER() OVER(PARTITION BY GroupBy ORDER BY voucherno)
FROM (
SELECT
ID,
voucherno,
modifieddate,
GroupBy = ROW_NUMBER() OVER(ORDER BY voucherno)-
ROW_NUMBER() OVER(ORDER BY modifieddate, voucherno)
FROM #tbl1
) d
ORDER BY voucherno

-- your results are rn=1




“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
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