How to refine this sql query with a LEFT JOIN? - mysql

I have a problem with the following SQL Query. As you can see, I join some tables (candidate_basic,candidate_lang,province_of_candidate,province,degree_of_candidate), and then I join the result with another table "professional_experience_basic" but, the query return several instances (example: it returns two instances for id_candidate_basic=55, or it returns three instances of id_candidate_basic=59).
My problem is that I only want that the query return 1 instance per id_candidate_basic. That is, the instance with attribute "main_job" with value 1, and if this is not possible, just a null.
I tried to put AND professional_experience_basic.main_job=1 inside the WHERE clause, but, it remove the instances with main_job=null.
I feel that I'm close to the final result but I have spent lots of minutes in this last effort. Does anyone know the solution?
Thanks in advance.
The Query:
SELECT DISTINCT
candidate_basic.id_candidate_basic,
candidate_lang.town,
province.name,
degree_of_candidate.id_degree_of_candidate,professional_experience_basic.main_job
FROM (((((candidate_basic
INNER JOIN candidate_lang
ON candidate_lang.id_candidate_basic=candidate_basic.id_candidate_basic)
INNER JOIN province_of_candidate
ON province_of_candidate.id_candidate_basic=candidate_basic.id_candidate_basic)
INNER JOIN province
ON province.id_province=province_of_candidate.id_province)
INNER JOIN degree_of_candidate
ON degree_of_candidate.id_candidate_basic=candidate_basic.id_candidate_basic)
LEFT JOIN professional_experience_basic
ON professional_experience_basic.candidate_id=candidate_basic.id_candidate_basic)
WHERE candidate_basic.candidate_state=2
AND degree_of_candidate.is_main_degree
AND candidate_lang.id_website_lang=1
The current result:
The result I want:

Change professional_experience_basic.main_job to max(professional_experience_basic.main_job) in the column list, then:
GROUP BY candidate_basic.id_candidate_basic, candidate_lang.town,
province.name, degree_of_candidate.id_degree_of_candidate

First Group your Result
... AND candidate_lang.id_website_lang=1
GROUP BY
candidate_basic.id_candidate_basic,
candidate_lang.town,
province.name,
degree_of_candidate.id_degree_of_candidate
And i think you want the max of main_job, but i dont know.
change select to:
SELECT DISTINCT
candidate_basic.id_candidate_basic,
candidate_lang.town,
province.name,
degree_of_candidate.id_degree_of_candidate,MAX(professional_experience_basic.main_job)
It seems like you want the max here, but i dont know

Related

SQL Temporary Table or Select

I've got a problem with MySQL select statement.
I have a table with different Department and statuses, there are 4 statuses for every department, but for each month there are not always every single status but I would like to show it in the analytics graph that there is '0'.
I have a problem with select statement that it shows only existing statuses ( of course :D ).
Is it possible to create temporary table with all of the Departments , Statuses and amount of statuses as 0, then update it by values from other select?
Select statement and screen how it looks in perfect situation, and how it looks in bad situation :
SELECT utd.Departament,uts.statusDef as statusoforder,Count(uts.statusDef) as Ilosc_Statusow
FROM ur_tasks_details utd
INNER JOIN ur_tasks_status uts on utd.StatusOfOrder = uts.statusNR
WHERE month = 'Sierpien'
GROUP BY uts.statusDef,utd.Departament
Perfect scenario, now bad scenario :
I've tried with "union" statements but i don't know if there is a possibility to take only "the highest value" for every department.
example :
I've also heard about
With CTE tables, but I don't really get how to use it. Would love to get some tips on it!
Thanks for your help.
Use a cross join to generate the rows you want. Then use a left join and aggregation to bring in the data:
select d.Departament, uts.statusDef as statusoforder,
Count(uts.statusDef) as Ilosc_Statusow
from (select distinct utd.Departament
from ur_tasks_details utd
) d cross join
ur_tasks_status uts left join
ur_tasks_details utd
on utd.Departament = d.Departament and
utd.StatusOfOrder = uts.statusNR and
utd.month = 'Sierpien'
group by uts.statusDef, d.Departament;
The first subquery should be your source of all the departments.
I also suspect that month is in the details table, so that should be part of the on clause.

SQL LEFT JOIN with possible join condition duplicate match

Here is my query so far:
SELECT
b.cs_bidding_id, b.cs_bidding_user_id,
floor(AVG(u.cs_rating)) AS cs_user_rating
FROM cs_biddings b LEFT JOIN
cs_user_ratings u ON u.cs_user_rated_id = b.cs_bidding_user_id
I would like to get the avg rating of the user's per bidding post.
However this does not work for multiple biddings because whenever the join condition is satisfied, it wont let me fetch other avg rating for other biddings that shares the same bidding_user_id
desired result:
Unsummarized Query:
SELECT
b.cs_bidding_id,
b.cs_bidding_title,
b.cs_bidding_details,
b.cs_bidding_user_id,
b.cs_bidding_permalink,
b.cs_bidding_added,
b.cs_bidding_picture,
b.cs_bidding_status,
b.cs_bidding_location,
floor(AVG(u.cs_rating)) AS cs_owner_rating
FROM cs_biddings b LEFT JOIN
cs_user_ratings u ON u.cs_user_rated_id = b.cs_bidding_user_id
Your query is malformed. It is an aggregation query (because of the AVG()) but the SELECT columns are inconsistent with the aggregation columns (well, there are none of those).
Fixing the group by might fix your problem:
SELECT b.cs_bidding_id, b.cs_bidding_user_id,
floor(AVG(u.cs_rating)) AS cs_user_rating
FROM cs_biddings b LEFT JOIN
cs_user_ratings u
ON u.cs_user_rated_id = b.cs_bidding_user_id
GROUP BY b.cs_bidding_id, b.cs_bidding_user_id;
I'm not sure if you want both columns in the GROUP BY (and the result set). However, without sample data and desired results, it is unclear what you actually intend.

How do I sum [add] multiple columns from joined tables

I need to write a query that joins several tables and totals several columns in different tables & can't seem to figure out how to do it:
here are the tables:
The result I am trying to get is a result with the contract budgets for a particular budget name added: i.e. [this does not work but gives an idea]
select c.contract_budget_f1, c.contract_budget_f2, cb.budget_type_id, c.id, sum(cb.budget_f1) as bf1, sum(cb.budget_f2) as bf2
from `flow_contract` c
left join `flow_contract_budget` cb on cb.contract_id = c.id
where c.program_id = '69'
group by cb.budget_type_id
with the whole result set looking like:
[budget_type_id]
[contract_budget_f1]
[contract_budget_f2]
[bf1]
[bf2]
where it will return 3 rows with the budgets for each budget type added
How can I do this, is it even possible?
Here are links to the tables - sorry, didn't realize you couldn't click on them...
http://media.bigblockstudios.ca/stack/program-name.gif
http://media.bigblockstudios.ca/stack/contract-budget.gif
http://media.bigblockstudios.ca/stack/contracts.gif
UPDATE
I got it working like this:
select c.id, c.program_id, c.contract_budget_f1, c.contract_budget_f2, cb.budget_f1, cb.budget_f2, cb.budget_type_id, c.id,
sum(cb.budget_f1) as bf1, sum(cb.budget_f2) as bf2
from `flow_contract` c
left join `flow_contract_budget` cb on cb.contract_id = c.id
where c.program_id = '".$formfields['program_id']."'
group by cb.budget_type_id
order by cb.budget_type_id
I think what you want is:
group by cb.budget_type_id, cb.contract_id
Or the other way around. Could you make an sql fiddle?
Short Answer: subquery
Try this:
Start with a subquery.
Write a query that produces the column values that you want to sum. This is now the subquery.
Write an outter query that sums the desired columns in the inner query.
After you get it working, review it to see if you can optimize out the subquery and just have one query.

JOIN ON Coalesce(Formula1,Formula2,Formula3)

Using MySQL, I'm trying to do a JOIN similar to how you would use a JOIN with OR statements like:
JOIN bioguide ON (bioguide.fulldistrict=house.districtname) OR
(left(bioguide.firstname,3)=left(house.first,3) AND bioguide.lastname=house.last) OR
(bioguide.fulldistrict=house.districtname AND bioguide.lastname=house.last)
As you probably know, the problem with doing it this way is that if a bunch of different ways work, you get multiple results per row.
My hope is that there's a way to use the JOIN and use it the way the COALESCEfunction works, essentially stating (even though it doesn't work):
JOIN bioguide ON COALESCE(bioguide.firstname=house.first AND
bioguide.lastname=house.last),(left(bioguide.firstname,3)=left(house.first,3) AND
bioguide.lastname=house.last),(bioguide.fulldistrict=house.districtname AND
bioguide.lastname=house.last))
If that worked, it'd be telling SQL to first JOIN on (bioguide.firstname=house.first AND bioguide.lastname=house.last) then on (left(bioguide.firstname,3)=left(house.first,3) AND bioguide.lastname=house.last), etc.
Is something like this possible? Please let me know if other information would be helpful.
Thanks everybody!
You can structure the query with the different matches in different tables, and then use coalesce() in the select to get what you want:
select coalesce(bg1.col1, bg2.col2, bg3.col3)
from . . . left outer join
bioguide bg1
on bg1.fulldistrict=house.districtname left outer join
bioguide bg2
on left(bg2.firstname,3)=left(house.first,3) AND bg2.lastname=house.last left outer join
bioguide bg3
on bg3.fulldistrict=house.districtname AND bg3.lastname=house.last
This can still result in multiple rows, if there are multiple matches for one of the comparisons (if the same row in bioguide matches all three, you have no problem with duplicated rows).
If so, then judiciously use a group by:
group by <id column that identifies each row in the result set>

simple joins between 2 mysql tables returning all results every time.. Help!

I just imported a large amount of data into two tables. Let's call them shipments and returns.
When trying to do a simple join (left or inner) based on any criteria in these two tables. query looks like it tries to do a cross join or find every combination instead of what the query should be pulling.
each table has an PK id field, but there is not FK relationship between the two other than some shared field.
I'm currently just trying to related them on shipment_id.
I feel this is a simple answer. Am I missing a reference or something obvious that is causing this? Thanks!
here's an example. This should returned under 100 rows. This instead returns hundreds of thousands.
SELECT r.*
FROM returns as r
left outer join shipments as s
on r.shipment_id = s.shipment_id
where r.date = '2011-06-20'
Here is a query that should work:
SELECT T0.*, T1.*
FROM shipments AS T0 LEFT JOIN returns AS T1 ON T0.shipment_id = T1.shipment_id
ORDER BY T0.shipment_id;
This query join assumes 1:1 on the shipment_id
It would be nice if you included the query you were using
You need to specify what you are joining on, otherwise it will do a cartesian join:
SELECT r.*
FROM returns as r
LEFT JOIN shipments as s ON s.shipment_id = r.shipment_id
where r.date = '2011-06-20'
Josh,
I would be interested in seeing what would happen if you forced a join to a specific record or set of records instead of the whole table. Assuming there is a shipment with an id of 5 in your table, you could try:
SELECT r.* FROM returns as r
left join shipments as s
ON 5 = r.shipment_id
WHERE r.date = '2011-06-20'
While just a fancy where clause, it would at least prove that the join you are attempting will eventually work correctly. The issue is that your on clause is always returning true, no matter what the value is. This could be because it's not interpreting the shipment_id as an integer, but instead as a true/false variable where any value evaluates to true.
Original Rejected Solution:
No Foreign Key relationship should be needed in order to make the joins happen. The PK id fields I'm assuming are an integer (or number, or whatever your rdms equivalent is)?
Can you past a snippet of your sql query?
Updating based on posted query:
I would add your explicit join criteria in order to rule out any funny business (my guess is since no criteria is specified, it's using 1=1, which always joins). So I would change your query to look like:
SELECT r.*
FROM returns as r
left join shipments as s ON
s.ShipId = R.ReturnId
where r.date = '2011-06-20'
The issue turned out to be very simple, just not readily apparent until going through all the columns. It turns out that the shipment ID was duplicated through every row as it hit the upper limit for the int datatype. This is why joins were returning every record.
After switching the datatype to bigint and reimporting, everything worked great. Thanks all for looking into it.