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.
Author |
Topic |
mimuk
Starting Member
19 Posts |
Posted - 2013-12-05 : 13:10:53
|
I hope someone can help with a Query/View I need to create.I have table called OpportunityCREATE TABLE [dbo].[Opportunity]( [Oppo_OpportunityId] [int] NOT NULL, [Oppo_AssignedUserId] [int] NULL, [Oppo_Stage] [nvarchar](40) NULL,If I query the table the data is output as:Select Oppo_OpportunityId, Oppo_AssignedUserID, Oppo_Stage from Opportunity Oppo_OpportunityId Oppo_AssignedUserID Oppo_Stage1 23 Closed 2 23 Negotiating3 23 Negotiating4 24 Closed5 25 Quoted6 25 Closed7 25 Closed8 25 Closed9 25 Lost I need the data presented as follows:AssignedUserID Quoted Negotiating Lost Closed Total Ratio 23 0 2 0 1 3 3324 0 0 0 1 1 10025 1 0 1 3 5 60 Ratio is calculated as the number of Closed / Total * 100I hope this makes sense. I am really scratching my head on this.Cheers,Mim |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-05 : 13:28:43
|
[code]SELECT Oppo_AssignedUserID AS AssignedUserID,[Quoted],[Negotiating],[Lost],[Closed],[Quoted] + [Negotiating] + [Lost] + [Closed] AS Total,[Closed]*100.0/[Quoted] + [Negotiating] + [Lost] + [Closed] AS RatioFROM(SELECT Oppo_AssignedUserID,Oppo_Stage,Oppo_OpportunityIdFROM [dbo].[Opportunity])tPIVOT (COUNT(Oppo_OpportunityId) FOR Oppo_Stage IN ([Quoted],[Negotiating],[Lost],[Closed]))p[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
mimuk
Starting Member
19 Posts |
Posted - 2013-12-05 : 14:53:52
|
HI visakh16, thats almost there, thank you.I just get "Divide by zero error encountered." |
|
|
mimuk
Starting Member
19 Posts |
Posted - 2013-12-05 : 15:06:35
|
[Closed]*100.0/NULLIF([Quoted] + [Negotiating] + [Lost] + [Closed],0) AS RatioSeems to have fixed it... Many thanks, really appreciate it |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-06 : 04:51:04
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|