sql - Using case and like statment without duplicate results -
i trying use case , statement. when run below query duplicating results on rows y on particular columns. using sql server 2000.
select distinct [rjvn_pound_reference] ,t_reference ,t_street_name ,t_zone_name ,( case when rjvn_note '%correspondence%' 'y' else 'n' end ) correspondencereceived ,( case when rjvn_note '%review form complete%' 'y' else 'n' end ) reviewformcomplete ,( case when rjvn_note '%manually issued nto - drive off%' 'y' else 'n' end ) manuallyissuednto ,( case when rjvn_note '%manually issued ntk - drive off%' 'y' else 'n' end ) manuallyissuedntk ,( case when rjvn_note '%drive off- final reminder sent%' 'y' else 'n' end ) finalremindersent [icps].[dbo].[removal_job_vehicle_notes] inner join tickets t on (a.rjvn_pound_reference = t.t_number) inner join dbo.ticket_hold_record thr on (t.t_number = thr.thr_system_ref) thr_hold_type = '2207555' , t.t_contract in ('18','1')
results:
805464 xl05512211 selby (60-100) abbey walk n n y n n 805464 xl05512211 selby (60-100) abbey walk n y n n n 805464 xl05512211 selby (60-100) abbey walk y n n n n
but want displayed on single row instead like:
805464 xl05512211 selby (60-100) abbey walk y y y n n. updated : 944786 xl07075730 selby (60-100) abbey walk n n n n y should 944786 xl07075730 selby (60-100) abbey walk n n y n y
my actual table data record :
rjvn_pound_reference rjvn_time rjvn_userid rjvn_note 944786 2014-01-08 10:38:17.000 jamied vehicle drove away during pcn issue 944786 2014-01-08 14:22:21.000 adrianj correspondent scanned 944786 2014-02-05 15:44:53.000 ellied manually issued nto drive off 944786 2015-03-12 10:16:53.000 katieb drive off- final reminder sent --manually issued nto drive off displayed 'n' instead of 'y'
because column references in query not qualified (to indicate which table returned from), , because don't have schema definition go through sort out... can't tell table rjvn_note
column comes from.
as far getting "duplicate" rows, explanation join
, there's one-to-many association, , multiple rows being returned. expected behavior.
to "collapse" rows, can add group by
end of query. can use aggregate functions (like min()
or max()
) "pick out" values returned expressions collapsed rows.
the distinct
keyword (as in query) operates on entire set of expressions in select list. eliminate rows exact duplicates.
there's couple of other alternatives useful in situations. example, using exists
predicate (it requirement test existence of row in related table, without producing "duplicates" join
operation would. or, using subquery in select
list.
because column references not qualified, , because don't have schema definition, we're guessing. of "maybe try this" guesses might turn out right, guesses.
here's "guess" @ changes need make query specified resultset:
- ditch
distinct
keyword - add
max()
aggregate around case expressions add
group by
clause listing non-aggregate expressions select list(optional) qualify column references table alias (several reasons this: aid future readers, , insulate statement future failure
select a.rjvn_pound_reference , t.t_reference , t.t_street_name , t.t_zone_name , max(case when a.rjvn_note '%correspondence%' 'y' else 'n' end ) correspondencereceived , max(case when a.rjvn_note '%review form complete%' 'y' else 'n' end ) reviewformcomplete , max(case when a.rjvn_note '%manually issued nto - drive off%' 'y' else 'n' end ) manuallyissuednto , max(case when a.rjvn_note '%manually issued ntk - drive off%' 'y' else 'n'end ) manuallyissuedntk , max(case when a.rjvn_note '%drive off- final reminder sent%' 'y' else 'n' end ) finalremindersent [icps].[dbo].[removal_job_vehicle_notes] join [tickets] t on t.t_number = a.rjvn_pound_reference join [dbo].[ticket_hold_record] thr on thr_system_ref = t.t_number thr.thr_hold_type = '2207555' , t.t_contract in ('18','1') group a.rjvn_pound_reference , t.t_reference , t.t_street_name , t.t_zone_name
Comments
Post a Comment