Question

I am trying to create a graph using two different rows from the same column.

The first row (the y-axis) is fine.

SELECT box_value FROM my_table WHERE box_value=42 AND box_id=250

represents the x-axis.

The second row (the x-axis)

SELECT box_value FROM my_table WHERE box_id=250

is not fine because I can't use ‘SELECT’ in the same procedure to call this second row!

What I'm trying to achieve is -

box_id=250  | box_value=42
--------------------------
45          | 42
63          | 42
70          | 42
54          | 42
45          | 42

It does not seem to me that it can be done in SQL

Am I right?

Or are you a genius willing to help?

Était-ce utile?

La solution

You can do this by performing a join on the two values. Note that you are returning more than one row, so you can't do this as nested subqueries in the select clause:

select x.box_value as x, y.box_value as y
from (SELECT box_value
      FROM my_table
      WHERE box_id = 250
     ) x cross join
     (SELECT box_value
      FROM my_table
       WHERE box_value = 42 AND box_id = 250
     ) y;

I do want to note that the y value is constant, so you could also do:

select x.box_value as x, 42 as y
from (SELECT box_value
      FROM my_table
      WHERE box_id = 250
     ) x 

Autres conseils

SELECT * FROM (
(SELECT ... FROM my_table WHERE ...) as Column_1_name,
(SELECT ... FROM my_table WHERE ...) as Column_2_name
)

The above should help you :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top