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
 Creating a simple View

Author  Topic 

njguy
Starting Member

16 Posts

Posted - 2012-05-20 : 21:40:29
I was creating the VIEW below and at this point it works just fine but I want to add another column to show it as a percentage of the toal amount

SELECT UserType, COUNT(*) AS TypeCount
FROM dbo.Ideas
WHERE (Approved = 'Y') AND (Refrence IS NOT NULL)
GROUP BY UserType

An example result set would look like the following


Customer 10 50%
Emplyee 10 50%

Thanks

Tom

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-20 : 21:44:04
[code]
SELECT UserType, COUNT(*) AS TypeCount, COUNT(*) * 100.0 / COUNT(*) OVER() AS Percentage
FROM dbo.Ideas
WHERE (Approved = 'Y') AND (Refrence IS NOT NULL)
GROUP BY UserType
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

njguy
Starting Member

16 Posts

Posted - 2012-05-20 : 21:54:32
quote:
Originally posted by khtan


SELECT UserType, COUNT(*) AS TypeCount, COUNT(*) * 100.0 / COUNT(*) OVER() AS Percentage
FROM dbo.Ideas
WHERE (Approved = 'Y') AND (Refrence IS NOT NULL)
GROUP BY UserType



KH
[spoiler]Time is always against us[/spoiler]





I am getting a strange result set

Customer 33 1650.000000000000
Employee 27 1350.000000000000
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-20 : 22:11:50
Oops sorry,
should be

COUNT(*) * 100.0 / SUM(COUNT(*)) OVER() AS Percentage



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -