SQL Query (SQL Server)

I have the following records

CarNumber     createtime
B509          2015-02-26 11:54:41.1620000
B509          2015-02-26 11:55:41.1620000
D600          2015-02-25 11:55:41.1620000
D600          2015-02-24 11:53:41.1620000
D708          2015-02-28 11:53:41.1620000

I want the following records displayed as per createtime (ASC) and per carNumber (Group wise) . Seq_No should be incremental and all the records should be in Order of CarNumber



CarNumber       seq_no
D600                   1
D600                   2
B509                   3
B509                   4
D600                   5
D708                   6
learner0824Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use the following to do what you are looking for...

select carnumber,
row_number() over (partion by carnumber order by createtime) Seq_No
from table1

Open in new window


or

select carnumber,
rank() over (partion by carnumber order by createtime) Seq_No
from table1

Open in new window


Saurabh..
0
learner0824Author Commented:
Thanks. I tried what you said
select carnumber,
row_number() over (partion by carnumber order by createtime) Seq_No
from table1

But the sequence number increements by group like
D600                   1
 D600                   2
 B509                   1
 B509                   2
 D600                   1
 D708                   1

I want it incrmeental all across the rows like  1,2,3,4,5,6  so on
0
Saurabh Singh TeotiaCommented:
I thought you said incremental but basis of car number..However if you just want incremental basis of time then you can simply use...

select carnumber,
row_number() over (order by createtime) Seq_No
from table1

Open in new window


or

select carnumber,
rank() over (order by createtime) Seq_No
from table1

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

learner0824Author Commented:
Thanks. In this case all the numbers get scattered . ie CarNumbers . They are disaplyed as

  D600                   1
  B509                   2
  D600                   3
  D708                   4
  B509                   5

As you can see D600 is not displayed in Order and even D509 as it goes by createtime.  But I would like to have it sorted in order by carnumber and increment sequence number  like

CarNumber       seq_no
 D600                   1
 D600                   2
 B509                   3
 B509                   4
 D600                   5
 D708                   6
0
PortletPaulEE Topic AdvisorCommented:
perhaps what you are asking for isn't possible......

you have supplied 5 rows with 2 columns of data
CarNumber     createtime
B509          2015-02-26 11:54:41.1620000
B509          2015-02-26 11:55:41.1620000
D600          2015-02-25 11:55:41.1620000
D600          2015-02-24 11:53:41.1620000
D708          2015-02-28 11:53:41.1620000

If order is based on createtime (ASC)

CarNumber     createtime
D600          2015-02-24 11:53:41.1620000
D708          2015-02-28 11:53:41.1620000
B509          2015-02-26 11:54:41.1620000
B509          2015-02-26 11:55:41.1620000
D600          2015-02-25 11:55:41.1620000

or createtime (DESC)

CarNumber     createtime
D600          2015-02-25 11:55:41.1620000
B509          2015-02-26 11:54:41.1620000
B509          2015-02-26 11:55:41.1620000
D600          2015-02-24 11:53:41.1620000
D708          2015-02-28 11:53:41.1620000

if the order is by BOTH CarNumber then createtime (DESC), then car numbers starting with B will be before car numbers starting with D

CarNumber     createtime
B509          2015-02-26 11:54:41.1620000
B509          2015-02-26 11:55:41.1620000
D600          2015-02-25 11:55:41.1620000
D600          2015-02-24 11:53:41.1620000
D708          2015-02-28 11:53:41.1620000

Perhaps you could describe how you arrive at the desired order AND display ALL other columns that are relevant to producing that order

CarNumber       seq_no
 D600                   1            is this the latest time? within D600?
 D600                   2
 B509                   3            is this the latest time? within B509? whay is B AFTER D
 B509                   4
 D600                   5           <<<<<<<< how is this possible there are only 2 D600 data rows

 D708                   6           if B is AFTER D how do we get back to D?
0
PortletPaulEE Topic AdvisorCommented:
a suggestion try this:

select carnumber,
row_number() over (order by NULL) Seq_No
from table1

Open in new window


or

select carnumber,
row_number() over (order by (select 1)) Seq_No
from table1

Open in new window

0
Saurabh Singh TeotiaCommented:
You can use this...

select carnumber,
row_number() over (order by carnumber,createtime) Seq_No
from table1

Open in new window


or

select carnumber,
rank() over (order by carnumber,createtime) Seq_No
from table1

Open in new window


Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
learner0824Author Commented:
Thanks. Posting similar question
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.