Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting from 1 table and put the result into 2 column

I have to select from a table and put into a 2 column result.

This is the table I have,

MAILTABLE:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MAIL_KIND CODE PRICE
PRIORITY MAILCOST 20
REGULAR MAILCOST 10

So from trying out the exercise above,

I have the following SQL statement:

select 
  (select PRICE from MAILTABLE where CODE = 'MAILCOST' AND MAIL_KIND = 'PRIORITY') as PM,
  (select PRICE from MAILTABLE where CODE = 'MAILCOST' AND MAIL_KIND = 'REGULAR') as RM
from MAILTABLE

But I’m getting a bunch of result which is not what I want is to differentiate the PRIORITY mail cost and the REGULAR mail cost like below:

PM RM
20 10

How do I select from 1 table and put into a 2 column result?

>Solution :

Leave out FROM MAILTABLE at the end. That causes it to return the values of the subqueries for each row in the original table.

Use FROM DUAL as a dummy table when you don’t need a specific table in the main query.

select (select PRICE from MAILTABLE where CODE = 'MAILCOST' AND MAIL_KIND = 'PRIORITY') as PM,
       (select PRICE from MAILTABLE where CODE = 'MAILCOST' AND MAIL_KIND = 'REGULAR') as RM
FROM DUAL
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading