-
select query
Hi All,
Can any one help me for getting a SQL for this condition.
TABLE
column A column B column C column D,columnE
VALUE1
VALUE2
VALUE3
I am selecting columnA,columnB,columnC from TABLE.
I need query to get select like
if the value is VALUE2 then this select must take columnA,column, D,columnE else columnA,columnB,columnC
Thanks
-
Please post a DESC of your table, solution might depend on datatypes.
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
-
table desc
Table is
TABLE
Name Type
---------------
colA VARCHAR2(25)
colB VARCHAR2(15)
colC DATE
colD DATE
colE DATE
colF DATEn
If colA is other than VALUE1 then
select colA,colB,colC from
if ColA is VALUE2 then
select colA,colD,colE from table
hope this is clear
-
table desc
Table is
TABLE
Name Type
---------------
colA VARCHAR2(25)
colB VARCHAR2(15)
colC DATE
colD DATE
colE DATE
colF DATE
If colA is other than VALUE2 then
select colA,colC,colD from
if ColA is VALUE2 then
select colA,colE,colF from table
hope this is clear
-
Okay... piece of cake.
I'm not doing your job but I'll point you in the right direction, here is the hint.
Use UNION statement.
First select should return colA, colC and colD when colA != VALUE2.
Second select should return colA, colE and colF when colA = VALUE2.
Now show us your best effort.
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
-
You can do it without UNION
Hi,
You may do it with a single pass throught select like this :
select colA, (case when colA = VALUE2 then colE else colC end) x_name,
(case when colA = VALUE2 then colF else colD end) y_name
from table
if your DBMS is not Oracle you can find a substitution to case when. For SQL SERVER use IIF
Best Regards
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|