[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

873 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