sql - how to get a percentage depending on a column value? -


update 05/18/15

having column customers names listed on it. how can percentage based in 1 customer per date? example

    customername     date     sam              04/29/15     joy              04/29/15     tom              04/29/15     sam              04/29/15     oly              04/29/15     joy              04/29/15                      04/29/15     sam              04/29/15                      04/29/15     sam              04/29/15     oly              04/29/15     sam              04/29/15     oly              04/30/15     joy              05/01/15 

notice column has 12 records, 2 of them blanks, won't count on percentage, ones has name. know percentage represents sam total(in case 10 records, sam % 50).

query should return

date             percentage 04/29/15            50 04/30/15            0 05/01/15            0 

update

i don't care other customers, lets treat them one. need know percentage sam total list.

any appreciated.

everyone seems using subqueries or derived tables. should perform , it's easy follow. try out:

declare @customername varchar(5) = 'sam';  select  [date],         @customername customername,         percentage = cast(cast(100.0 * sum(case when customername = @customername 1 else 0 end)/count(*) int) varchar(20)) + '%' @yourtable customername != '' group [date] 

results:

date       customername percentage ---------- ------------ --------------------- 2015-04-29 sam          50% 2015-04-30 sam          0% 2015-05-01 sam          0% 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -