Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 22nd, 2006, 02:51 PM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default build and populate manualy "identity" column

Hello I am new here and I would appriciate your help
I have 2 tables for example region (id,name) and state(id,R_id,name)
And I would like in a quey to build another columns for each state so it will be identity within it region
Thanks
Sandu


 
Old October 22nd, 2006, 03:21 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

I assume that what you are asking for is something that will build the R_ID column in the state table... the bad news is, can't be done... there's nothing in either table that would allow you to do that... if there were, you wouldn't need the R_ID column in the state table to begin with. I'm afraid this is going to be a manual assignment... the good news is, there's only 50 states :)

--Jeff Moden
 
Old October 22nd, 2006, 04:23 PM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Thank for your quick reply.
Well not exactly
Lets say that Region has the following Records (I wont fill them al)
E East
S South
And States has the following Records:
NC E North Carolina
SC E South Carolina
GA E Georgia
AR S Arkansas
MS S Mississippi
AL S Alabama

I would like to produce a query that will be a join of the two tables plus a column which will be the order of the state in region
For example
E East 1 GA North Carolina
E East 2 NC South Carolina
E East 3 SC Georgia
S South 1 AL Alabama
S South 2 AR Arkansas
S South 3 MS Mississippi
The bold is what I am looking for
And if it was for the sates I would build it my self manually but this is only for an example of master detail where i need to use such a query
sandu


 
Old October 22nd, 2006, 09:39 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Why didn't you say so in the first place? :D

Haven't tested it but this should be pretty close...

 SELECT r.ID,r.Name,
        RegionCount = (SELECT COUNT(*)
                         FROM State s1
                        WHERE s1.R_ID = s.R_ID
                          AND s1.ID <= s.ID)
        s.ID,s.Name
   FROM Region r,
        State s
  WHERE r.ID = s.R_ID
  ORDER BY s.R_ID, s.ID

--Jeff Moden
 
Old October 23rd, 2006, 02:06 PM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

10x







Similar Threads
Thread Thread Starter Forum Replies Last Post
Identity Column mike_remember SQL Server 2000 3 July 9th, 2007 07:12 PM
getting identity column from the table g_vamsi_krish SQL Server 2000 1 March 15th, 2006 05:05 PM
identity column rohit_sant SQL Language 4 June 6th, 2005 08:27 AM
Identity column jbenson001 SQL Server 2000 5 April 12th, 2005 02:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.