in my database I want to select every construction_manual
where storage_room.quantity > component.quantity
But when I use:
SELECT construction_manual.name
FROM construction_manual cm, construction_manual_component cmc, component, storage_room
WHERE cm.ID = cmc.construction_manual_ID
AND cmc.component_ID = component.ID
AND component.storage_room_ID = storage_room.ID
AND storage_room.quantity > component.quantity
It will select every construction_manual.name as long as the first component in the storage_room has enough quantity.
So let's say...
construction_manual.1 needs
component.A -> quantity 5
component.B -> quantity 10
and in the storage room there is:
component.A -> quantity 6
component.B -> quantity 0
Although there is not enough in the storage room, construction_manual.1 will be selected. How do I select only those construction_manuals where there are enough components in the storage_room?
edit:
In my 4 tables are the following datasets. I will get the following result when I use the query mentioned above:database . But I should not be able to construct a table because there are not enough wooden_plates (wooden planks and not wooden plates ofc...ups)
I think what you want to ask is to ignore or exclude any construction manual that does not have enough components to satisfy a build. If that's the case you can do something like the below (a left join would also do it):
select distinct cm.name as construction_manual
from construction_manual cm
join construction_manual_component cmc
on cmc.construction_manual_ID = cm.ID
join component c
on c.ID = cmc.component_ID
join storage_room sr
on sr.ID = c.storage_room_ID
where sr.quantity > c.quantity
and cm.ID not in (select cm.ID
from construction_manual cm
join construction_manual_component cmc
on cmc.construction_manual_ID = cm.ID
join component c
on c.ID = cmc.component_ID
join storage_room sr
on sr.ID = c.storage_room_ID
where sr.quantity < c.quantity);
Related
I have four tables, three of which are pretty static: haul_types, dumpster_type_team (the dumpster_type_team has the many-to-many relationship between dumpster_types and teams), and users. The fourth table, hauls, has transactional data.
haul_types:
id
name
dumpster_type_team:
id
dumpster_type_id
team_id
users:
id
first_name
last_name
is_driver
team_id
hauls:
haul_type_id
haul_status_id
set_dumpster_type_id
completed_driver_id
team_id
I would like a query that has a combination of dumpster_types, haul_types, and drivers (users) and a count of the hauls they were involved in. In some cases, there should be a count of zero because some drivers haven't completed hauls for every haul_type / dumpster type combination.
Here's the query I have so far that seems to be behaving as if it is an inner join because the records are getting filtered to only show where there are matches:
SELECT
c.haul_type_id,
c.dumpster_type_id,
c.driver_id,
count(h.id) AS haul_count
FROM
hauls h
RIGHT JOIN ( SELECT DISTINCT
ht.id AS haul_type_id,
dtt.dumpster_type_id AS dumpster_type_id,
dtt.team_id AS team_id,
u.id AS driver_id
FROM
haul_types ht
CROSS JOIN dumpster_type_team dtt
CROSS JOIN users u
WHERE
u.team_id = dtt.team_id
AND u.is_driver = TRUE) c ON c.haul_type_id = h.haul_type_id
AND c.dumpster_type_id = h.set_dumpster_type_id
AND c.driver_id = h.completed_driver_id
AND c.team_id = h.team_id
WHERE
h.team_id = 9
AND h.haul_status_id = 3
AND h.completed_driver_id IS NOT NULL
GROUP BY
c.haul_type_id, c.dumpster_type_id, c.driver_id
When I run the subquery in isolation:
SELECT DISTINCT
ht.id AS haul_type_id,
dtt.dumpster_type_id AS dumpster_type_id,
dtt.team_id AS team_id,
u.id AS driver_id
FROM
haul_types ht
CROSS JOIN dumpster_type_team dtt
CROSS JOIN users u
WHERE
u.team_id = dtt.team_id
AND u.is_driver = TRUE
I get the results I want: a row for each permutation of haul_type, dumpster_type, driver_id, and team_id. However, when I run the entire query, I get filtered results despite the right join.
What I would like to have is the following:
If I have 4 haul_types: delivery, swap, live, pickup
and 2 dumpster_types: 10YD, 15YD
and 2 drivers: 1, 2
I would like a haul count for the combination of haul_type, dumpster_type, and driver. If there are no hauls matching the row, show 0:
Any help is appreciated. Thank you
The description of the question and the query seem to have little to do with each other. I don't know what a "pivot table" is supposed to be.
I would like a query that has a combination of dumpster_types, haul_types, and drivers (users) and a count of the hauls they were involved in.
This sounds like a cross join to generate the rows and then a left join/group by to calculate the results:
select d.dumpster_id, ht.haul_type_id, d.driver_id, count(h.driver_id)
from dumpster_types d cross join
haul_types ht cross join
drivers d left join
hauls h
on h.dumpster_id = d.dumpster_id and
h.haul_type_id = ht.haul_type_id and
h.driver_id = d.driver_id
group by d.dumpster_id, ht.haul_type_id, d.driver_id;
Running the query #GordonLinoff provided, exposed the issue I was facing - when applying a where clause on the top level query, the results were getting filtered to only matches. I moved the where clause to individual subqueries and now I am getting all expected results.
Not sure if this is the most efficient way to write it but it yields the correct results:
SELECT
d.dumpster_type_id,
ht.id AS haul_type_id,
u.id AS driver_id,
count(h.id) AS haul_count
FROM (
SELECT
dumpster_type_id,
team_id
FROM
dumpster_type_team
WHERE
team_id = 9) d
CROSS JOIN haul_types ht
CROSS JOIN (
SELECT
users.id
FROM
users
WHERE
users.is_driver = TRUE
AND users.team_id = 9) u
LEFT JOIN (
SELECT
id, set_dumpster_type_id, haul_type_id, completed_driver_id, team_id
FROM
hauls
WHERE
haul_status_id = 3
AND team_id = 9) h ON h.set_dumpster_type_id = d.dumpster_type_id
AND h.haul_type_id = ht.id
AND h.completed_driver_id = u.id
AND h.team_id = d.team_id
GROUP BY
d.dumpster_type_id,
ht.id,
u.id
I have 3 table (property, facility, property_facility)
Now I want to get all facilities from facility table alone with property data to given property ID.
I tried it with LEFT JOIN as below. But I cannot get all facilities from facility table.
SELECT property_id
, contract_id
, type_id
, location_id
, beds
, ROUND(price,3) as price
, f.facility_id
, f.name
, pf.facility_id
FROM facility f
LEFT JOIN property_facility pf ON pf.facility_id = f.facility_id AND pf.property_id = 6
LEFT JOIN property p USING(property_id)
WHERE p.property_id = 6
Can anybody tell me how I make this query correctly?
Your WHERE clause is filtering out the facilities that do not match. You will need to switch the USING to ON and do:
FROM facility f LEFT JOIN
property_facility pf
ON pf.facility_id = f.facility_id AND
pf.property_id = 6 LEFT JOIN
property p
ON p.property_id = pf.property_id AND p.property_id = 6
You seem to understand the concept, because you are using the same condition for the first JOIN.
Well, actually, the property_id doesn't change. So, you can just remove the WHERE clause and continue with USING if you prefer.
Have an access report that shows training programs, and which employees should be but are not trained on that program. This query is fine. Problem is that we want to only display on the report training programs which have more than 10 employees untrained. So we have the total of untrained for each program in a subtotal, but we want to filter on that value.
How can this be done?
EDIT:
Here is pass-through query to SQL Server
SELECT T.ProgramTitle
,T.ProgramCode
,AE.Code AS 'AvantiCode'
,AE.FullName
,AE.FirstName
,AE.LastName
,AE.Department
,C.Position
,AE.Shift
FROM HR_Curriculum C
INNER JOIN HR_Trainings T ON C.TrainingID = T.TrainingID
INNER JOIN HR_EmployeeDetails ED ON C.Position = ED.Postion
INNER JOIN Avanti_Employees AE ON ED.AvantiRecID = AE.RecID
LEFT JOIN HR_Employeetrainings ET ON C.TrainingID = ET.TrainingID
AND ED.AvantiRecID = ET.AvantiRecID
LEFT JOIN HR_TrainingVersion V ON V.VersionID = ET.VersionID
WHERE terminated = 0
AND T.Active = - 1
AND CompletedDate IS NULL
GROUP BY T.ProgramTitle
,T.ProgramCode
,AE.Code
,AE.FullName
,AE.FirstName
,AE.LastName
,AE.Department
,C.Position
,AE.Shift
Order by programtitle
Consider an inline view, using a grouped by table alias with HAVING clause.
Try adding one more inner join:
INNER JOIN
(SELECT TrainingID, ProgramTitle, ProgramCode
FROM HR_Trainings
GROUP BY TrainingID, ProgramTitle, ProgramCode
HAVING Count(TrainingID) > 10) AS Trainings10More
ON Trainings10More.TrainingID = T.TrainingID
I'm not sure what I'm trying to do is possible but I've been trying to get different methods to a solution I need but so far I've come up empty handed.
Lets say I have 2 tables (just an example, in my case theres a hell of a lot more + alot more data)
One called clients and the other called form_data.
We have multiple clients in the clients table and in the form_data table we have multiple rows for each company present in the clients table. In form_data we store the serialized data from different forms. (id and data)
I'm currently pulling all records from the form_data table and I am trying to use a regexp on the data column to filter for instance that the value 'motor oil' is found in them.
I would like a way to do this filter but filter the company and not the forms .. so I want to find the forms that have 'motor oil' in them and the remove all entries for COMPANIES that don't have this match, but I want to keep all the forms showing for the companies that match.
I can post my query but it is rather long and i think if we can solve the above it should be sufficient for me to implement into the actual query.
Regards
EDIT:
SELECT f.form_question_has_answer_id AS f__form_question_has_answer_id, f.form_question_has_answer_request AS f__form_question_has_answer_request,
f.form_question_has_answer_form_id AS f__form_question_has_answer_form_id, f.form_question_has_answer_user_id AS f__form_question_has_answer_user_id,
p.project_company_has_user_id AS p__project_company_has_user_id, p.project_company_has_user_project_id AS p__project_company_has_user_project_id,
p.project_company_has_user_user_id AS p__project_company_has_user_user_id, c.company_id AS c__company_id, c.company_hall_no AS c__company_hall_no,
c.company_type AS c__company_type, c.company_company_name AS c__company_company_name, c.company_country AS c__company_country,
c.company_stand_number AS c__company_stand_number, c.company_image_file_1 AS c__company_image_file_1, p2.project_id AS p2__project_id,
p2.project_name AS p2__project_name, u.user_id AS u__user_id, u.user_username AS u__user_username, f2.form_id AS f2__form_id
FROM form_question_has_answer f
INNER JOIN project_company_has_user p ON f.form_question_has_answer_user_id = p.project_company_has_user_user_id
INNER JOIN company c ON p.project_company_has_user_company_id = c.company_id
INNER JOIN project p2 ON p.project_company_has_user_project_id = p2.project_id
INNER JOIN user u ON p.project_company_has_user_user_id = u.user_id
INNER JOIN form f2 ON p.project_company_has_user_project_id = f2.form_project_id
WHERE f.form_question_has_answer_id IN ('19262', '21560', '23088', '22660', '14772', '18495', '18720', '21625', '19957', '20943')
AND ((f2.form_template_name = "custom" AND p.project_company_has_user_garbage_collection = 0 AND p.project_company_has_user_project_id = 29) AND f.form_question_has_answer_request REGEXP 'item-cadcae')
ORDER BY company_company_name asc
The query is from a doctrine query.
EDIT:
If i have a company with 10 forms in the form_data table if i filter by 'motor oil' all forms for that company that don't have motor oil are removed.. so if only 1 of the 10 forms for company id 144 has the word motor oil then the other 9 forms are lost in the query.. I want to keep them. What I want is any company that didn't find any forms with that match (motor oil) to have all their forms removed from the search.
All form data for all customers who have at least one form that matches the search:
SELECT * FROM `form_data`
WHERE `clientid` IN (
SELECT DISTINCT `clientid` FROM `form_data`
WHERE `data` RLIKE '[[:<:]]motor oil[[:>:]]'
);
SELECT
c.*
FROM
clients as c
INNER JOIN (
select
*
from
forms
where
sometext like '%motor oil%'
) as f
ON f.client = c.id
http://sqlfiddle.com/#!3/5b616/1
in this part:
select
*
from
forms
where
sometext like '%motor oil%'
you can put any query that selects the relevant rows from your forms table with the appropriate filters.
you can do the inverse too with a left outer join where null technique.. though in this case modifying the subquery to return a list of distinct clients will probably give you more like the results you expect:
SELECT
c.*
FROM
clients as c
LEFT OUTER JOIN (
select
distinct
client
from
forms
where
sometext like '%motor oil%'
) as f
ON f.client = c.id
where f.client is null
http://sqlfiddle.com/#!3/5b616/4
so with your query something like:
SELECT
c.*
FROM
company AS c
INNER JOIN (
SELECT
DISTINCT c.company_id
FROM
form_question_has_answer f
INNER JOIN project_company_has_user p
ON f.form_question_has_answer_user_id = p.project_company_has_user_user_id
INNER JOIN company c
ON p.project_company_has_user_company_id = c.company_id
INNER JOIN project p2
ON p.project_company_has_user_project_id = p2.project_id
INNER JOIN user u
ON p.project_company_has_user_user_id = u.user_id
INNER JOIN form f2
ON p.project_company_has_user_project_id = f2.form_project_id
WHERE
f.form_question_has_answer_id IN ('19262', '21560', '23088', '22660', '14772', '18495', '18720', '21625', '19957', '20943')
AND ((f2.form_template_name = "custom"
AND p.project_company_has_user_garbage_collection = 0
AND p.project_company_has_user_project_id = 29)
AND f.form_question_has_answer_request REGEXP 'item-cadcae')
ORDER BY company_company_name asc
) as f
ON f.company_id= c.id
Not sure What do you mean by keeping all the forms showing for the companies that match?
Aren't just getting at this...
select * from clients inner join form_data on clients.id = form_data.id where form_data.[data] like '%motor oil%'
This will return all forms with 'motor oil' in them filled for the companies which also have 'motor oil' in their name:
SELECT *
FROM clients c
JOIN form_data fd
ON fd.client = c.id
WHERE fd.data RLIKE '[[:<:]]motor oil[[:>:]]'
AND c.name RLIKE '[[:<:]]motor oil[[:>:]]'
I have 3 tables
person (id, name)
area (id, number)
history (id, person_id, area_id, type, datetime)
In this tables I store the info which person had which area at a specific time. It is like a salesman travels in an area for a while and then he gets another area. He can also have multiple areas at a time.
history type = 'I' for CheckIn or 'O' for Checkout.
Example:
id person_id area_id type datetime
1 2 5 'O' '2011-12-01'
2 2 5 'I' '2011-12-31'
A person started traveling in area 5 at 2011-12-01 and gave it back on 2011-12-31.
Now I want to have a list of all the areas all persons have right now.
person1.name, area1.number, area2.number, area6.name
person2.name, area5.number, area9.number
....
The output could be like this too (it doesn't matter):
person1.name, area1.number
person1.name, area2.number
person1.name, area6.number
person2.name, area5.number
....
How can I do that?
This question is, indeed, quite tricky. You need a list of the entries in history where, for a given user and area, there is an 'O' record with no subsequent 'I' record. Working with just the history table, that translates to:
SELECT ho.person_id, ho.area_id, ho.type, MAX(ho.datetime)
FROM History AS ho
WHERE ho.type = 'O'
AND NOT EXISTS(SELECT *
FROM History AS hi
WHERE hi.person_id = ho.person_id
AND hi.area_id = ho.area_id
AND hi.type = 'I'
AND hi.datetime > ho.datetime
)
GROUP BY ho.person_id, ho.area_id, ho.type;
Then, since you're really only after the person's name and the area's number (though why the area number can't be the same as its ID I am not sure), you need to adapt slightly, joining with the extra two tables:
SELECT p.name, a.number
FROM History AS ho
JOIN Person AS p ON ho.person_id = p.id
JOIN Area AS a ON ho.area_id = a.id
WHERE ho.type = 'O'
AND NOT EXISTS(SELECT *
FROM History AS hi
WHERE hi.person_id = ho.person_id
AND hi.area_id = ho.area_id
AND hi.type = 'I'
AND hi.datetime > ho.datetime
);
The NOT EXISTS clause is a correlated sub-query; that tends to be inefficient. You might be able to recast it as a LEFT OUTER JOIN with appropriate join and filter conditions:
SELECT p.name, a.number
FROM History AS ho
JOIN Person AS p ON ho.person_id = p.id
JOIN Area AS a ON ho.area_id = a.id
LEFT OUTER JOIN History AS hi
ON hi.person_id = ho.person_id
AND hi.area_id = ho.area_id
AND hi.type = 'I'
AND hi.datetime > ho.datetime
WHERE ho.type = 'O'
AND hi.person_id IS NULL;
All SQL unverified.
You're looking for results where each row may have a different number of columns? I think you may want to look into GROUP_CONCAT()
SELECT p.`id`, GROUP_CONCAT(a.`number`, ',') AS `areas` FROM `person` a LEFT JOIN `history` h ON h.`person_id` = p.`id` LEFT JOIN `area` a ON a.`id` = h.`area_id`
I haven't tested this query, but I have used group concat in similar ways before. Naturally, you will want to tailor this to fit your needs. Of course, group concat will return a string so it will require post processing to use the data.
EDIT I thikn your question has been edited since I began responding. My query does not really fit your request anymore...
Try this:
select *
from person p
inner join history h on h.person_id = p.id
left outer join history h2 on h2.person_id = p.id and h2.area_id = h.area_id and h2.type = 'O'
inner join areas on a.id = h.area_id
where h2.person_id is null and h.type = 'I'