SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to get in single row based on ID
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 04/29/2013 :  09:40:53  Show Profile  Reply with Quote
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

547 Posts

Posted - 04/30/2013 :  01:10:45  Show Profile  Reply with Quote

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

India
52325 Posts

Posted - 04/30/2013 :  04:01:06  Show Profile  Reply with Quote

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



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000