I have 2 tables A, B where B contains a foreign key to A
ida,cola1
idb,fka,colb1
For each record from A I need to get only the last result of LEFT OUTER JOIN. The following query displays all JOINs, how to limit it to the last occurence of fka only?
SELECT ida,idb,cola1,colb1 FROM a LEFT OUTER JOIN b ON ida=fka
If the last result in table b is the one with the highest idb, then one solution is to use a subquery where you calculate max(idb) for each fka:
select
a.ida,
a.cola1,
b.idb,
b.fka,
b.colb1
from
a left outer join (
select fka, max(idb) as max_idb
from b
group by fka
) max_b on a.ida=max_b.fka
left outer join b on max_b.fka=b.fka and max_b.max_idb=b.idb
You can use group by to remove all other columns from the table B
then join the to the table A to get the result
Group by requires an aggregate function like min or max etc
Here I tried it with min
SELECT
ida, idb, cola1, colb1
FROM A LEFT OUTER JOIN (
select
min(idb) idb, fka, min(colb1) colb1
from B
group by fka
) b
ON ida = fka
SELECT
ida,idb,cola1,colb1
FROM a
LEFT OUTER JOIN b ON ida=fka
ORDER BY ida DESC LIMIT 1
May Be this one is Help you.
Related
I have the following mysql query and attempting to do group by country and type, however for all countries not all types are available but would still like to see all types for every country populated with 0.
select distinct
t1.Country,
t2.sectype,
count(t1.secid) AS SecID
from test.t2
left outer join test.t1 on test.t2.sectype= test.t1.sectype
group by t1.Country, t2.sectype;
t1 has country, sectype and secid fields and have created another table t2 which has all sectype's possible.
I get the following output:
https://i.stack.imgur.com/VAdyj.png
As you can see Germany only has 3 sectype's attached to that country but would like to see all sectype's like Canada - to be like the following output:
https://i.stack.imgur.com/ZC73H.png
Is this possible to do? Thanks
Consider a cross join of your distinct country and sectype tables. Then left join this all possible pairings to your actual data table. Finally, use a SUM condition over COUNT. Below uses table names that should be updated to your actual tables:
select cj.Country,
cj.sectype,
sum(d.secid IS NOT NULL) AS Count_SecID
from
(select n.country, s.sectype
from sectypes_table s
cross join countries_table n) cj
left outer join actual_data d
on d.sectype = cj.sectype AND d.country = cj.country
group by cj.Country,
cj.sectype;
To avoid the cross join should you have many distinct values, create such a table beforehand and replace subquery with this new table:
create table country_sectypes as (
select n.country, s.sectype
from sectypes_table s
cross join countries_table n
);
select cs.Country,
cs.sectype,
sum(d.secid IS NOT NULL) AS Count_SecID
from country_sectypes cs
left outer join actual_data d
on d.sectype = cs.sectype AND d.country = cs.country
group by cs.Country,
cs.sectype;
Rextester Demo (using actual_data for distinct country and sectype)
I have long hive query, which has 10 joins and lots of conditions, below is 3 conditions
1) If id is not equal to XFG or GHT, use field sid
join ABC_Tables on sid
join CDE_Tables on sid
2) If id is equal to XFG or GHT, Tested is null, use field pid
join ABC_Tables on kid
join CDE_Tables on kid
3) If id is equal to XFG or GHT, Tested is not null, use field pid
join ABC_Tables on kid
join CDE_Tables on kid
What am I doing,
select 1 conditions
union all
select 2 conditions
union all
select 3 conditions
am I doing right. What is the alternative of above problem.
Your conditions are allowed to be part of ON join condition. Equal/not equal to constants are allowed in Hive ( ID!='XFG')and(ID!='GHT')and(a.PID=b.PID) is allowed join condition. a.ID not in ('XFG', 'GHT') and a.sid=b.sid also should work:
select *
from a
left join b on a.ID not in ('XFG', 'GHT') and a.sid=b.sid
left join b on a.ID in ('XFG', 'GHT') and Tested is null and a.pid=b.pid
There are 3 tables: table_A{id, table_b_id, table_c_id}, table_B{id,..} and table_C{id}
I am using left join to get all of A's, joined with table_B.id.
What i need is to add a count of items from table_C.
I prefer not to do it with subquery, is it possible?
The query:
SELECT table_A.*
FROM table_A
LEFT JOIN table_B
ON table_B.id = table_A.table_b_id
WHERE table_A.{field} = {condition}
GROUP BY table_A.{some_field}
Try
SELECT table_A.*,
COUNT(table_C.id) count_of_c
FROM table_A
LEFT JOIN table_B
ON table_B.id = table_A.table_b_id
LEFT JOIN table_C
ON table_C.id = table_A.table_c_id
WHERE table_A.{field} = {condition}
GROUP BY table_A.{some_field}
On a side note: Although MySql GROUP BY extensions allows you to put columns in SELECT that are not part of GROUP BY don't do it since there is no way to tell which row to pick in a group. Therefore your SELECT clause should look like
table_A.{some_field}, <Aggregate function>(table_A.{other_field})...
or you have to join results of aggregation with table_A.
Am I on completely the wrong tack ?
I want to do a left outer join to a query generated from 2 tables , but i keep getting errors. Do I need a different approach?
t1:
ID, Surname,Firstname
t2:
ID,JobNo,Confirmed
I have the following query:
SELECT JobNo AS N, StaffID AS P, Confirmed as C,
FirstName AS F,Surname AS S
FROM gigs_players, Players
WHERE t1.StaffID=t2.StaffID AND JobNo="2"
AND (`Confirmed` IS NULL OR Confirmed ='Y' )
ORDER BY Instrument,Surname
I want to add:
LEFT OUTER JOIN contacted (ON t1.StaffID=contact.ID AND t2.JobNo=contact.JobNo)"
Can I do a left outer join to a query generated from 2 tables ?
In order to use the t1 and t2 in the left outer join that you want to add you need to join them with the first tables, you can't reference them directly in the left outer join you, Something like the following:
SELECT JobNo AS N, StaffID AS P, Confirmed as C,
FirstName AS F,Surname AS S
FROM gigs_players, Players
Inner join t1 on ...
Inner join t2 on ...
LEFT OUTER JOIN contacted c
on t1.StaffID=c.ID AND t2.JobNo = c.JobNo
WHERE t1.StaffID=t2.StaffID AND JobNo="2"
AND (`Confirmed` IS NULL OR Confirmed ='Y' )
ORDER BY Instrument,Surname
So, based in your tables' structure, define the conditions of the two joins with t1 and t2 with other tables.
Here is the an example of a left join to a sub query. This might be what you are looking for.
select
parts.id,
min(inv2.id) as nextFIFOitemid
from test.parts
left join
( select
inventory.id,
coalesce(parts.id, 1) as partid
from test.inventory
left join test.parts
on (parts.id = inventory.partid)
) inv2
on (parts.id = inv2.partid)
group by parts.id;
I have two tables, like so:
table "a" contains:
id|name
stock1|fullname
stock2|fullname2
stock3|fullname3
table "b" contains product quantities for given stock.
id|stock_id|quantity|product_id|
1|stock1|3|13
2|stock3|4|13
3|stock1|1|5
4|stock2|2|2
Now I would need to combine those two tables, so that each product takes its stock full name from table "a", and if its quanitity is not given for stock, it would still show the row with the quanitity as 0.
So from my example, product_id 13 would show as:
stock|quanitity|product_id|stock_fullname
stock1|3|13|fullname1
stock2|0|13|fullname2
stock3|4|13|fullname3
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13
It sounds like you need to use a LEFT JOIN, although the records with no quantity might show as NULL rather than zero. Something like:
SELECT a.*, b.*
FROM table_a a
LEFT JOIN table_b b ON a.stock_id = b.stock_id
try this:
SELECT stock,COALESCE(quanitity,0),product_id,stock_fullname FROM stock JOIN product
You need an outer join so that rows from the a table without a corresponding row in b are still considered. An inner join, by contrast, insists that you have a matching row. If you are pulling a value from the table where you don't have a row, you get NULL. Syntax varies between DBs and there is a distinction made depending on if it's the table on the left or right that gets the fake rows.
see other answers for syntax.
I think this query should work for your example:
SELECT a.id stock if(b.quantity IS NULL, 0, b.quantity),
b.product_id, a.name stock_fullname
FROM b
LEFT JOIN a b.stock = a.id
WHERE b.product_id = 13;
You should be able to use a LEFT JOIN to achieve this.
SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
ON a.id = b.stock_id
AND b.product_id = 13