Solved

using T-SQL to get count and percentage

Posted on 2011-02-17
3
729 Views
Last Modified: 2012-05-11
Hi, experts

I use a SQL code like below

SELECT DATEPART(weekday, From_date) as week_day, COUNT (*) as CNT
FROM  table_A
group by DATEPART(weekday, From_date)
order by DATEPART(weekday, From_date)

it give me
week_day      CNT
1      2
2      3
4      5
5      7
6      2
7      10

How can I add the percentage in the 3rd column. so my result will be

week_day      CNT   Percent
1      2         2 / (2+3+5+7+2+10)
2      3        3 / (2+3+5+7+2+10)
4      5        5 / (2+3+5+7+2+10)
5      7        7 / (2+3+5+7+2+10)
6      2         2 / (2+3+5+7+2+10)
7      10       10 / (2+3+5+7+2+10)

Is there a simply way to do by T-SQL

0
Comment
Question by:rmtogether
3 Comments
 
LVL 11

Assisted Solution

by:JoeNuvo
JoeNuvo earned 167 total points
ID: 34922559
;with CTE AS ( 
SELECT DATEPART(weekday, From_date) as week_day, COUNT (*) as CNT
FROM  table_A
group by DATEPART(weekday, From_date)
 )
SELECT
	week_day, CNT, CAST(CNT * 100 as float) / (select sum CNT) from CTE) as percentage
FROM CTE
ORDER BY week_day

Open in new window

0
 
LVL 3

Assisted Solution

by:sunezapa
sunezapa earned 167 total points
ID: 34922608
for easy of reading I used "weekday" for DATAEPART etc.

 DECLARE @Totals as int;
	set @totals  = (SELECT COUNT(weekday)  FROM  dbo._Test);
	SELECT     weekday, COUNT(x) AS cnt, 100*COUNT(xxx)/@Totals as prcnt
	FROM         dbo._Test AS _Test_1
	GROUP BY weekday

Open in new window

0
 
LVL 22

Accepted Solution

by:
Thomasian earned 166 total points
ID: 34922657
You could use a combination of SUM OVER and COUNT to get the total count.

SELECT DATEPART(weekday, From_date) as week_day, COUNT (*) as CNT
     ,100.0 * COUNT(*) / SUM(COUNT(*)) OVER () [Percent]
FROM  table_A
group by DATEPART(weekday, From_date)
order by DATEPART(weekday, From_date)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 29
SQL Server R2 Stored procedure performance 8 33
MS SQL 2014 get SPIDs of users 6 26
Sql query for filter 12 22
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now