Need a help with the query
Could someone help me to write the query to get the below results for every row in the table
Table format:
Column1 Column2 Column3 Column4 Column5 Column6
a b server1 1 name1 value1
a b server2 2 name2 value2
a b server3 2 name3 value2
a b server4 3 name4 value3
a b server5 3 name5 value4
a b server6 3 name6 value5
Result for 1st row
Column1 Column2 Column3 Column4 Column5 Column6
a b server1 1 name1 value1
a b server2 1 name1 Not defined
a b server3 1 name1 Not defined
a b server4 1 name1 Not defined
a b server5 1 name1 Not defined
a b server6 1 name1 Not defined
**Result for 2nd row **
a b server1 2 name2 Not defined
a b server2 2 name2 value2
a b server3 2 name2 Not defined
a b server4 2 name2 Not defined
a b server5 2 name2 Not defined
a b server6 2 name2 Not defined
Answers
Hi vanutauro,
What is the logic behind?
Is 1st row, 2nd row a parameter or do you want all the results in one go?
I've supposed it's a parameter filled at the execution time:
Modified the requirement as server names(column 3) can be repeated. In the result set same column4 and column5 should be repeated for each c1,c2, and c3, and c5 should come as "not defined" for the server other than for which the data exists. Also, I need the results in one go.
Column1 Column2 Column3 Column4 Column5 Column6
a b server1 1 name1 value1
a b server2 2 name2 value2
a b server3 2 name3 value2
a b server4 3 name4 value3
a b server5 3 name5 value4
a b server6 3 name6 value5
a b server1 4 name7 value6
a b server2 4 name8 value7
Result of 1st row
Column1 Column2 Column3 Column4 Column5 Column6
a b server1 1 name1 value1
a b server2 1 name1 Not defined
a b server3 1 name1 Not defined
a b server4 1 name1 Not defined
a b server5 1 name1 Not defined
a b server6 1 name1 Not defined
Result of 2nd row
Column1 Column2 Column3 Column4 Column5 Column6
a b server1 2 name2 Not defined
a b server2 2 name2 value2
a b server3 2 name2 Not defined
a b server4 2 name2 Not defined
a b server5 2 name2 Not defined
a b server6 2 name2 Not defined
This appears to produce the correct format as a single query using the CREATE TABLE and INSERT written by VValdar:
with l2 as (select distinct c5 from vinutauro) select c1,c2,c3,c4,v.c5,CASE WHEN v.c5 = l.c5 then c6 else 'not defined' end from vinutauro v, l2 l;