sql server - SQL subs select sometimes returning 2 values causing extra Rows -
i have sql statement following. right it's returning values both projectrate , rate. want return rate when rate >= 0 , not null. if that's not true want return project rate when project rate >= 0 , not null. if both null or both <0 want return 0. right it's returning rate , projectrate if both exist instead of picking rate on projectrate
and have tables joined correctly, used generic names keep simple
edit: here's full query
select distinct ph.id_projecthours, ph.status, ph.hoursquantity, ph.entrydate, ph.entryyear, ph.entrymonth, ph.entryweek, ph.entrydescription, ph.deliverablecode, ph.wbsline, ph.itemvalue, ph.itemdescription, ph.lastmodifieddate, ph.lastmodifieduser, ph.synergyrequestid, p.projectname, p.projectdescription, p.projectmanager, p.projectbudgettimemat, p.projectbudgetfixed, p.projectrate, p.projectbudget, p.projectcontingency, p.contingencytype, p.contingencyremaining, p.discount, p.golivedate, p.invoiceinstruction, e.employeename, e.fte, b.typename, bt.budgettypename, cc.costcentername, cc.costcenterdescription, cfu.cfuname, d.departmentname, c.customername, c.customersynergyid, i.internalrate, i.currency, i.startdate ratestart, i.enddate rateend, (select(coalesce(case when per.rate > 0 rate else null end, case when p.projectrate > 0 p.projectrate else null end, 0))) calcrate dbo.projecthours ph inner join dbo.project p on p.id_project = ph.id_project inner join dbo.employee e on e.id_employee = ph.id_employee inner join dbo.billabletype b on b.id_billabletype = ph.id_billabletype inner join dbo.budgettype bt on p.id_budgettype = bt.id_budgettype inner join dbo.costcenter cc on cc.id_costcenter = p.id_costcenter inner join dbo.cfu on cc.id_cfu = cfu.id_cfu inner join dbo.department d on cfu.id_department = d.id_department inner join dbo.customer c on c.id_customer = p.id_customer left join dbo.internal on i.id_employee = e.id_employee , i.internalrate <> 0 , ph.entrydate between i.startdate , i.enddate left join dbo.projectemployeerates per on per.id_employee = e.id_employee
try case statement this:
case when rate >= 0 rate when projectrate >= 0 projectrate else 0 end
Comments
Post a Comment