-
SQL Query
We have a table which consist of following two fields
Table name: Tab1
Field1 Field3
1 OK1
1 OK2
2 OK3
2 OK4
3 OK5
For every Field1 value, We need to know the values of Field3 in a single row.
Output has to be like this
Field1 Field3(1) Field3(2)
1 OK1 OK2
2 OK3 OK4
3 OK5 NULL
Thanks In Advance
Paddy
-
Your example suggests a maximum of two possible values of field3 for each field1, in which case,
select field1,
min_field3 field3_1,
case when max_field3 !=min_field3 then max_field3
else null
end field3_2
from
(
select field1,
min(field3) min_field3,
max(field3) max_field3
from tab1
group by field1
)
The in-line view is not required, but it makes it a little clearer what is going on