?
Solved

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

Posted on 2015-02-15
5
Medium Priority
?
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 37

Accepted Solution

by:
bbao earned 1000 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 34

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 34

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 1000 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 34

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

762 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