I work in recruiting and would like to get a list of jobs that are posted to multiple offices along with the names of those offices per job.
I started with the query below to identify the jobs with multiple offices. What is my next step for showing offices.name for only jobs with multiple offices? Bonus if I can get one record per job and have the office names fill into columns. I'm using SQL Workbench connected to Amazon Redshift. Thanks much!
select jobs_offices.job_id, count(offices.id)
from jobs_offices join offices on jobs_offices.office_id = offices.id
group by jobs_offices.job_id
having count(offices.id) >1
order by count(offices.id) DESC
Your query looks good so far, it is just missing the office name? I believe you could get a comma-separated list of the offices by adding the listagg function. Something like select jobs_offices.job_id, listagg(offices.name, ', ') from ...
Redshift docs
you can use corelated subquery
select t.* from offices t
where exists ( select 1 from jobs_offices t1 where t1.office_id=t.id
group by t1.job_id
having count(*) >1
)
Related
This is on a Postgres server using SQL. I have a supply_chain table in which I need to pull the warehouse_id from, and an order table in which I need to pull the total returns. Located on the same server.
I need to combine them on the delivery zipcode from the order table and the zipcode on the supply_chain table. I am unsure the best way to join this in SQL.
SELECT deliveryzipcode, COUNT(OrderReturned) AS Total_returned
FROM transactions_log
WHERE OrderReturned= 'Yes'
GROUP BY deliveryzipcode;
This query will successfully return the number of returns based on zipcode. So basically I need to pull those warehouse_id's and count them.
Apologize in advance for not wording this question well.
You can try this :
SELECT sc.warehouse_id, sc.zipcode, tl.Total_returned
FROM logistics_supply_chain_network AS sc
INNER JOIN
(
SELECT deliveryzipcode, COUNT(OrderReturned) AS Total_returned
FROM transactions_log
WHERE OrderReturned= 'Yes'
GROUP BY deliveryzipcode
) AS tl
ON tl.deliveryzipcode = sc.zipcode ;
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.
I have two tables. The first one is for sales (name's table is 'ventas') and the other one, for detailed articles by sale (the name is 'ventaArticulos'). Basically, the last one contains all the articles that were sold.
Those are related by the columns ventas.id_venta and ventaArticulos.id_ventaArticulo
Basically, the idea is to make an SQL SELECT for the first table (ventas) for example, getting the columns 'fecha' and 'importe' but also, perform a 'count' with the total of registers that are in the second table related by sale. (ventas.id_venta and ventaArticulos.id_ventaArticulo)
hope to be clear enough and can help me!
SQL to try to clarify (Obviously it doesn't work):
SELECT ventas.fecha, ventas.importe, count(ventaArticulos.id_codigoArt)
FROM ventas JOIN
ventaArticulos
ON ventaArticulos.id_ventaArticulo = ventas.id_venta
Thanks!
I would recommend to use table alise that could be easier to follow & you forgot to include GROUP BY Clause
SELECT v.fecha, v.importe, count(va.id_codigoArt) counts
FROM ventas v -- Use alise v entire the query instead of table_name
INNER JOIN ventaArticulos va ON va.id_ventaArticulo = v.id_venta
GROUP BY v.fecha, v.importe;
SELECT v1.fecha, v1.importe, count(v2.id_codigoArt)
FROM ventas v1 , ventaArticulos v2
where v1.id_ventaArticulo= v2.id_venta
group by v1.fecha, v1.importe
having count(*) > 1
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;
Can you help me please with the divide select in SQL? I use the Oracle Apex Express Edition. I have three tables. In English (original names are in brackets): User (Uzivatel), Event (Udalost) and Groups of users (Skupina_u). Their context is on screens:
Now I have to use divide select in SQL. This select is formulated: You must show all users who were on all events. In my tables, you can see that I have two users, who were on all events. They are part of group “SK2.” I am using this select:
select *
from Uzivatel
where not exists (select *
from Skupina_u
where not exists (select *
from Udalost
where Skupina_u.ID_uz = Uzivatel.ID_uz
and Skupina_u.ID_uz = Udalost.ID_uz))
But the result is: no data found.
Thank you for help.
This is answering the question you pose in English . . . "Find all users who are on all events."
This is an example of a set-within-sets query. I think the best approach is using aggregation (because the same structure can be used for many questions). Let me show you using the English names for the tables and columns:
select eu.UserId
from User_Events eu
group by eu.UserId
having count(distinct eu.EventId) = (select count(*) from Events e)
That is, select all the users where the number of distinct event ids is the number in the events table. If you only want events that have users, then use the following having clause:
having count(distinct eu.EventId) = select count(distinct eventId) from Events e)