SQL First Match or just First -


because part of larger sql select statement, want wholly sql query selects first item matching criteria or, if no items match criteria, first item.

i.e. using linq want:

dim t1 = t in tt  dim t2 = t in t1 criteria(t)  dim firstifany = t in if(t2.any, t2, t1) take 1 select t 

because if not part of linq, linqpad doesn't show single sql statement, two, second depending upon whether criteria matches of tt values.

i know select top 1 etc. , can add order by clauses specific first 1 want, i'm having trouble thinking of straightforward way first of 2 criteria. (it @ point when able solve myself.)

seeing don't see existing question this, let stand. i'm sure else see answer quickly.

select top 1 * (     select top 1 *, 1 rank mytable somecolumn = mycriteria     union     select top 1 *, 2 rank mytable order myordercolumn )  order rank 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -