473,624 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Identity column in query result

I am firing a query like:

SELECT TaskName, StartDate FROMTasks
WHERE StartDate >= '01-Aug-2003'

Now the result comes as:

TaskName StartDate
--------------------------
Task1 01-Aug-2003
Task5 10-Oct-2003

etc.

Now what I want is this:
Sl. No. TaskName StartDate
----------------------------------
1 Task1 01-Aug-2003
2 Task5 10-Oct-2003
How do I get the Sl. No. field (it does not exist in the table).
Can I do it?

-surajit
Jul 20 '05 #1
11 13739
"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
I am firing a query like:

SELECT TaskName, StartDate FROMTasks
WHERE StartDate >= '01-Aug-2003'

Now the result comes as:

TaskName StartDate
--------------------------
Task1 01-Aug-2003
Task5 10-Oct-2003

etc.

Now what I want is this:
Sl. No. TaskName StartDate
----------------------------------
1 Task1 01-Aug-2003
2 Task5 10-Oct-2003
How do I get the Sl. No. field (it does not exist in the table).
Can I do it?

-surajit


SELECT T1.StartDate, T1.TaskName, COUNT(*) AS Cnt
FROM Tasks AS T1
INNER JOIN
Tasks AS T2
ON T2.StartDate <= T1.StartDate AND
T1.StartDate >= '20030801' AND
T2.StartDate >= '20030801'
GROUP BY T1.StartDate, T1.TaskName

Regards,
jag
Jul 20 '05 #2
Hi

If TaskName is unique and gives you the correct order then try:

select ( SELECT COUNT(*) FROM tmp c WHERE c.TaskName <= b.TaskName ) as id,
b.TaskName, b.Startdate
from Tasks b
order by TaskName

John

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
I am firing a query like:

SELECT TaskName, StartDate FROMTasks
WHERE StartDate >= '01-Aug-2003'

Now the result comes as:

TaskName StartDate
--------------------------
Task1 01-Aug-2003
Task5 10-Oct-2003

etc.

Now what I want is this:
Sl. No. TaskName StartDate
----------------------------------
1 Task1 01-Aug-2003
2 Task5 10-Oct-2003
How do I get the Sl. No. field (it does not exist in the table).
Can I do it?

-surajit

Jul 20 '05 #3
Surajit,

I'm not saying this is a better way, it's just another option:

SELECT identity(int) as Sl,
TaskName,
StartDate
INTO #Tmp
FROM Tasks
WHERE StartDate >= '01-Aug-2003'

SELECT * FROM #Tmp

DROP TABLE #Tmp
Shervin

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
I am firing a query like:

SELECT TaskName, StartDate FROMTasks
WHERE StartDate >= '01-Aug-2003'

Now the result comes as:

TaskName StartDate
--------------------------
Task1 01-Aug-2003
Task5 10-Oct-2003

etc.

Now what I want is this:
Sl. No. TaskName StartDate
----------------------------------
1 Task1 01-Aug-2003
2 Task5 10-Oct-2003
How do I get the Sl. No. field (it does not exist in the table).
Can I do it?

-surajit


Jul 20 '05 #4
Hi all,

Unfortunately none of them worked!
The serial no. column returned some big values and do not appear
serially.

The last one (using a temp table) is ok, but I do not want to use temp
tables.

Task name may not be unique, there are Task_ID and Task_UID which
forms a composite key for this table.

But how can that help?

Please help/comment.

-surajit

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
I am firing a query like:

SELECT TaskName, StartDate FROMTasks
WHERE StartDate >= '01-Aug-2003'

Now the result comes as:

TaskName StartDate
--------------------------
Task1 01-Aug-2003
Task5 10-Oct-2003

etc.

Now what I want is this:
Sl. No. TaskName StartDate
----------------------------------
1 Task1 01-Aug-2003
2 Task5 10-Oct-2003
How do I get the Sl. No. field (it does not exist in the table).
Can I do it?

-surajit

Jul 20 '05 #5
Surajit,

Can you tell us more about the nature of your problem? How big is your
table? Isn't it possible to generate sequence numbers in your front-end
application? Why don't you want to use temporary tables? Is it because of
some technical or performance problem or you just prefer not to use temp
tables?

Shervin

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
Hi all,

Unfortunately none of them worked!
The serial no. column returned some big values and do not appear
serially.

The last one (using a temp table) is ok, but I do not want to use temp
tables.

Task name may not be unique, there are Task_ID and Task_UID which
forms a composite key for this table.

But how can that help?

Please help/comment.

-surajit

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
I am firing a query like:

SELECT TaskName, StartDate FROMTasks
WHERE StartDate >= '01-Aug-2003'

Now the result comes as:

TaskName StartDate
--------------------------
Task1 01-Aug-2003
Task5 10-Oct-2003

etc.

Now what I want is this:
Sl. No. TaskName StartDate
----------------------------------
1 Task1 01-Aug-2003
2 Task5 10-Oct-2003
How do I get the Sl. No. field (it does not exist in the table).
Can I do it?

-surajit

Jul 20 '05 #6
Hi

It would help if you posted DDL (Create table statements), example data
(Insert Statements) and your own attempts to solve the problem, then
everyone would have a clear understanding of your problem actually is.

John

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
Hi all,

Unfortunately none of them worked!
The serial no. column returned some big values and do not appear
serially.

The last one (using a temp table) is ok, but I do not want to use temp
tables.

Task name may not be unique, there are Task_ID and Task_UID which
forms a composite key for this table.

But how can that help?

Please help/comment.

-surajit

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
I am firing a query like:

SELECT TaskName, StartDate FROMTasks
WHERE StartDate >= '01-Aug-2003'

Now the result comes as:

TaskName StartDate
--------------------------
Task1 01-Aug-2003
Task5 10-Oct-2003

etc.

Now what I want is this:
Sl. No. TaskName StartDate
----------------------------------
1 Task1 01-Aug-2003
2 Task5 10-Oct-2003
How do I get the Sl. No. field (it does not exist in the table).
Can I do it?

-surajit

Jul 20 '05 #7
Hi,

The table has 25000 rows at this moment and increasing.

I can obviously generate a sequence in the front end, but:
1) I want to do it in the back end, to check if it is possible
2) I dont want to use Temp tables

If I accept any of these, I can solve this problem right away.

But I want to find an option of doing it in the backend, following
these constraints, and want to know if it is possible.

If it is not possible, then it is important for me to know that it is
NOT POSSIBLE.

The description of the table does not help much, as I would like to
use this idea (if I get any) in any query result from any table.

Thanks,

-surajit


"Shervin Shapourian" <Sh**********@h otmail.com> wrote in message news:<vo******* *****@corp.supe rnews.com>...
Surajit,

Can you tell us more about the nature of your problem? How big is your
table? Isn't it possible to generate sequence numbers in your front-end
application? Why don't you want to use temporary tables? Is it because of
some technical or performance problem or you just prefer not to use temp
tables?

Shervin

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
Hi all,

Unfortunately none of them worked!
The serial no. column returned some big values and do not appear
serially.

The last one (using a temp table) is ok, but I do not want to use temp
tables.

Task name may not be unique, there are Task_ID and Task_UID which
forms a composite key for this table.

But how can that help?

Please help/comment.

-surajit

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
> I am firing a query like:
>
> SELECT TaskName, StartDate FROMTasks
> WHERE StartDate >= '01-Aug-2003'
>
> Now the result comes as:
>
> TaskName StartDate
> --------------------------
> Task1 01-Aug-2003
> Task5 10-Oct-2003
>
> etc.
>
> Now what I want is this:
>
>
> Sl. No. TaskName StartDate
> ----------------------------------
> 1 Task1 01-Aug-2003
> 2 Task5 10-Oct-2003
>
>
> How do I get the Sl. No. field (it does not exist in the table).
> Can I do it?
>
> -surajit

Jul 20 '05 #8
Hi,
I don't think you can get a serial number with a simple SELECT, unless
you migrate on Oracle RDBMS which has a pseudo-column named ROWNUM
very useful for your purpose; rather, on MS-SQL 2K, you can create a
function that return a table. This example run on Northwind sample
database:

######

create function dbo.FN_ORDERS()
returns @tab table (
ROWNUM int,
OrderId int,
OrderDate datetime,
ShipName nvarchar(40)
) as
begin
declare @OrderId int
declare @OrderDate datetime
declare @ShipName nvarchar(40)
declare @i int
declare c cursor for
select OrderId, OrderDate, ShipName from Northwind.dbo.O rders
set @i = 0
open c
fetch c into @OrderId, @OrderDate, @ShipName
while @@fetch_status = 0 begin
set @i = @i + 1
insert @tab values (@i, @OrderId, @OrderDate, @ShipName)
fetch c into @OrderId, @OrderDate, @ShipName
end
close c
deallocate c
return
end
go

select * from dbo.FN_ORDERS()

######
Obviously you have to create one function per table and change your
front end code...

Bye.
s_****@rediffma il.com (Surajit Laha) wrote in message news:<ba******* *************** ****@posting.go ogle.com>...
Hi,

The table has 25000 rows at this moment and increasing.

I can obviously generate a sequence in the front end, but:
1) I want to do it in the back end, to check if it is possible
2) I dont want to use Temp tables

If I accept any of these, I can solve this problem right away.

But I want to find an option of doing it in the backend, following
these constraints, and want to know if it is possible.

If it is not possible, then it is important for me to know that it is
NOT POSSIBLE.

The description of the table does not help much, as I would like to
use this idea (if I get any) in any query result from any table.

Thanks,

-surajit


"Shervin Shapourian" <Sh**********@h otmail.com> wrote in message news:<vo******* *****@corp.supe rnews.com>...
Surajit,

Can you tell us more about the nature of your problem? How big is your
table? Isn't it possible to generate sequence numbers in your front-end
application? Why don't you want to use temporary tables? Is it because of
some technical or performance problem or you just prefer not to use temp
tables?

Shervin

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
Hi all,

Unfortunately none of them worked!
The serial no. column returned some big values and do not appear
serially.

The last one (using a temp table) is ok, but I do not want to use temp
tables.

Task name may not be unique, there are Task_ID and Task_UID which
forms a composite key for this table.

But how can that help?

Please help/comment.

-surajit
> "Surajit Laha" <s_****@rediffm ail.com> wrote in message
> news:ba******** *************** ***@posting.goo gle.com...
> > I am firing a query like:
> >
> > SELECT TaskName, StartDate FROMTasks
> > WHERE StartDate >= '01-Aug-2003'
> >
> > Now the result comes as:
> >
> > TaskName StartDate
> > --------------------------
> > Task1 01-Aug-2003
> > Task5 10-Oct-2003
> >
> > etc.
> >
> > Now what I want is this:
> >
> >
> > Sl. No. TaskName StartDate
> > ----------------------------------
> > 1 Task1 01-Aug-2003
> > 2 Task5 10-Oct-2003
> >
> >
> > How do I get the Sl. No. field (it does not exist in the table).
> > Can I do it?
> >
> > -surajit

Jul 20 '05 #9
Surajit,

I'm afraid you can't find a straight forward solution for this problem. If
you really want to do this without a temp table and on the back end, the
only other way that I can think about is a self-join and counting the
records (as both Johns posted), but this solution is not efficient on large
result sets.

I wish SQL-Server had a ROWNUM pseudo column like Oracle.

Shervin

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
Hi,

The table has 25000 rows at this moment and increasing.

I can obviously generate a sequence in the front end, but:
1) I want to do it in the back end, to check if it is possible
2) I dont want to use Temp tables

If I accept any of these, I can solve this problem right away.

But I want to find an option of doing it in the backend, following
these constraints, and want to know if it is possible.

If it is not possible, then it is important for me to know that it is
NOT POSSIBLE.

The description of the table does not help much, as I would like to
use this idea (if I get any) in any query result from any table.

Thanks,

-surajit


"Shervin Shapourian" <Sh**********@h otmail.com> wrote in message

news:<vo******* *****@corp.supe rnews.com>...
Surajit,

Can you tell us more about the nature of your problem? How big is your
table? Isn't it possible to generate sequence numbers in your front-end
application? Why don't you want to use temporary tables? Is it because of some technical or performance problem or you just prefer not to use temp
tables?

Shervin

"Surajit Laha" <s_****@rediffm ail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
Hi all,

Unfortunately none of them worked!
The serial no. column returned some big values and do not appear
serially.

The last one (using a temp table) is ok, but I do not want to use temp
tables.

Task name may not be unique, there are Task_ID and Task_UID which
forms a composite key for this table.

