Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-02-15
5
Medium Priority
?
455 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

609 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