SQL trouble with average function - sql

I am very new to SQL and am having a lot of trouble wrapping my head around certain things. The following code
SELECT Treated.ProgScore, Patient.Ethnicity
FROM Patient
JOIN City
ON Patient.ZIP = City.ZIP
JOIN Treated
ON Patient.SSN = Treated.PSSN
WHERE City.Cname != 'Dallas';
produces the following data. I would like to extract the average ProgScore of each ethnicity and list it from lowest to highest average ProgScore
PROGSCORE ETHNICITY
2 Japanese
5 Caucasian
9 Caucasian
2 African
3 Japanese
7 Caucasian
10 Japanese
8 Caucasian
1 African
4 Japanese
7 Caucasian
Is there a way to do this? I would like my output to look like this.
Average ETHNICITY
4.75 Japanese
7.2 Caucasian
Thanks for any help

It sounds like you want
SELECT avg(Treated.ProgScore), Patient.Ethnicity
FROM Patient
JOIN City
ON Patient.ZIP = City.ZIP
JOIN Treated
ON Patient.SSN = Treated.PSSN
WHERE City.Cname != 'Dallas'
GROUP BY Patient.Ethnicity
ORDER BY avg(Treated.ProgScore);

Related

Complex join with different data types in laravel 8 query builder

I have two tables Infos and nationalities like this :
1) Table infos:
id(pk)
name
nationality(varchar) accept multiple nationalities
1
John
1,2,3
2
Camilia
2,4
1) Table nationalities :
id(pk) int
desig
1
German
2
Turkey
3
Algeria
4
Qatar
I want to get in the output something like this:
desig
count
German
1
Turkey
2
Algeria
1
Qatar
1
So for that I tried something like this(but it did not help):
$total_nationalite = DB::table('infos')
->select('desig', 'nationalities.id',DB::raw('COUNT(*) as count'))
->join('nationalities', DB::raw('CAST(nationalities.id as varchar)'), '=', 'infos.nationality')
->groupBy('desig','nationalities.id')
->get();
please any suggestion with the query builder or with a simple query in postgres.
Having a comma separated list of values text in nationality field is a bad idea. Have an array of integers or - much better - a normalized design. Anyway here is a native query solution.
select desig, count(*) count
from
(
select unnest(string_to_array(nationality, ','))::integer nat_id
from infos
) t
join nationalities n on t.nat_id = n.id
group by desig;
DB Fiddle
desig
count
German
1
Qatar
1
Algeria
1
Turkey
2

I want to trim the names in SQL. for eg. my data has one name as American Airlines and same as "American airlines"

I want to trim the names in SQL. for eg. my data has one name as American Airlines and same as "American airlines.
I am looking forward to write a sql query
select customer_name, requests.
from Table1.
where code = "blue"
--output:
American airlines 25
Alaska Airline 45
American airlines 6
looking for output
American airlines 31
Alaska Airline 45
Can someone please help.:)
Thank you
Use replace for replacing the " with null
REPLACE(string1, string_to_replace, replacement_string)
e.g.
select replace(customer_name,'"','') as cusname ,sum(requests)
.....
group by cusname
Have you tried group by with sum ??
select replace(customer_name,'"','') as customer_name, sum(requests)
from Table1.
where code = "blue"
group by replace(customer_name,'"','');

union result of multiple dynamic queries sql

I have two tables tblJobs and tblJobseeker.
tblJobs
JobId skill location
5 .net, php Mexico
8 java Boston
9 sql, c++ London, Mexico
tblJobseeker
ID skill location
3 .net Mexico
7 sql Boston
10 java Boston
12 php Mexico
13 c++ London, Boston
Now I want to loop through first table tblJobs and find matching Jobseeker based on skill and location. For each record in tblJobs a result will come which I need to union with other records result set. I was trying to use cursor and dynamic query but how I can set condition of column skill and location in dynamic query. Also the records in both tables may vary
In above case, result should be
ID skill location
3 .net Mexico
12 php Mexico
10 java Boston
13 c++ London, Boston
I have edited the question. Here I am using charindex to match the result and inner join is not possible.There can be n number of locations or skills so different columns are not possible.
Simple inner join would do:
select s.*
from tblJobseeker s
inner join tblJobs j
on s.skill = j.skill
and s.location = j.location;

