SQLPlus - Count function across several tables - sql

i am trying to count the number of riders in my data. and i am having trouble figuring this out. sample of my output is noted below. The data comes from many different tables and I had to join the tables which is not the problem I am having. I am trying to get the count number of RIDERS by EVENT by DESCRIPTION. And still display the columns as noted below.
SQL> SELECT EVENTNAME, DESCRIPTION, RIDERS2 FROM ERP_REPORT;
EVENTNAME DESCRIPTION RIDERS
------------------------------ ------------------------------
Ace Rental Car - Fair Hill Inv Day 1 Race on Fair Hill's Easy Aaron Adams
itational level Course
Ace Rental Car - Fair Hill Inv Day 1 Race on Fair Hill's Easy Aaron Adams
itational level Course
Ace Rental Car - Fair Hill Inv Day 2 Race on Fair Hill's Inte Aaron Adams
itational rmediate level Course
Huffy's Mountain Trip Weekend 1 Race 1 on Huffy Moun Sam Adams
tain's Easy level Course
Valley Spring Water Mountain B Day 3 Race on Hoola Hut Gorge' Jay Gillgan
ike Extravaganza s Intermediate level Course
I have tried a bunch of different code but this is one Sample I have tried. I am lost on this so any help would huge!
SELECT COUNT(DISTINCT RIDERS) as "RIDERS"
2 FROM ERP_REPORT;

Are you looking for something like:
SELECT COUNT(riders) AS rider_count, eventname, description
FROM erp_report
GROUP BY eventname, description;

Related

Compute number of direct report for each employee in the organization (aggregation)

FYI I use Redshift SQL.
I have a database that looks roughly like the one below (the database has multiple columns that I'll abstract away for simplicity).
This table is a representation of the hierarchical tree within my organization.
employee manager
-------- -------
daniel louis
matt martha
martha kim
laura matt
michael martha
...
As you can see, matt appears in two distinct records, one as the employee and the other as laura's manager. Martha appears in three records, one as an employee and in two other as manager.
I'd like to find a way to compute the number of direct reports each employee has. A conditional count in which the criteria would be where employee = manager, perhaps?
I guess I could find this information using a subquery and then join it back but I was wondering if there was a more "elegant" way to do this making use of window functions maybe.
The expected output for the table above would be:
employee manager direct_reports
-------- ------- --------------
daniel louis 0
matt martha 1
martha kim 2
laura matt 0
michael martha 0
...
I would approach this with a correlated subquery:
select
t.employee,
t.manager,
(select count(*) from mytable t1 where t1.manager = t.employee) direct_reports
from mytable t
This should be a quite efficient method, especially with an index on (employee, manager).
Use a left join and aggregation:
select em.employee, em.manager, count(ew.employee)
from employees em left join
employees ew
on ew.manager = em.employee
group by em.employee, em.manager;

How to get the value of the least and most expensive book in sql?

How to get the value of the least and the most expensive book for each publisher that is based on the retail price?
I have already queried the tables below. However I just don't know how to get, which I thought would be the last 5 rows, the result for this one. Also, if I understood the question right.
select name, title, retail, sum(quantity)
from books full outer join orderitems using(isbn)
join publisher using(pubid)
group by name, title, retail
order by retail;
NAME TITLE RETAIL SUM(QUANTITY)
----------------------- ------------------------------ ---------- -------------
REED-N-RITE BIG BEAR AND LITTLE DOVE 8.95 4
READING MATERIALS INC. COOKING WITH MUSHROOMS 19.95 8
PRINTING IS US REVENGE OF MICKEY 22 5
AMERICAN PUBLISHING HANDCRANKED COMPUTERS 25 2
READING MATERIALS INC. THE WOK WAY TO COOK 28.75
READING MATERIALS INC. HOW TO GET FASTER PIZZA 29.95
READING MATERIALS INC. BODYBUILD IN 10 MINUTES A DAY 30.95 1
PRINTING IS US HOW TO MANAGE THE MANAGER 31.95 1
REED-N-RITE SHORTEST POEMS 39.95 1
PUBLISH OUR WAY E-BUSINESS THE EASY WAY 54.5 2
AMERICAN PUBLISHING DATABASE IMPLEMENTATION 55.95 7
PUBLISH OUR WAY BUILDING A CAR WITH TOOTHPICKS 59.95
AMERICAN PUBLISHING HOLY GRAIL OF ORACLE 75.95 3
REED-N-RITE PAINLESS CHILD-REARING 89.95 6
A bit hard to tell as you haven't shown us the other tables, but how about:
select publisher, min(retail), max(retail)
from books join orderitems using(isbn)
join publisher using(pubid)
group by publisher;
Key points:
You want a min and a max per publisher so group by the publisher
min(retail) gives you the minimum value of retail for all books in the group (the publisher) so that tells you the cheapest book each publisher publishes.
Similarly, max tells you the most expensive

I need to use a COUNT operation in Access SQL

I have the following tables:
PATIENT PRACTICE
- PATIENT_ID - PRACTICE_ID
- PATIENT_NAME - PRACTICE_NAME
- PRACTICE_ID
These tables keep track of patients who attend medical practices. I need to use a COUNT operation to count the number of patients that each practice has, then order the results by ascending order of number of patients.
The following is the desired output:
PracticeName NumberOfPatients
North Medical 3
East Medical 4
South Medical 5
West Medical 6
I have tried this so far, but it doesn't count it as I expected:
SELECT BRANCH.BRANCH_NAME, COUNT(EMPLOYEE.EMP_NUM)
FROM EMPLOYEE, BRANCH
GROUP BY BRANCH.BRANCH_NAME, EMPLOYEE.EMP_NUM;
This is a pretty trivial question and you should have done some rudimentary research before asking it, which is why someone voted your question down.
For the sake of helping the new guy out, here is a query that will give you those results.
SELECT pr.Practice_Name, COUNT(pa.Patent_ID) as NumberOfPatients
FROM practice pr INNER JOIN patient pa ON pr.practice_id = pa.practice_id
GROUP BY pr.practice_name
ORDER BY COUNT(pa.patientid)
You seem to be rather new to SQL, so your question is a little bit fuzzy. Try this:
select pra.practice_name,
count(pat.patient_id) as NumberOfPatients
from patient as pat inner join practice as pra on (pat.practice_id = pra.practice_id)
group by pra.practice_name
And learn the basics of joining tables to get a better understanding of SQL.

Eliminate duplicate records/rows?

I'm trying to list result from a multi-table query with on row, 2 columns. I have the correct data that I need, I merely need to trim it down to 1 line of results. In other words, eliminate duplicate entries in the result. I'm using a value not shown here, school_id. Should I go with that as a distinct value? Can I do that without displaying the school_id?
SQL> select DISTINCT(school_name),Team_Name
2 from school, team
3 where team.team_name like '%B%'
4 AND school.school_id = team.school_id;
SCHOOL_NAME TEAM_NAME
-------------------------------------------------- ----------
Lawrence Central High School Bears
Lawrence Central High School BEars
Lawrence Central High School BEARS
The problem, as I'm sure you know, is the fact that "Bears" is in 3 different cases here. The simple fix is to do the upper or lower of "Team_Name" so it will only have 1 return record.
UPPER(Team_Name)

best way to merge rows that have matching ids

i have a table of households which has the address information and city info
and then i have and individuals table of all the people in the household
it could be 1 person that belongs to the house hold or it could be 10
what i want to achieve is that if the individuals belong to the same household there information will show up in the same row as the household information all in 1 row
so if theres 10 people the inforamtion will still be in 1 row, if theres 2 people still only 1 row
household table
1 bekshire st dell MA 10001 02639 50 0002 dell NULL ALRGEN
BERKSHIRE ST NULL NULL NULL NULL
individuals that belong to household id 10001
first last code
BOB BUILDER U
JESS BUILDER A
i want
1 bekshire st dell MA 10001 02639 50 0002 dell NULL ALRGEN 1 BERKSHIRE ST BOB,JESS BUILDER U,A
The reason this is so hard is that SQL favors normalization and structure, and essentially what your asking for is to go the opposite direction. I know I'm not directly answering your question, but maybe your best bet is to consider manipulating and displaying the data on the client side and stick to simple queries to get the data from the database.