SQL Server - Missing records in join -


i'm trying accomplish following using below query:

  • take records dial table , group based on various criteria contained within table
  • join table history table on unique key (dial.uid , history.uid) return information unobtainable dial table

all working fine, apart not returning rows dial table have uid of 0 (this updated in both tables once record has been processed, if hasn't been processed, remains @ 0). count of records have uid of 0 added total avail. , total eligible columns, based on dial.id

can help?

regards

select      (case          when history.id = 824 or project.name 'example1%' or project.name 'example2' or project.name '%example3%'            'team one'         when history.id = 814 or project.name '%example4%'            'team two'          else 'other'       end) [team],      project.name [project],      count(dial.id) [total avail.],      count (case                 when dial.flag = '1' , dial.att = 0 or dial.att > 0 , dial.flag = 0                 1                 else null              end) [total eligible],      isnull(sum(history.attempt), 0) [total attempts],      round(count(cast(isnull(history.attempt, 0) float)) / count(case when dial.flag = '1' or dial.attempts > 0 1 else null end), 2)*100 [penetration %],      count(history.attempt) [unique attempts],       count (case when history.code in ('ex1','ex2','ex3','ex4','ex5')              1 else null end) [contacts],      count (case when history.code in ('ex6','ex6','ex7')              1 else null end)  [success]       dial dial left join       history history on dial.uid = history.uid left join       project project on dial.id = project.id       dial.field2 not ''      , dial.id not '-%'      , history.datetime > convert(datetime, convert(char(8), getdate(), 112)) group       project.name, history.id 

let's @ from , where parts of query:

from dial dial left join      history history      on dial.uid = history.uid left join      project project      on dial.id = project.id dial.field2 not '' ,       dial.id not '-%' ,       history.datetime > convert(datetime,convert(char(8),getdate(),112)) 

your use of left join correct, in sense want keep in dial table. however, undo left join last comparison on history.datetime. null when there no matching record. , null fail comparison. can fix adding coalesce() or is null where clause.

another fix add logic on clause:

from dial dial left join      history history      on dial.uid = history.uid ,         history.datetime > convert(datetime,convert(char(8),getdate(),112)) left join      project project      on dial.id = project.id dial.field2 not '' ,       dial.id not '-%' 

if using more recent version of sql server, can date comparison converting date:

from dial dial left join      history history      on dial.uid = history.uid ,         history.datetime > cast(getdate() date) left join      project project      on dial.id = project.id dial.field2 not '' ,       dial.id not '-%' 

it makes query easier read.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -