Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL selection table
Hello,
 
I wanted to know if it is possible to select 1 value from 2 diferent Columns in the same table and display them in the same column.
 
Heres an Example.
Total Free Space     |     Total Space
10                            30
20                            10
40                            20
Show something like:
10 --First Column
30 --Second Column
Thank You.
Best Regards.
Posted 24-May-12 2:08am
Edited 24-May-12 11:38am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

SELECT F1 
FROM Table1
UNION ALL
SELECT F2 AS F1 
FROM Table2
 
More at MSDN: UNION[^]
 
[EDIT]
Analogically, if data comes from the same table:
SELECT F1 
FROM Table1
UNION ALL
SELECT F2 AS F1 
FROM Table1
[/EDIT]
  Permalink  
v2
Comments
Member 8956437 at 24-May-12 7:20am
   
What if they are in the same table?
losmac at 24-May-12 8:37am
   
In the first post, you have wrote, that columns comes from different tables. So... an answer is as is.
Analogically... See my answer after changes.
VJ Reddy at 24-May-12 8:49am
   
Good answer. 5!
losmac at 24-May-12 11:48am
   
Thank you, VJ ;)
Sandeep Mewara at 25-May-12 1:54am
   
Good one. 5!
losmac at 25-May-12 4:07am
   
Thank you, Sandeep ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you can use union all for this
e.g.
select column_Name1 from tbl_1 union all select column_Name2 from tbl_2
  Permalink  
v2
Comments
Member 8956437 at 24-May-12 7:20am
   
What if the Values are in the same table, but in diferent Columns
ujju.1 at 24-May-12 8:52am
   
even then, it will be similar to the first one. i.e.
select column_Name1 a from tbl_1 union all select column_Name2 a from tbl_1
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You haven't mentioned the exact logic you're trying to use since the answer may vary depending on the needs. But if you want for example pick a value from some column in your query then you can use CASE[^]
 
An example
SELECT CASE
          WHEN column1 = 10 THEN column1
          WHEN column2 = 30 THEN column2
          ELSE column3
       END AS ResultColumnName
FROM YourTable
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 204
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 183
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 24 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100