Select one row over another

I have a table that looks like this:

ID   Type   Value
A    Z01    10
A    Z09    20
B    Z01    30
C    Z01    40
D    Z09    50
E    Z10    60

For each identifier, I would like to get a value. Ideally, the value should come from a string of type Z01. However, if Z01 is not available, I will take Z09 instead. If nothing is available, I would not want to choose anything.

The result will look like this:

Id   Type   Value
A    Z01    10
B    Z01    30
C    Z01    40
D    Z09    50

How can I accomplish this using T-SQL?

+4
source share
6 answers

This should give you what you want:

select *
from table t
where 1 = case 
              when t.type = 'Z01' 
                  then 1 
              when t.type = 'Z09' 
                    and not exists (select 1 from table where id = t.id and type = 'Z01')
                  then 1 
          else 0 
       end

An alternative, using a more general approach, is (rewriting an expression CASE):

select *
from table
where type = 'Z01'
    OR (type = 'Z09' and not exists (select 1 from table where id = t.id and type = 'Z01'))

The obvious approach sargable(which will force your query to use the appropriate index in your table, if one exists):

select *
from table
where type = `Z01`

union all
select *

from table
where type = `Z09`
    and not exists (select 1 from table where id = t.id and type = 'Z01')

, type.

+3

;WITH cte
AS (SELECT
    *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY type) AS rn
FROM yourtable
WHERE type IN ('Z01', 'Z09'))
SELECT
    id, type, value
FROM cte
WHERE rn = 1
0

:

select t.*
from (select t.*,
             row_number() over (partition by id
                                order by (case when type = 'Z01' then 1
                                               when type = 'Z09' then 2
                                          end)
                               ) as seqnum
      from t
     ) t
where seqnum = 1;

" , ". , , "Z01", "Z09" , :

select t.*
from (select t.*,
             row_number() over (partition by id order by type) as seqnum
      from t
      where type in ('Z01', 'Z09')
     ) t
where seqnum = 1;

where , .

0

 Select distinct a.id, 
    coalesce(t1.type, t9.type) type,
    case when t1.type is not null 
         then t1.value else t9.value end value
 From table a 
    left join table t1 
       on t1.id = a.id 
          and t1.type = 'Z01'
    left join table t9 
       on t9.id = a.id 
          and t9.type = 'Z09'
  Where a.type in ('Z01', 'Z09')  -- < -- this last to eliminate row E
0

- :

select distinct
base.Id,
Coalesce(z01.Type, any.Type) Type,
Coalesce(z01.Value, any.Value)
from (select distinct id from [table]) base
left outer join [table] z01
    on z01.id = base.id
    and z01.Type = 'Z01'
left outer join [table] any
    on any.id = base.id
    and any.type != 'Z01'
0

WHERE. Z01 Z09. ROW_NUMBER .

select id, type, value 
from
(
    select 
      id, type, value, 
      row_number() 
        over (partition by id order by case when type = 'Z01' then 1 else 2 end) as rn
    from mytable
    where type in ('Z01','Z09')
) ranked
where rn = 1;
0

Source: https://habr.com/ru/post/1657287/


All Articles