sql - Removing Duplicate Rows in Employee Demographic Data Pull -


i doing data pull demographic information on employees.
had issue missing employees because not has phone number (i don't know why)

now have duplicates because people have primary , not-primary phone number.
can listed mobile, home, work, etc... want if employee has primary phone number list it, if don't have number @ want listed "n/a".

here query below. appreciate assitance.

select  distinct         ejc.persontaxidno [ssn]         ,ejc.empno [employee id]         ,isnull(ejc.salutation, 'n/a') [salutation]         ,ltrim(ejc.firstname) [first name]         ,ltrim(ejc.lastname) [last name]         ,isnull(ejc.namesuffix,'n/a') [suffix]         ,vpa.personaddress1 [address]         ,isnull(vpa.personaddress2,'') [address 2]         ,vpa.personaddresscity [city]         ,vpa.personaddressstateabbrev [state]         ,vpa.personaddresspostalcode [zip]         ,case when vph.personphoneprimaryind = 1 , (getdate() between     vph.personphonefromeffectdate , vph.personphonetoeffectdate)     vph.personphoneno             when vph.personphoneno null 'n/a' end     [primary phone] cnkronos.cnkronos.dbo.employeejob_curr ejc     left join cnkronos.cnkronos.dbo.vperson_addresses vpa         on ejc.personidno = vpa.personidno     left join cnkronos.cnkronos.dbo.vperson_phones vph         on ejc.personidno = vph.personidno vpa.personaddressprimaryind = 1     , getdate() between vpa.personaddressfromeffectdate ,     vpa.personaddresstoeffectdate order ltrim(ejc.lastname), ltrim(ejc.firstname)  

.

personidno | personphoneseqno | personphonetypeidno | personphonetype | personphoneprimaryind | personphoneno | personphoneext | personphonefromeffectdate | personphonetoeffectdate | personphonechangedate   | personphoneissmssw | wtkphoneidno   | timestamp 71376      | 8129             | 23                  | mobile          | 1                     | 123-456-7890  |                | 2013-11-21 00:00:00.000   | 3000-01-01 00:00:00.000 | 2014-04-10 20:58:53.450 | 0                  | 2              | 0x000000000503db2e 71376      | 8130             | 21                  | work            | 0                     | 234-567-8901  |                | 2013-11-21 00:00:00.000   | 3000-01-01 0:00:00.000  | 2014-04-10 20:58:53.760 | 0                  |   3              | 0x000000000503db31 

is hard give solution without having schema.
try teach how using cte common table expressions

sql fiddle demo

in fiddle can select code inside with see partial result.

that query valid phone inside date range. user1 have data user2 doesnt

with current_phone (    select u.name, p.*            users u     left join phone p         on u.user_id = p.user_id        , getdate() between fromdate , todate ) select name, case                   when phone null 'na'                  else phone              end phone current_phone 

the idea small calculation in cte , join result main table. hope find solution.

you can update sqlfiddle better schema case , let me know.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -