Jump to content

Sql Query Help


gupta24

Recommended Posts

Col1 col2
23 121
23 123
24 125
25 127
25 124

Epudu col1lo 23 ane value multiple times repeat ayindi so apudu col 2 null ipovali.

Result should be like this:

Col1 col2
23 Null
23. Null
24. 125
25. Null
25. Null

Plss help

Link to comment
Share on other sites

find the ids that repeating first
Then take those ids and join to original table to create a case statment for ur condition

Link to comment
Share on other sites

SELECT COL1,
       CASE
          WHEN COUNT (1) OVER (PARTITION BY COL1 ORDER BY COL1) > 1 THEN NULL
          ELSE COL2
       END
          AS COL2
  FROM (SELECT 23 AS COL1, 121 AS COL2 FROM DUAL
        UNION ALL
        SELECT 23 AS COL1, 123 AS COL2 FROM DUAL
        UNION ALL
        SELECT 24 AS COL1, 125 AS COL2 FROM DUAL
        UNION ALL
        SELECT 25 AS COL1, 127 AS COL2 FROM DUAL
        UNION ALL
        SELECT 25 AS COL1, 124 AS COL2 FROM DUAL) TAB
Link to comment
Share on other sites

 Use count(col2) over (partition by col1 )   functions . If the Rank is >1 then make the second column null else leave it as is . 

 

select 

t.col1,

case when t.no_of_rows=1 then t.col2 else null end new_col2 

from 

(

select col1,

col2,

count(col2) over (partition by col1 ) no_of_rows

from 

Table A 

) t1

 

Hope it helps. 

Col1 col2
23 121
23 123
24 125
25 127
25 124

Epudu col1lo 23 ane value multiple times repeat ayindi so apudu col 2 null ipovali.

Result should be like this:

Col1 col2
23 Null
23. Null
24. 125
25. Null
25. Null

Plss help

 

Link to comment
Share on other sites

1) first in a cte get the counts = 1 man

2) below that use that CTE with exists clause and get all the values

3) rest all nulls man...

 

not at my kemputer to write query man...

Link to comment
Share on other sites

Col1 col2
23 121
23 123
24 125
25 127
25 124

Epudu col1lo 23 ane value multiple times repeat ayindi so apudu col 2 null ipovali.

Result should be like this:

Col1 col2
23 Null
23. Null
24. 125
25. Null
25. Null

Plss help

 

 

baaaa endi query lu aipoledaaa inka brahmi-hug-o.gif

Link to comment
Share on other sites

with x as

(select col1 from

(select col1,  count(col1)  cnt

from table group by col1)

where cnt > 1)

select col1,

case

    when col1 in (select col1 from x)

  then null

else col2  

from table;

Link to comment
Share on other sites

with x as

(select col1 from

(select col1,  count(col1)  cnt

from table group by col1)

where cnt > 1)

select col1,

case

    when col1 in (select col1 from x)

  then null

else col2  

from table;

Why subquery on top of it when you can achieve the same with one query man?? Also do you think case is needed in the case?

Link to comment
Share on other sites

Why subquery on top of it when you can achieve the same with one query man?? Also do you think case is needed in the case?


Emo mama... Oka query multiple ways lo rayachu... Naaku teliyakunda repu inko type lo rastanemo query... Edo sql query meeda thread chudagane edi rasesa...
Link to comment
Share on other sites

with x as

(select col1 from

(select col1,  count(col1)  cnt

from table group by col1)

where cnt > 1)

select col1,

case

    when col1 in (select col1 from x)

  then null

else col2  

from table;

gp

Link to comment
Share on other sites

×
×
  • Create New...