Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to get in single row based on ID

Author  Topic 

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2013-04-29 : 09:40:53
HERE I have query where i need to get all rows based on EMPID and BranchID in one row but here it is giving
result like this one :

SELECT DISTINCT
(ed.EmpID),
(ab.Branch_ID),
(SELECT
SUM(CASE WHEN cs.IsAccepted IS NULL AND cs.IsRejected IS NULL THEN 1 ELSE 0 END)
FROM
Cust_ExpressIntrestLintStatus cs
WHERE cs.ToCustId = emu.CreatedByCustID GROUP BY ExpressinterstID )
AS Pending,
SUM(CASE WHEN ces.IsAccepted = 1 THEN 1 ELSE 0 END) AS Accepted,
SUM(CASE WHEN ces.IsRejected = 1 THEN 1 ELSE 0 END) AS Rejected,
COUNT(Distinct emu.Emp_FollowupTicket_ID ) AS Assigned
FROM
Cust_BasicInfo cb
LEFT JOIN Cust_Details cd
ON cd.Cust_ID = cb.Cust_ID
LEFT JOIN Cust_Login cl
ON cl.Cust_ID = cd.Cust_ID
LEFT JOIN Emp_Details ed
ON ed.EmpID = cb.ProfileOwnerEmpID
LEFT JOIN Adm_Branch ab
ON ab.Branch_ID = Ed.BranchID
LEFT JOIN Emp_MatchFollow_UpTicket emu
ON emu.CreatedByCustID = cb.Cust_ID OR emu.CreatedByCustID = cb.Cust_ID
LEFT JOIN Cust_ExpressIntrestLintStatus ces
ON EMU.ExpressintrestID = CES.ExpressIntrestLintStatus_ID
LEFT JOIN Cust_ProfileExpressInterest cpl
ON cpl.Cust_ProfileInterestsLog_ID = ces.ExpressinterstID
WHERE ab.Branch_ID IS NOT NULL AND ed.EmpID IS NOT NULL
GROUP BY ed.EmpId,Ab.Branch_ID,emu.CreatedByCustID

I am getting output like this :

EmpID Branch_ID Pending Accepted Rejected Assigned
2 9 NULL 0 0 0
2 9 0 0 1 1
2 9 0 1 0 1
3 8 NULL 0 0 0
3 8 0 1 0 1
4 8 NULL 0 0 0
15 8 NULL 0 0 0


i need to get output like this :


EmpID Branch_ID Pending Accepted Rejected Assigned
2 9 NULL 1 1 1
3 8 NULL 1 0 1
4 8 NULL 0 0 0
15 8 NULL 0 0 0


here observe empid and branchid are unique and rest of all case condition. i have used group by and having and max condition but result is not getting in correct way

P.V.P.MOhan

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-04-30 : 01:10:45

The group by clause has emu.CreatedByCustID, what are the values in that column.
May be you don't want to include this field in your grouping criteria...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-30 : 04:01:06
[code]
SELECT
ed.EmpID,
ab.Branch_ID,
SUM(CASE WHEN cs.IsAccepted IS NULL AND cs.IsRejected IS NULL THEN 1 ELSE 0 END) AS Pending,
SUM(CASE WHEN ces.IsAccepted = 1 THEN 1 ELSE 0 END) AS Accepted,
SUM(CASE WHEN ces.IsRejected = 1 THEN 1 ELSE 0 END) AS Rejected,
COUNT(Distinct emu.Emp_FollowupTicket_ID ) AS Assigned
FROM
Cust_BasicInfo cb
LEFT JOIN Cust_Details cd
ON cd.Cust_ID = cb.Cust_ID
LEFT JOIN Cust_Login cl
ON cl.Cust_ID = cd.Cust_ID
LEFT JOIN Emp_Details ed
ON ed.EmpID = cb.ProfileOwnerEmpID
LEFT JOIN Adm_Branch ab
ON ab.Branch_ID = Ed.BranchID
LEFT JOIN Emp_MatchFollow_UpTicket emu
ON emu.CreatedByCustID = cb.Cust_ID OR emu.CreatedByCustID = cb.Cust_ID
LEFT JOIN Cust_ExpressIntrestLintStatus ces
ON EMU.ExpressintrestID = CES.ExpressIntrestLintStatus_ID
and cEs.ToCustId = emu.CreatedByCustID
LEFT JOIN Cust_ProfileExpressInterest cpl
ON cpl.Cust_ProfileInterestsLog_ID = ces.ExpressinterstID
WHERE ab.Branch_ID IS NOT NULL AND ed.EmpID IS NOT NULL
GROUP BY ed.EmpId,Ab.Branch_ID
[/code]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -