i have the following tables:
VISITS:
vid,
pid,
date
PATIENT:
pid,
pname,
age,
gender
so i want to know the average of visits for each patient.
I have tried to solve it so hard, but still can't get it done!
hope someone can help me out.
You can do this by counting the total number of visits and dividing by the total number of patients.
select (select count(*) from visits) / (select 1.0*count(*) from patients) as AvgVisitsPerPatient
Note the following:
The 1.0* is needed to change the count to a decimal. SQL Server does integer division on integers.
The use of the patients table. Some patients may not have any visits, and they are included.
The use of nested subqueries in the select. This is allowed.
EDIT:
You do not have enough information to calculate averages for a given unit of time. Although you have the date for a visit, you don't have a date for the patient, so you don't know who the population is at any given point of time.
The Average number of visits for each patient is not possible. There is only one scalar value for the total number of visits for any single patient. You cannot average that. You could average the number of visits for all patients, or the number of visits per month for each patient, but you cannot average a set of values when there is only one value in the set. (or to be technically accurate, you can average a single value, but the average is the same as the value because the count is one.)
Average number of visits for all patients =
Sum of visits / number of patients
Select Avg(Count(*))
From Visits
Group By pid
Average number of visits per month for each patient
Select p.pid, p.pname,
Avg(z.monthVisits) AvgMonthlyVisits
From (Select Pid, Count(*) monthVisits
From table
Group By Pid, Month(date)) z
join Patient p
on p.pid = z.pid
Group By p.pid
Related
I have a table which have columns- customer_id, order_id, order_value, city and Date. I need to find the following
The average number of orders in the district needs to be calculated at customer level.
Example: Three customers in a district named as 'xyz' have placed below number of orders
CUSTOMER|ORDER_COUNT
CUS1 | 5
CUS2 | 2
CUS3 |3
I need to find the average for each district comprising of count of orders in this case cus1(5), cus2(2), cus3(3) which is 3.3
please help me on how do i move ahead with the query.. Thanks
You may perform two levels of aggregation, in the first one find the count of orders grouped by city, customer_id, and in the second one find the average of that count grouped by city.
select city, AVG(cn) avg_orders_count
from
(
select customer_id, city, count(*) cn
from orders_table
group by customer_id, city
) T
where city='xyz'
group by city
See a demo.
My source table contains sales information. Each row is a person and records every time they've shopped/where. I can therefore calculate the average transaction value per industry by the following:
select
industry,
COALESCE(AVG(CASE WHEN shopcode in (1,2,4) THEN dollar END), 0) AS avt
from sales
group by industry
But how can I adapt this to calculate the spend per distinct count of user i.e.: sum(dollar)/count(distinct person) so similar to above but instead of sum/count(*) sum/count(distinct person)... I need to use coalesce with this as well.
how can i adapt this to calculate the spend per distinct count of user i.e.: sum(dollar)/count(distinct person)
You can use:
select industry,
sum(dollar) / count(distinct person)
from sales
group by industry;
I'm not sure what the filtering on shop_code is for. It is in your query but not part of the question. If you want this for particular shops, I would suggest moving this to a where clause:
select industry,
sum(dollar) / count(distinct person)
from sales
where shop_code in (1, 2, 4)
group by industry;
I have huge database and I need to get top 90% average of all category using group by.
Example, I have 300 locations and data is around 100k with TAT column against all dockets, I need to take min 90% average of TAT all location in one query using group by(location).
Most DBMSes support Windowed Aggregate Fuctions, you need PERCENT_RANK:
select location, avg(TAT)
from
(
select location, TAT,
-- assign a value between 0 (lowest TAT) and 1 (highest TAT) for each location
percent_rank() over (partition by location order by TAT) as pr
from tab
) as dt
where pr <= 0.9 -- exclude the highest TAT amounts
group by location
(Easy) Find the difference between each student’s average grade and the best average grade in his group.
pls help....
i am trying hard to figure out the problem still not solved.
i should use partition function and how to generate the differences between the best student average in a group to that of the lower grades students.
this is my query,,
**select
students.st_id,students.st_group,
rank() over (partition by students.st_group order by avg(grades.g_grade) desc ) as ranking ,
lead(avg(grades.g_grade), 1, 0) OVER (PARTITION BY students.st_group ORDER BY avg(grades.g_grade) DESC NULLS LAST) DifferencebetweenHigheraverage,
avg(grades.g_grade)
from grades left join students on grades.g_student=students.st_id
group by students.st_id,students.st_group
having students.st_group in (1,2,3);**
Pls help.......
if in the table STUDENT_GRADES, i have attributes
ST_ID
ST_GROUP
ST_GRADE
select query to get student's difference from maximum grade among his group is
with MAXGRADES as (select ST_GROUP, max(ST_GRADE) as MAX from STUDENT_GRADES)
select ST_ID, ST_GRADE - MAX as DIFF from STUDENT_GRADES inner join MAXGRADES using(ST_GROUP)
I have been at this for the past two hours and have tried many different ways in regards to subquery and joins. Here's the exact question "Get the name and city of customers who live in the city where the least number of products are made"
Here is a snapshot of the database tables
I know how to get the min
select min(quantity)
from products
but this returns just the min without the city attached to it so I can't search for the city in the customers table.
I have also tried group by and found it gave me 3 min's (one for each group of cities) which i believe may help me
select city,min(quantity)
from products
group by city
Putting everything together I got something that looks like
SELECT
c.name,c.city
FROM
customers c
INNER JOIN
(
SELECT
city,
MIN(quantity) AS min_quantity
FROM
products
GROUP BY
city
) AS SQ ON
SQ.city = c.city
But this returns multiple customers, which isn't correct. I assume by looking at the database the city when the lowest number of products seems to be Newark and there are no customers who reside in Newark so I assume again this query would result in 0 hits.Thank you for your time.
Example
Here is an example "Get the pids of products ordered through any agent who makes at least one order for a customer in Kyoto"
and the answer I provided is
select pid
from orders
inner join agents
on orders.aid = agents.aid
inner join customers
on customers.cid = orders.cid
where customers.city = 'Kyoto'
In Postgresql you have sophisticated tools, viz., windowing and CTEs.
WITH
find_least_sumq AS
(SELECT city, RANK() OVER ( PARTITION BY city ORDER BY SUM(quantity) ) AS r
FROM products)
SELECT name, city
FROM customers NATURAL JOIN find_least_sumq /* ON city */
WHERE r=1; /* rank 1 is smallest summed quantity including ties */
In Drew's answer, you are zeronig in on the cities where the smallest number of any particular item is made. I interpret the question as wanting the sum of items made in that city.
I guess it be something around this idea:
select customers.name, city.city, city.min
from customers
join (
select city, sum (quantity) as min
from products
group by city
--filter by the cities where the total_quantity = min_quantity
having sum (quantity) = (
--get the minimum quantity
select min(quantity) from products
)
) city on customers.city = city.city
This can be made so much simpler. Just sort the output by the field you want to get the minimum of.
SELECT city, quantity FROM customers ORDER BY quantity LIMIT 1;
I have just figured out my own answer. I guess taking a break and coming back to it was all I needed. For future readers this answer will use a subquery to help you get the min of a column and compare a different column (of that same row) to a different tables column.
This example is getting the city where the least number of products are made (quantity column) in the products table and comparing that city to the cities to the city column in the customers table, then printing the names and the city of those customers. (to help clarify, use the link in the original question to look at the structure of the database I am talking about) First step is to sum all the products to their respective cities, and then take the min of that, and then find the customers in that city.Here was my solution
with citySum as(
select city,sum(quantity) as sum
from products
group by city)
select name,city
from customers
where city
in
(select city
from citySum
where sum =(
select min(sum)
from citySum))
Here is another solution I have found today that works as well using only Sub queries
select c.name,c.city
from customers c
where c.city
in
(select city
from
(select p.city,sum(p.quantity) as lowestSum
from products p
group by p.city) summedCityQuantities
order by lowestsum asc
limit 1)