sql - Exclude the specific kind of record -
i using sql server 2008 r2. have records below in table :
id sys dia type uniqueid 1 156 20 first 12345 2 157 20 first 12345 3 150 15 last 12345 4 160 17 average 12345 5 150 15 additional 12345 6 157 35 last 891011 7 156 25 average 891011 8 163 35 last 789521 9 145 25 average 789521 10 156 20 first 963215 11 150 15 last 963215 12 160 17 average 963215 13 156 20 first 456878 14 157 20 first 456878 15 150 15 last 456878 16 160 17 average 456878 17 150 15 last 246977 18 160 17 average 246977 19 150 15 additional 246977
regarding data, these records kind of groups have common uniqueid. records can of type "first, last, average , additional". now, these records want select "average" type of records if have "first" or "additional" kind of reading in group. else want exclude them selection..
the expected result :
id sys dia type uniqueid 1 156 20 first 12345 2 157 20 first 12345 3 150 15 last 12345 4 160 17 average 12345 5 150 15 additional 12345 6 157 35 last 891011 7 163 35 last 789521 8 156 20 first 963215 9 150 15 last 963215 10 160 17 average 963215 11 156 20 first 456878 12 157 20 first 456878 13 150 15 last 456878 14 160 17 average 456878 15 150 15 last 246977 16 160 17 average 246977 17 150 15 additional 246977
in short, don't want select record have type="average" , have "last" type of record same uniqueid. solution?
using exists
operator along correlated sub-query:
select * dbo.table1 t1 [type] != 'average' or exists (select * table1 t2 t1.uniqueid = t2.uniqueid , t1.[type] = 'average' , t2.[type] in ('first','additional'))
Comments
Post a Comment