Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello friends,
I was not getting how to do grouping
I have table FWB_VWForcSummCalc having follwing values:
ForcSumID     ForcSummCalDesc    OrderID
100	      100% & Contracts	 1
60	      Below 60%	         4
60-90	      60% - 90%	         3
90	      90%	         2


I have query which gives me following result:
ProbabilityPercent  Q1      Q2     Q3     Q4
90                  Null    Null   640    Null
90                  Null    500    2430   1523
75                  1400    Null   Null   Null
75	            Null    Null   260.00 Null
60	            NULL    NULL   NULL	  18304.00
40	            NULL    NULL   NULL	  75144.99
30	            NULL    NULL   200.00 NULL
20	            NULL    NULL   NULL	  37500.00
10	            2580.00 NULL   NULL	  NULL


What i expect the result in this format:
OrderID ForSummCalDesc Q1 Q2 Q3 Q4


Where Q1, Q2, Q3, Q4 are total according to ProbabilityPercentage

Till now I have written this query
SELECT ProbabilityPercent,
CASE WHEN CONVERT(INT,MONTH(ClosedDate)) IN (7,8,9) THEN
SUM(ORDERVALUE) END Q1,
CASE WHEN CONVERT(INT,MONTH(ClosedDate)) IN (10,11,12) THEN
SUM(ORDERVALUE) END Q2,
CASE WHEN CONVERT(INT,MONTH(ClosedDate)) IN (1,2,3) THEN
SUM(ORDERVALUE) END Q3,
CASE WHEN CONVERT(INT,MONTH(ClosedDate)) IN (4,5,6) THEN
SUM(ORDERVALUE) END Q4
FROM FWB_Opportunities OPP
INNER JOIN dbo.FWB_ufn_GetOpportunity(@empid) GETOPP ON OPP.OpportunityId = GETOPP.OpportunityId
WHERE ClosedDate Between @startDate AND @endDate
AND ACTIVE = 1
AND GETOPP.AccountID=ISNULL(@accountid,GETOPP.AccountID) AND GETOPP.VCID=ISNULL(@vcid,GETOPP.VCID)
AND GETOPP.GeoID= ISNULL(@geoid,GETOPP.GeoID)
AND OPP.ProbabilityPercent = ISNULL(@probPerc,OPP.ProbabilityPercent)
GROUP BY ProbabilityPercent,ClosedDate
ORDER BY ProbabilityPercent DESC


Any idea, how I can do this

Thanks in advance
Posted
Comments
RDBurmon 9-May-12 5:33am    
What is the issue ? Can you state it with some example of out put from final query ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900