I have two tables: T_Guest, T_Table
In T_Guest I have a list of the guests. Each guest has an ID (g_no) and ID of a table (t_no) to which the guest is related.
It is possible that to the same table will be related many guests.
In the T_Table I have a list of the tables. Each table has an ID (t_no) and a field n_places that describes how many places that table does have.
QUESTION:
I need to build an SQL query (in MS Access), which will show a list of a tables ID's which are still have a free sits.
I have an idea to find first the list of the tables which are full, and than just to implement EXCEPTION on that group from the whole group of the available tables.
But to compute how many tables does have ZERO free places, I need somehow to compute for each table:
n_places - NUMBER_OF_GUESTS_ON_THAT_TABLE
Where the last one (NUMBER_OF_GUESTS_ON_THAT_TABLE) it's just:
SELECT COUNT(*)
FROM T_Table INNER JOIN T_Guest ON T_Table.t_no = T_Guest.t_no
GROUP BY T_Table.t_no
But I don't how to implement DIFFERENCE n_places - NUMBER_OF_GUESTS_ON_THAT_TABLE
Can somebody help me with that kind of a task???
You can use the guest count as a derived table:
SELECT t.t_no, t.n_Places-Nz(g.Guests,0) As FreePlaces
FROM T_Table t
LEFT JOIN (
SELECT t_no,COUNT(*) as Guests
FROM T_Guest
GROUP BY t_no) g
ON t.t_no = g.t_no
To get tables with spaces, you can add:
WHERE t.n_Places-Nz(g.Guests,0)>0
SELECT [tt].[t_no]
FROM T_Table AS tt
WHERE (SELECT COUNT(*) FROM T_Guest AS tg WHERE tg.t_no = tt.t_no) < tt.n_places
Should work in access, uses a subquery but given the context I don't think this will be an issue.
This is classic for the HAVING clause:
SELECT t_no FROM T_Guest GROUP BY t_no
HAVING COUNT(*)<(SELECT T_Table.n_Places FROM T_Table WHERE T_Table.t_no=T_Guest.t_no)
Related
From the code as shown below. I wonder that why it can select data from 2 tables in same time that are table "venue AS v" and table "as s".
or I misunderstand?
SELECT name, s.num_match
FROM venue AS v,
(SELECT venue_id, COUNT(*) AS num_match
FROM match
GROUP BY venue_id) AS s
WHERE v.venue_id = s.venue_id
LIMIT 3;
Yes you can using JOIN clause for example.
More infos here: https://www.informit.com/articles/article.aspx?p=30875&seqNum=5
Yes you can select data from as many as tables you want at the same time.
In this case you are trying to get an aggregated number from table-s and join it with the table v.
There are many ways to write the code to join the table. Above is one method which you have used.
Hi I’m trying to write a query and I’m struggling to figure out how to go about it.
I have a suppliers table and a supplier parts table I want to write a query that lists suppliers that have specified related Parts in the supplier parts table. If a supplier doesn’t have all specified related parts then they should not be listed.
At the moment I have written a very basic query that lists the supplier if they have a related supplier part that meets the criteria.
SELECT id ,name
FROM
efacdb.dbo.suppliers INNER JOIN [efacdb].[dbo].[spmatrix] ON
id = spmsupp
WHERE spmpart
IN ('ALUM_5083', 'ALUM_6082')
I only want to show the supplier if they have both parts related. Does anyone know how I could do this?
Use a subquery with counting distinct occurences:
select * from suppliers s
where 2 = (select count(distinct spmpart) from spmatrix
where id = spmsupp and spmpart in ('ALUM_5083', 'ALUM_6082'))
As a note, you can modify your query to get what you want, just by using an aggregation:
SELECT id, name
FROM efacdb.dbo.suppliers INNER JOIN
[efacdb].[dbo].[spmatrix]
ON id = spmsupp
WHERE spmpart IN ('ALUM_5083', 'ALUM_6082')
GROUP BY id, name
HAVING MIN(spmpart) <> MAX(spmpart);
If you know there are no duplicates, then having count(*) = 2 also solves the problem.
I have using the last too many hours trying to construct this sql query that i just can't wrap my head around.
I have three tables, with the following relations, i have removed the rest of the columns for simplicity.
- Jobs
id
- Company
id
- Offer
job_id
company_id
offer_type (either 'single' or 'voucher')
- Reservation
job_id
company_id
Context.
A user creates a job. Companies can make one or two offers (one of each type) on a job, a job is closed when a job gets offers from 3 different companies. Also a reservation can take one of the spots.
So i am trying to fetch all open jobs, for a listing to the company. That is all jobs which have received offers from 2 different companies.
As mentioned i have tried to come up with a query for this, so far i got.
;WITH company_offers AS
(
SELECT
DISTINCT ON(offers.company_id) offers.company_id,
count(offers.company_id) as total,
offers.job_id
FROM offers
GROUP BY offers.company_id, offers.job_id
),
counts AS
(
SELECT jobs.*,
(SELECT count(*) FROM company_offers) as offer_count,
(SELECT count(*) FROM reservations WHERE reservations.job_id = jobs.id) as reservation_count
FROM jobs
JOIN company_offers ON company_offers.job_id = jobs.id
GROUP BY jobs.id
)
SELECT offer_count+reservation_count as total
FROM counts
I have tried to fetch the offers by unique company id, in the first CTE. Then using the second CTE to count the results of the first, and also find the reservation. Then i add them together at last, and lastly i should make a condition that the total is less than 3.
But this doesn't return the expected result, in fact long from.
I would appreciate if someone could help me out, and explain aswell.
Let me know if you got question.
Some generic SQL could look like this:
select Jobs.id
from Jobs
left outer join Offer on Offer.job_id = Jobs.id
left outer join Reservation on Reservation.job_id = Jobs.id
group by Jobs.id
having count(distinct Offer.company_id) + count(distinct Reservation.company_id) < 3
If PostgreSQL does not like that count(distinct ...), you may have to include an equivalent sub-query.
By the way:
SELECT DISTINCT ... GROUP BY ..., i.e. DISTINCT and GROUP BY, usually does not work out.
I'm using peewee2.1 with python3.3 and an sqlite3.7 database.
I want to perform certain SELECT queries in which:
I first select some aggregate (count, sum), grouping by some id column; then
I then select from the results of (1), aggregating over its aggregate. Specifically, I want to count the number of rows in (1) that have each aggregated value.
My database has an 'Event' table with 1 record per event, and a 'Ticket' table with 1..N tickets per event. Each ticket record contains the event's id as a foreign key. Each ticket also contains a 'seats' column that specifies the number of seats purchased. (A "ticket" is really best thought of as a purchase transaction for 1 or more seats at the event.)
Below are two examples of working SQLite queries of this sort that give me the desired results:
SELECT ev_tix, count(1) AS ev_tix_n FROM
(SELECT count(1) AS ev_tix FROM ticket GROUP BY event_id)
GROUP BY ev_tix
SELECT seat_tot, count(1) AS seat_tot_n FROM
(SELECT sum(seats) AS seat_tot FROM ticket GROUP BY event_id)
GROUP BY seat_tot
But using Peewee, I don't know how to select on the inner query's aggregate (count or sum) when specifying the outer query. I can of course specify an alias for that aggregate, but it seems I can't use that alias in the outer query.
I know that Peewee has a mechanism for executing "raw" SQL queries, and I've used that workaround successfully. But I'd like to understand if / how these queries can be done using Peewee directly.
I posted the same question on the peewee-orm Google group. Charles Leifer responded promptly with both an answer and new commits to the peewee master. So although I'm answering my own question, obviously all credit goes to him.
You can see that thread here: https://groups.google.com/forum/#!topic/peewee-orm/FSHhd9lZvUE
But here's the essential part, which I've copied from Charles' response to my post:
I've added a couple commits to master which should make your queries
possible
(https://github.com/coleifer/peewee/commit/22ce07c43cbf3c7cf871326fc22177cc1e5f8345).
Here is the syntax,roughly, for your first example:
SELECT ev_tix, count(1) AS ev_tix_n FROM
(SELECT count(1) AS ev_tix FROM ticket GROUP BY event_id)
GROUP BY ev_tix
ev_tix = SQL('ev_tix') # the name of the alias.
(Ticket
.select(ev_tix, fn.count(ev_tix).alias('ev_tix_n'))
.from_(
Ticket.select(fn.count(Ticket.id).alias('ev_tix')).group_by(Ticket.event))
.group_by(ev_tix))
This yields the following SQL:
SELECT ev_tix, count(ev_tix) AS ev_tix_n FROM (SELECT Count(t2."id")
AS ev_tix FROM "ticket" AS t2 GROUP BY t2."event_id")
GROUP BY ev_tix
I had this sql statement running for these two tables: tblStations and tblThreads, for counting number of Stations in each thread:
SELECT tblThreads.*, (SELECT COUNT(*) FROM tblStations WHERE tblStations .fldThreadID=tblThreads.fldID) AS TotalStationsInThread FROM tblThreads;
and then display tblThread.Name and TotalStationsInThread, for each thread in the system.
Now I added another table (tblUsers) in this hierarchy:
each thread can have many users, and each user can have many stations.
The three tables are related to each other by this:
tblStations.fldUserID=tblUsers.fldID > tblUsers.fldThreadID=tblThreads.fldID.
So I changed my SQL query to this:
SELECT tblThreads.*, (SELECT COUNT(*) FROM tblStations WHERE tblStations.fldUserID=tblUsers.fldID AND tblUsers.fldThreadID=tblThreads.fldID) AS TotalStationsInThread FROM tblThreads;
But now I'm getting this message: "No value given for one or more required parameters." It's like the database can't connect the tables tblStations with tblThreads via tblUsers...
Any help please on how to count the number of stations that are connected to all the users that are connected to each thread??
This is the correct answer for MS Access Jet Database Engine:
SELECT tblThreads.*, (SELECT COUNT(tblStations.fldUserID) FROM tblStations INNER JOIN tblUsers ON tblStations.fldUserID = tblUsers.fldID WHERE tblUsers.fldThreadID = tblThreads.fldID) AS TotalStationsInThread FROM tblThreads;
Many thanks for Gordon Linoff for his answer.
You need to include tblUsers in the from clause:
SELECT tblThreads.*,
(SELECT COUNT(*)
FROM tblUsers inner join
tblStations
on tblStations.fldUserID = tblUsers.fldID
WHERE tblUsers.fldThreadID = tblThreads.fldID
) AS TotalStationsInThread
FROM tblThreads;