But how can that help?

Please help/comment.

-surajit
> "Surajit Laha" <s_****@rediffm ail.com> wrote in message
> news:ba******** *************** ***@posting.goo gle.com...
> > I am firing a query like:
> >
> > SELECT TaskName, StartDate FROMTasks
> > WHERE StartDate >= '01-Aug-2003'
> >
> > Now the result comes as:
> >
> > TaskName StartDate
> > --------------------------
> > Task1 01-Aug-2003
> > Task5 10-Oct-2003
> >
> > etc.
> >
> > Now what I want is this:
> >
> >
> > Sl. No. TaskName StartDate
> > ----------------------------------
> > 1 Task1 01-Aug-2003
> > 2 Task5 10-Oct-2003
> >
> >
> > How do I get the Sl. No. field (it does not exist in the table).
> > Can I do it?
> >
> > -surajit

Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
19062
by: Gunnar Vøyenli | last post by:
Hi (SQL Server 2000) I have an existing table (t) with a column that is NOT an identity column (t.ID), but it has manually inserted "row numbers". I want to make this column become an identity column. This column is a key field to other tables, so I want to keep the row numbers that are allready inserted. From the Query Analyzer, how do I do this?
2
3210
by: Dave | last post by:
I cannot insert into my appointments table because the primary key and identity column, appt_id, cannot be added. What do I have to change in my SQL statement to add new records into this table? I'm using SQL Server 2000 BE with Access Data Project FE. tbl_appointment ------------------- 1. appt_id (pk) --- identity column, seed 25, increment 1 2. date_id 3. time_start 4. time_end 5. appt_details 6. lkp_emp_id
2
15090
by: JJA | last post by:
I have fixed length records (167 bytes) in a .txt file and want to load this data exactly as is into a staging table where I hope to be able to later get at selected columns using the SUBSTRING function. Here is my target table: CREATE TABLE JJA_BCP_NHO_DAT ( RecID int IDENTITY(1,1) NOT NULL , Data VARCHAR(167) NOT NULL ) Here is my bcp command: bcp DevMDW.dbo.JJA_BCP_NHO_DAT in
3
8004
by: mal_k100 | last post by:
Hi All, 1. Created table in SQL Server 2K with Primary key as int Identity 2. Link to table in MS Access 2K 3. Using form in MSAccess to update the linked table I want SQL server to automatically update the int identity column as it would normally. i.e. no need to enter a value even though it is a NOT NULL field. SQL Server recognises this , allows the row details to be entered without the int identity and creates it when moving to the...
2
5453
by: WhiteEagl | last post by:
Hello, I would need some help with this identity column problem in SQLServer. I have a database with two tables. The Parent table has an Identity column. Parent (ParentID IDENTITY, Name) Child (ChildID, Name, ParentID)
3
6990
by: Jason L James | last post by:
Hi all, I recently wrote a vb.net app using oledb to an access database. When I inserted new rows in my datatable the identity column was automatically created. This app used an un-typed dataset. My current app is using sqlClient and a typed dataset that I created by exporting an xsd
1
1469
by: Hongyu Sun | last post by:
Hi, All: Please help. I use sql server as back end and Access 2003 as front end (everything is DAO). A table on SQL server has an identity column as the key. We have trouble on adding records to this table using the following SQL. strSQL = "INSERT INTO myTableOnSQLServer (A, B, C, D, E) SELECT A, B, C, D,
7
6928
by: Praveen_db2 | last post by:
Hi all Db2 8.1.3 Windows I have to load a table with an identity column with GENERATED ALWAYS option. I have to load the data in the identity column as it is. I created the table and tried this command db2 load from tab110.ixf of ixf modified by generatedoverride insert into wsaces.tabname for exception wsaces.tabname_excp
8
4318
by: shenanwei | last post by:
I have 2 same windows machine, same instance configure and Database , all run DB2 UDB V8.1.5 Test 1 : create table OUT_1 (LINE VARCHAR(350), LINENUMBER INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1, NO CACHE)); insert into out_1 (line) values ('C000000002XYTNF102020201855000000075000519600040547000003256510 0000000000000000000000000SIM CAR ADJ JOHN, SMITHJA CPRM SIM CARMBCORL XYTNF1020282726
0
8233
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8619
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.