Solved

Is it possible to create a temporary column during a sql query?

Posted on 2015-02-15
5
69 Views
Last Modified: 2015-02-17
If I am joining two tables, is it possible t create a temporary column for a table, strictly for the sake of me doing the sql query? I wouldn't want to make the additional column permanent, as it would only be used for the sake of the query?

For example, below suppose Table_1 has two columns, and Table_2 has three columns. For my query, I would like to create temporary column for table 1 named "StudentId", populate it with an incrementing unique integer starting at 1, and going as high as needed.

I suppose I could use a temp table, however if I could create a temporary column, that would be great.



Table_1
FirstName  LastName   StudentId
Jerry            Grisselton    
Merry          Drakeson

Table_2
Jerry           Grisselton   SocialSSN
Merry         DrakeSon
0
Comment
Question by:brgdotnet
  • 3
5 Comments
 
LVL 37

Accepted Solution

by:
Bing CISM / CISSP earned 250 total points
ID: 40611468
you can. once you have named the temparry column (actually a newly defined field) in a nested query, you can use it by referring its name in the query afterward.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40611478
Requirment:
"For example, below suppose Table_1 has two columns, and Table_2 has three columns. For my query, I would like to create temporary column for table 1 named "StudentId", populate it with an incrementing unique integer starting at 1, and going as high as needed."

Select FirstName
    , LastName
    , row_Number() over (order by FirstName, LastName) as StudentId
From Table_1

Open in new window


This will give you:
FirstName  LastName   StudentId
Jerry            Grisselton         1    
Merry          Drakeson          2

Question: What do you want to do with Table_2?

After joining, as you say, what will be the sample output?

Mike
0
 
LVL 33

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 250 total points
ID: 40611487
If you want:

 StudentId  FirstName  LastName        SocialSSN
      1                Jerry            Grisselton    xxx-xxx-xxxx
      2                Merry          Drakeson    xxx-xxx-xxxx

Then try:

Select row_Number() over (order by t.FirstName, t.LastName) as StudentId
    , t.FirstName
    , t.LastName
    , tt.SocialSSN
From Table_1 t Inner Join Table_2 tt
On t.FirstName = tt.FirstName
And t.LastName= tt.LastName

Open in new window


This post has been revised...
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40611628
Tables as you know, have a primary key in them. Assuming your tables also each have like:

Table_1
ID    FirstName   LastName   StudentId
xxx   Jerry            Grisselton    
xxx  Merry          Drakeson

Table_2
ID    FirstName   LastName   SocialSSN
xxx    Jerry           Grisselton  
xxx   Merry         DrakeSon

The best is use them to link your table via:
Select row_Number() over (order by t.FirstName, t.LastName) as StudentId
    , t.FirstName
    , t.LastName
    , tt.SocialSSN
From Table_1 t Inner Join Table_2 tt
On t.ID = tt.ID

Open in new window

0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 40615622
Thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now