问题描述:

I have this table and I would like to have a select statement that selects VALUE based on a given code.

CASE statement might be needed, but all examples I looked at so far had distinct values as a result of the case.

TABLE A

ID || VALUE

1 || value1

2 || value2

3 || value3

4 || value4

5 || value5

6 || value3

7 || value2

8 || value8

TABLE B

ID ||CODE

1 ||1

2 ||2

3 ||3

4 ||4

5 ||1

6 ||2

7 ||3

8 ||4

SELECT B.CODE,

CASE

WHEN B.CODE = '01' THEN A.value based on code

WHEN B.CODE = '02' THEN A.value based on code

WHEN B.CODE = '03' THEN A.value based on code

WHEN B.CODE = '04' THEN A.value based on code

END AS VALUE_OUT

FROM A JOIN B

ON A.ID = B.ID;

网友答案:

You may just need a WHERE clause

Select Value
From MyTable
Where Code = 1

EDIT

It looks like an INNER JOIN may suffice:

SELECT B.Code, A.Value as Value_Out
From A
Inner Join B
    On A.ID = B.Code
网友答案:

I think you want just

SELECT DISTINCT
code, value
FROM MyTable

or

SELECT DISTINCT
value
FROM MyTable
WHERE code = 1

If you want all values for a particular code.

相关阅读:
Top