473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Help in writing SQL query

21
Hi,
I have a table like this with two fields Field1,Field2.

Field 1 Field 2

00345 Hello world
00456 Hello world
09934 Hello world
78900 New filed
12345 New filed
24423 New filed
98778 How are you
12355 How are you
344545 How are you
22223 How are you

I have to add an extra field called ID which should generate a sequential number depending on Field2.
The out put should be like this:

ID Field 1 Field2
1 00345 Hello world
2 00456 Hello world
3 09934 Hello world

1 78900 New filed
2 12345 New filed
3 24423 New filed

1 98778 How are you
2 12355 How are you
3 344545 How are you
4 22223 How are you

I need to create a count and increase it depending on Field2. whenever field 2 changes I need to reset the count.

Could any body help me in writing a stored procedure for this.

TIA
Jun 25 '08 #1
5 1248
deepuv04
227 Expert 100+
Hi,
use the following query:

Expand|Select|Wrap|Line Numbers
  1. SELECT Dense_Rank() over (PARTITION BY Field2 ORDER BY Field1) as ID,
  2.        Field1,Field2
  3. FROM Table_Name
  4.  
  5.  
Thanks
Jun 25 '08 #2
cathy25
21
What is this Dense_Rank() function in the query?
Jun 25 '08 #3
deepuv04
227 Expert 100+
What is this Dense_Rank() function in the query?
Hi,

There are four different functions to assign a rank value for reach row.
Rank(),
Row_Number(),
Ntile(),
Dense_Rank()

for more details visit the following link

http://technet.microsoft.com/en-us/library/ms189798.aspx

Thanks
Jun 25 '08 #4
cathy25
21
OK! I think these are the new functions in SQL Server 2005.
But, I am using SQL Server 2000.
Jun 25 '08 #5
deepuv04
227 Expert 100+
OK! I think these are the new functions in SQL Server 2005.
But, I am using SQL Server 2000.
hi,

try the following query

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT 
  3.     (select count(*) 
  4.     FROM Table_Name as B
  5.     WHERE B.Field1 <= A.Field1 and B.Field2 = a.Field2) as Id,
  6.     Field1,Field2
  7. FROM Table_Name as a
  8. ORDER BY 3,1
  9.  
  10. -- here the number 3,1 specifies order by 3 column , 1st column in the resultset
  11.  
thanks
Jun 25 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike N. | last post by:
Hello to all: First let me apologize for the length of this question, I've made an attempt to include as much information as is needed to help with the question. I am having problems putting...
10
by: Peschtra | last post by:
Hello -- I am trying to write to a a MySql database using php, and I seem to be hitting a wall. I am attaching my php file in case someone can look at it. When I run it, I don't get any error...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
2
by: mmitchell_houston | last post by:
I'm working on a .NET project and I need a single query to return a result set from three related tables in Access 2003, and I'm having trouble getting the results I want. The details: ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
4
by: BeeMarie | last post by:
Hi, I've been trying to figure out this query for several days. I'm not that familiar with writing SQL statements, but in order to get the results I want, I think I need a subquery which requires...
6
by: Daveo | last post by:
Hi there, I have a query (say it's called "Query1") that contains data in the following structure: id reference scoretype score -- -------------- -------------- ...
2
by: DavidPr | last post by:
I have a Bible database and I need help writing the query a little bit of styling. I want to display all the books and chapters (which will be linked to another page -passage.php) that will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.