Constructing a query, for selecting a table with limit of associations - sql

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.

Related

SQL: Nested subquery is returning entries incorrectly

I'm having some trouble querying a dataset with a nested subquery, which I thought would be pretty straightforward.
I have a table of customers and their addresses dbo.PERSON_ADDRESSES and transactions with customers dbo.TRANSACT_CUSTOMERS. It is very common for customers to have multiple addresses stored in the dbo.PERSON_ADDRESSES table over time. I simply would like to use the most recent transaction in the dbo.TRANSACT_CUSOMTERS table to a table of most recent addresses from the dbo.PERSON_ADDRESSES table.
When I run the inner subquery independently, it works fine: it shows the one most recent transaction per customer like I envisioned. But, for some reason when I run this entire query, I obtain many, many addresses per customer. I don't understand why.
SELECT MaxTransaction.PERSON_ID, Addr.*
FROM dbo.PERSON_ADDRESSES AS Addr
INNER JOIN
(SELECT PERSON_ID, Max(TRANSACTION_ID) AS MaxTID
FROM dbo.TRANSACTION_CUSTOMERS
GROUP BY PERSON_ID) AS MaxTransaction
ON MaxTransaction.MaxTID = Addr.TRANSACTION_ID
I am guessing that one transaction can have multiple customers. To get one row per person, use an additional JOIN condition:
SELECT maxp.PERSON_ID, pa.*
FROM dbo.PERSON_ADDRESSES pa JOIN
(SELECT PERSON_ID, Max(TRANSACTION_ID) AS MaxTID
FROM dbo.TRANSACTION_CUSTOMERS
GROUP BY PERSON_ID
) maxp
ON maxp.person_id = pa.person_id AND
maxp.MaxTID = pa.TRANSACTION_ID;

SQL queries: how to get the value which appears the most in the total of two different tables?

Context: I want to know which vehicle brand appears the most in different accidents.
I have the table vehicle (v_number, brand).
Problem is, I have two different accident tables:
One refers to driven cars involved in an accident, let's call it acc_drive (v_number, acc_number, driver) [v_number FK vehicle]
The other refers to parked cars which are involved in an accident, let's call it acc_park (v_number, acc_number) [v_number FK vehicle, acc_number FK acc_drive]
Now, I'm trying to get the vehicle brand which appears the most in the total of the two tables. For example, if Audi cars appeared 2 times in acc_drive and 3 times in acc_park, the total number of appearences would be 5.
I'm having a really hard time trying to figure this out, so a helping hand would be much appreciated!
UNION ALL can be used to bring the tables together for the JOIN:
select v.brand, count(a.v_number)
from vehicle v left join
((select v_number
from acc_drive
) union all
(select v_number
from acc_park
)
) a
on v.v_number = a.v_number
group by v.brand
order by count(v_number) desc; -- put the biggest numbers first
Note that this uses a left join. So brands with no accidents will be included in the results.
Try this-
SELECT TOP 1 brand,COUNT(*)
FROM vehicle A
INNER JOIN acc_drive B ON A.v_number = B.v_number
INNER JOIN acc_park C ON A.v_number = C.v_number
GROUP BY brand
ORDER BY COUNT(*) DESC

Can peewee nest SELECT queries such that the outer query selects on an aggregate of the inner query?

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

Counting number of occurrences in subquery

My task is to find the number of occurrences of late timesheet submissions for each employee in our database. There are two tables which I have primarily been looking at, but I'm having trouble putting the two together and coming up with a decent view of the COUNT of occurrences and the employee ID for which they are associated with.
I have created this query which provides me with the EmployeeID for each occurrence.
SELECT db.Employee.EmployeeID
FROM db.LateTimesheets
INNER JOIN db.Employee ON Employee.LastName = LateTimesheets.LastName AND Employee.FirstName = Late Timesheets.FirstName
Now, with this simple query I have a view of the EmployeeID repeated however many times these incidents have occured. However, what I ultimately want to end up with is a table that displays a count for each occurance, along with the EmployeeID for which this count is associated with.
I would assume I would need to use the COUNT() function to count the amount of rows for each EmployeeID, and then select that value along with EmployeeID. However, I am having trouble structuring the subquery correctly, and everything I have tried thus far has only generated errors with MS SQL Server Management Studio.
A simpler version of usr's answer would be the following which avoids the construction of the derived table:
Select db.Employee.EmployeeID, Count( db.LateTimesheets.somecolumn ) As Total
From db.Employee
Left Join db.LateTimesheets
On LateTimesheets.LastName = Employee.LastName
And Late Timesheets.FirstName = Employee.FirstName
Group By db.Employee.EmployeeID
I may have misunderstood the question, but wouldn't GROUP BY solve your problem?
SELECT COUNT(db.LateTimesheets.somecolumn), db.Employee.EmployeeID
FROM db.LateTimesheets
INNER JOIN db.Employee ON Employee.LastName = LateTimesheets.LastName
AND Employee.FirstName = Late Timesheets.FirstName
GROUP BY db.Employee.EmployeeID
Just replace somecolumn with the name of a column that's actually in the table.
select e.*, isnull(lt.Count, 0) as Count
from Employee e
left join (
select LastName, count(*) as Count from LateTimesheets
) LateTimesheets lt on e.LastName = lt.LastName
The trick is to do the grouping in a derived table. You don't want to group everything, just the LateTimesheets.
We need a left join to still get employees with no LateTimesheets.

MySQL Select Statement - Two Tables, Sort One Table by Count of Other Table

So I have built a voting system for a custom post system i wrote.
I want to be able to sort by "most voted", "Most liked", etc.
I have two tables.
Entry: ID, Title, Post
Vote: ID, EntryID, Result
I want to be able to query the vote table for each entry and see how many vote's there are, and then sort the entry's by how many vote's each table had. I have messed around with joins, etc. and cannot seem to figure it out. Any suggestions?
You want to make a join and group by the entry items, then use aggregates like count and sum to get the number of votes and the sum of votes:
select e.ID, e.Title, e.Post, count(*) as Votes, sum(Result) as Result
from Entry e
inner join Vote v on v.EntryId = e.Id
group by e.ID, e.Title, e.Post
order by 4 desc