I am struggling with a SQL request.
I need to find a request to calculate the average loans per user.
I was thinking in doing a LEFT OUTER JOIN between the table SUSCRIBER and the table Loan in order to get all the subscribers even if they have made a loan or not.
Then I have used a GROUP BY based on the IDNumber of the suscribers to COUNT the number of lines. Then I need to get the total numbers of suscribers to calculate the average but by doing this I get 1 or 0 only instead of an average. I don't know what is wrong in this request. Maybe as I have made a group by the IDNumber from the table Loan. How could I get the number of all IDNumber based on this request to make the division ? Thanks
See, the request written in English:
SELECT COUNT(Loan.IDNumber) / COUNT(SUSCRIBER.IDNumber), SUSCRIBER.Name AS Name
FROM SUSCRIBER
LEFT OUTER JOIN Loan
ON SUSCRIBER.IDNumber = Loan.IDNumber
GROUP BY Loan.IDNumber;
Here are the tables:
TOPIC(Code_topic, Description)
KEY_WORD(Code_key_word, keyword)
PUBLISHER(Code_publisher, Name, Adress)
AUTHOR(Code_author, Name, Surname)
BOOK(Code_catalogue, Title, #Code_topic)
COPY (Code_bookshelf, Code_wear, Date_aquisition, #Code_publisher, #Code_catalogue)
SUSCRIBER(IDNumber, Name, Adress, Phone, Birthdate, Subscription_date)
Loan(#IDNumber,Code_bookshelf,Loan_date, Return_date)
Thank you in advance
I need to find a request to calculate the average loans per user.
If you want the average number of loans per subscriber, then you want the total number of loans divided by the number of subscribers. You can do this with an expression like this:
SELECT num_l * 1.0 / num_s
FROM (SELECT COUNT(*) as num_s FROM SUBCRIBER) s CROSS JOIN
(SELECT COUNT(*) as num_l FROM Loan) l;
The * 1.0 is because some databases do integer division, so 3 / 2 = 1 rather than 1.5.
Related
There is a table called "Athletes" which has columns for "Group" and "Award". It is necessary to calculate using Count the number of awards in groups separately and display the group with the maximum number of awards and the number of awards for this group. I tried to make a request of this type:
SELECT Max (Reward1) AS Reward
FROM (
SELECT Count (Athletes.Reward) AS Reward1
FROM Athletes
GROUP BY Athletes.Group
) AS [% $ ## # _ Alias];
This works, but the column corresponding to the maximum number of awards is not displayed for the group that received these awards. Can you please tell me what is worth adding so that this column is also displayed?
You're not selecting the GROUP column. You don't need to nest the query in a subquery, you can just pull the largest count of athlete rewards by selecting the largest row from your query.
SELECT TOP 1 Athletes.Group as Group
, Count (Athletes.Reward) AS Reward1
FROM Athletes
GROUP BY Athletes.Group
ORDER BY Reward1 DESC;
I've got a database that holds information about volunteers and their participation in a range of events.
The following query gives me a list of their names and total attendances
SELECT
volunteers.last_name,
volunteers.first_name,
count (bookings.id)
FROM
volunteers,
bookings
WHERE
volunteers.id = bookings.volunteer_id
GROUP BY
volunteers.last_name,
volunteers.first_name
I want the result table to show the distinct number of attendances and how many there are of each; So if five people did one event it'd display 1 in the first column and 5 in the second and so on.
Thanks
If I understand correctly, you want what I call a "histogram of histograms" query:
select numvolunteers, count(*) as numevents, min(eventid), max(eventid)
from (select b.eventid, count(*) as numvolunteers
from bookings b
group by b.eventid
) b
group by numvolunteers
order by numvolunteers;
The first column is the number of volunteers booked for an "event". The second is the number of events where this occurs. The last two columns are just examples of events that have the given number of volunteers.
I am currently developing a system that allows you to loan money. The thing is in order to place a loan you must pass the required or must have greater amount in your deposit in your bank acct.
Here is the schema for the deposit
tbl_fxd_dep:
fxid | amount
1 10,000
2 15,000.01
3 20,000.01
Here is the schema for the employees amount deposited
tbl_employee:
empid | amount
1 15,100.01
as you can see there the amount of the employees deposit is neither equal to 15,000.01 and 20,000.01 but rather in between those amounts
Here is my sql statement
Select
empid, fname, mname, lname, st, prv, city, cnt, fxid
from
emp as e
inner join
fd as f on e.amount >= f.amount
and e.amount <= f.amount
where
uname = #user and pwd = #pwd
This is an inner join statement that I created the problem is that whenever I try to log in using the users acct and password it does not query the profile of the user I tried removing this part and e.amount <= f.amount the fxid that is pulls up is 1 rather than showing the fxid it should belong to in this case 2 by can somebody provide me a alternative solution to this problem
THANK YOU and appreciate any effort that you guys give...
You have a pretty severe logical flaw in your join criteria - namely, since only one row is being considered at any given time, the only value that can be both less than or equal to x and greater than or equal to x is by definition equal to x. That's what marc_s is trying to point out in the comments: the criteria you're using isn't doing what you think it is. You can run through this pretty quickly, since you have a small data set: just substitute the literal value 15000.01 for e.amount and look at the data.
What you appear to want is to get the highest fxid that a given employee qualifies for (side note: why employee and not customer?). You can do that with a query like the following:
SELECT
empid
,fxid = MAX(fxid)
FROM
emp AS e
INNER JOIN fd AS f
ON e.amount <= f.amount
WHERE
uname = #user AND
pwd = #pwd
GROUP BY
empid
SQL Server is going to take this query and
Select the data for the employee
Connect that data to all values it is eligible for (deposit limit is less than the deposit amount)
Aggregate the data so it only returns one row for the employee, and the biggest fxid that the employee qualifies for.
I am trying to write a query that will use the sum function to add up all values in 1 column then divide by the count of tuples in another table. For some reason when i run the sum query by itself i get the correct number back but when i use it in my query below the value is wrong.
this is what im trying to do but the numbers are coming out wrong.
select (sum(adonated) / count(p.pid)) as "Amount donated per Child"
from tsponsors s, player p;
I found out the issue is in the sum. below returns 650,000 when it should return 25000
select (sum(adonated)) as "Amount donated per Child"
from tsponsors s, player p;
if i remove the from player p it gets the correct amount. However i need the player table to get the number of players.
I have 3 tables that are related to this query.
player(pid, tid(fk))
team(tid)
tsponsors(tid(fk), adonated, sid(fk)) this is a joining table
what i want to get is the sum of all the amounts donated to each team sum(adonated) and divide this by the number of players in the database count(pid).
I guess your sponsors are giving amounts to teams. You then want to know the proportion of donations per child in the sponsored team.
You would then need something like this:
SELECT p.tid,(SUM(COALESCE(s.adonated,0)) / COUNT(p.pid)) AS "Amount donated per Child"
FROM player p
LEFT OUTER JOIN tsponsors s ON s.tid=p.tid
GROUP BY p.tid
I also used a LEFT OUTER JOIN in order to show 0$ if a team has no sponsors.
Try
select sum(s.adonated) / (SELECT count(p.pid) FROM player p)
as "Amount donated per Child"
from tsponsors s;
Your original query joins 2 tables without any condition, which results in cross join.
UPDATE
SELECT ts.tid, SUM(ts.adonated),num_plyr
FROM tsponsors ts
INNER JOIN
(
SELECT tid, COUNT(pid) as num_plyr
FROM player
GROUP BY tid
)a ON (a.tid = ts.tid)
GROUP BY ts.tid,num_plyr
I have two tables in my setup. One with sales persons and there income. Each sales person only know their total income. For this particular income period, they are asked to give an estimate of their income on either private, small business or large business customers. This information is entered in the second table.
Income
=================
SalesPerson
Income
Distribution
=============================
SalesPerson
CustomerType
Weight
Now, my query would look something like this:
SELECT
Income.SalesPerson,
Distribution.CustomerType,
Income.Income * Distribution.Weight as DistributedIncome
FROM
Income INNER JOIN Distribution ON
Income.SalesPerson = Distribution.SalesPerson
How would I enforce, that the SUM(Weight) = 1 for each SalesPerson in Distribution?
Normalize by the sum, according to the same criteria.
SELECT
Income.SalesPerson,
Distribution.CustomerType,
Income.Income * Distribution.Weight/(
select sum(d.weight)
from distribution d
inner join income i on i.salesperson = d.salesperson
)
as DistributedIncome
FROM
Income INNER JOIN Distribution ON
Income.SalesPerson = Distribution.SalesPerson
If somehow want to select unmodified weights that sum to 1 then I believe you've got a case of the subset sum problem and you are probably not going to be able to solve this with an SQL query.