Question : SQL Query to give percentage of colour customers

I need to create a query to return the number and percentage of total customers that had a colour service in a POS database for a hairdressing salon. The data is in the sale header table: salehdr and the sale lines table: saleline. They are linked by the salehdrID field, with a one to many relationship. The colour services are saleline.categoryID=3. There may be more than one saleline record with categoryID=3 for each salehdr record but in that case it needs to count as 1, not the number of saleline records. Also, any sales that have no services: only catedoryID = 7 shouldn't be included at all.
The results need to be grouped by salehdr.stylistname, to give count and percentage of total for each stylist, as well as for overall. This can be in the same query or two seperate queries.
I need to be able to filter by salehdr.saledate to give results for a lingle day or a range.

I have only basic SQL knowledge so I'm hoping there is an expert that can help.

Answer : SQL Query to give percentage of colour customers

That only grouped them by sale (regardless of sale items).
This groups by sales person
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
select tot.stylistname,
  COUNT(col.salehdrID) as CountColour,
  COUNT(tot.salehdrID) as CountTotal,
  1.0 * COUNT(col.salehdrID) / COUNT(tot.salehdrID) as PercentageColour
from (
select distinct h2.stylistname, h2.salehdrID
from salehdr h2
inner join saleline l2 on l2.salehdrID = h2.salehdrID
where l2.categoryID<>7
and h2.saledate between [Report Start] and [Report End]
) tot left join (
select distinct h1.stylistname, h1.salehdrID
from salehdr h1
inner join saleline l1 on l1.salehdrID = h1.salehdrID
where l1.categoryID=3
and h1.saledate between [Report Start] and [Report End]
) col on tot.stylistname=col.stylistname and tot.salehdrID=col.salehdrID
Group by tot.stylistname
Random Solutions  
 
programming4us programming4us