Multiple counts in a query

I'm writing a query that counts company complaints. The query should count the total number of complaints from a company and also break it down into who called from the company to make the complaint. So an example output would be:
Company Ref Company Name Company Complaints Caller Caller Complaints
11 Argos 10 Steve 8
11 Argos 10 JIM 2
So as you can see the query counts the total number of complaints, then also breaks it down to who complained. So far I have everything perfect apart from the total count. My code is:
SELECT COMPANYREF,
COMPANY,
(SELECT COUNT(COMPANYREF)
FROM XCALLSTABLE) AS CompanyCalls,
CALLER,
COUNT(*)
FROM XCALLSTABLE
JOIN XCUSTOMERTABLE ON (XCALLSTABLE.COMPANYREF = XCUSTOMERTABLE.CUSTREF)
JOIN XTELEPHONETABLE ON (XCUSTOMERTABLE.TELEPHONE = XTELEPHONETABLE.PHONENUMBER)
GROUP BY COMPANYREF,
COMPANY,
CALLER
HAVING COUNT(*) > 0 ;
Some sample output for the current code is:
Company Ref Company Name Company Complaints Caller Caller Complaints
145 Comfiture Traders 500 Alexis Patel 4
The issue here is the total count for the company just counts every row, whereas I'm trying to count the occurances of that company appearing in the column.
Any and all help would be greatly appreciated.
Using sstan's code gave a result of
111 Medusa Shipping 4 Lily Morgan 5
111 Medusa Shipping 4 Ruby Walker 6
Whereas the result should be
111 Medusa Shipping 11 Lily Morgan 5
111 Medusa Shipping 11 Ruby Walker 6
I'm sure there is a cleaner way of doing this, but the following query should work:
SELECT companyref,
company,
CompanyCalls,
caller,
COUNT(*)
FROM (SELECT c.companyref,
company,
COUNT(*) OVER (PARTITION BY c.companyref) AS CompanyCalls,
caller
FROM xcallstable c
JOIN xcustomertable ct
ON ct.custref = c.companyref
JOIN xtelephonetable t
ON t.phonenumber = ct.telephone)
GROUP BY companyref, company, CompanyCalls, caller
HAVING COUNT(*) > 0

statistic syntax in access

I want to do some statistic for the Point in my appliation,this is the columns for Point table:
id type city
1 food NewYork
2 food Washington
3 sport NewYork
4 food .....
Each point belongs to a certain type and located at the certain city.
Now I want to caculate the numbers of points in different city for each type.
For example, there are two types here :food and sport.
Then I want to know:
how many points of `food` and `sport` at NewYork
how many points of `food` and `sport` at Washington
how many points of `food` and `sport` at Chicago
......
I have tried this:
select type,count(*) as num from point group by type ;
But I can not group the by the city.
How to make it?
Update
id type city
1 food NewYork
2 sport NewYork
3 food Chicago
4 food San
And I want to get something like this:
NewYork Chicago San
food 2 1 1
sport 1 0 0
I will use the html table and chart to display these datas.
So I need to do the counting, I can use something like this:
select count(*) from point where type='food' and city ='San'
select count(*) from point where type='food' and city ='NewYork'
....
However I think this is a bad idea,so I wonder if I can use the sql to do the counting.
BTW,for these table data,how do people organization their structure using json?
this's what you want:
SELECT city,
COUNT(CASE WHEN [type] = 'food' THEN 1 END) AS FoodCount,
COUNT(CASE WHEN [type] = 'sport' THEN 1 END) AS SportCount
FROM point
GROUP BY city
UPDATE:
To get the results in an aggregated row/column format you need to use a pivot table. In Access it's called a Crosstab query. You can use the Crosstab query wizard to generate the query via a nice UI or cut straight to the SQL:
TRANSFORM COUNT(id) AS CountOfId
SELECT type
FROM point
GROUP BY type
PIVOT city
The grouping is used to count the number of Id's for each type. The additional PIVOT clause groups the data by city and displays each grouping in a separate column. The end result looks something like this:
NewYork Chicago San
food 2 1 1
sport 1 0 0