« first day (571 days earlier)      last day (2816 days later) » 

4:10 PM
begin tran asdf;

create table TheData
(
	PersonName nvarchar(max) not null,
	CurrentStep nvarchar(max)
)

insert into TheData(PersonName, CurrentStep) values
('Bob', 'Building'),
('Sue', 'Building'),
('Joe', 'Running'),
('Mary', 'Testing'),
('Billy', 'Building');

select * from TheData

/* find the percent of each status

	Building - 3/5, 60%
	Running - 1/5, 20%
	Testing - 1/5, 20%
*/

--easy to get the total people per step (nominator)
select
	td.CurrentStep,
	count(td.PersonName) [PeopleInStep]
in this example, I have a list of people with a given "step"
I want to find out the percentage of people at each step
as the comment inside says, there are 5 people in the data set, "Building" has 3 people, so the answer would be 60% for that entry
The thing I would like to do is do the last query without a subquery or CTE
 
hmm. This is a bit tougher than I was expecting.
I feel like Group By can be of some use here, but I'm not sure exactly how.
 
in LINQ (c#) you can use a Count method, but add a filter, like:
myList.Count(x => x > 10);
that would give the count of items where that item was > 10
it feels like I need something like that in sql
 
group by is an aggregator, so stuff like
select currentStep, count(personName)
from theData
group by currentStep
 
right, that gets you the person count per step
but then you also need the total number of records in the table so you can do the division
 
yes, and then to turn that to a percentage, I'm not sure how to do that without a subselect for the total count
 
4:25 PM
yeah, I don't think what I'm asking for is possible
 
We'll wait for rene then. I want to see mad SQL-fu
 
because you need to have already calculated the number of rows beforehand to do the division
ok
afk for lunch
 
Or just a definitive that won't work
@gunr2171 have fun
 
5:17 PM
select CurrentStep, count(*) * 100.0 / sum(count(*)) over()
from TheData
group by CurrentStep
 
hum
ok, now how does that work?
the only time I've used over is with row numbers
but yes, that gives the correct answer
 
still not sure how it really works internally either
 
well, that's a problem for another day
afk for a while
thanks though!
 
yw
 
@Tunaki yeah I was looking at that as well
select distinct td.CurrentStep,
       count(td.PersonName) over (partition by currentstep)  ,
       count(personname)  over (partition by 1)
from #TheData td
 
5:29 PM
ah that looks nice also
 
6:05 PM
huh. I'll have to look more into partition stuff
 
Hello!
oops; I thought this was SOCVR
 
haha
Just discussing SQL. Learning more stuff every day.
 

« first day (571 days earlier)      last day (2816 days later) »